1.前言
當你搭建了MySQL服務,或者使用別人搭建好的MySQL服務,忘記root密碼怎麼辦?今天跟大家簡單分享下處理方法。
不要忘記密碼,要記錄好
由於MySQL不同版本處理的方式不一樣,這裡為大家分享3個大版本的重置方法。
2.MySQL 5.1.26 重置root密碼(適用低版本的MySQL)
1.重啟mysql服務,並在啟動腳本里添加參數–skip-grant-tables 。
$ mysql -h127.0.0.1 -P3306 -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 1
Server version: 5.1.26-rc Source distribution
Type 'help;' or 'h' for help. Type 'c' to clear the buffer.
mysql> use mysql
//將密碼置空
mysql> update user set password=password('你要設置的密碼') where user='root';
mysql> flush privileges;2.用root用戶進行空密碼登錄設置新密碼。
$ ./mysqld_safe --defaults-file=./my.cnf &
3.去除免密碼登錄,並重啟mysql服務注釋掉步驟1的語句 。
$ ./mysqld_safe --defaults-file=./my.cnf &4.這時候使用新密碼登錄。
3.MySQL 5.7.28 重置root密碼
1.修改配置文件my.cnf免密碼登錄。
在【mysqld】模塊添加:skip-grant-tables 保存退出;
2.重啟mysql服務。
3.用root用戶進行登錄設置新密碼,注意從5.7版本開始,不再是password這個字段賦值。
mysql> use mysql
//將密碼置空
mysql> update user set authentication_string=password('你要設置的密碼') where user='root';
mysql> flush privileges;
新的語法
4.去除免密碼登錄,並重啟mysql服務注釋掉步驟1的語句 。
# skip-grant-tables5.這時候使用新密碼登錄。
4.MySQL 8.0忘記密碼後重置密碼
1.修改配置文件my.cnf免密碼登錄。
在【mysqld】模塊添加:skip-grant-tables 保存退出;
2.重啟mysql服務。
3.用root用戶進行登錄置空密碼,這一步很重要。
mysql> use mysql
//將密碼置空
mysql> update user set authentication_string = '' where user = 'root';
4.去除免密碼登錄,並重啟mysql服務注釋掉步驟1的語句。
# skip-grant-tables5.用root用戶進行空密碼登錄修改密碼。
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root密碼';注意:root密碼需要複雜點
5.MySQL 5.7.28 創建用戶並設置密碼
上面講了root如何重置,下面順便跟大家講下如何創建用戶密碼。
$ mysql -h127.0.0.1 -P3306 -uroot -p
mysql> grant all privileges on testuser.* to testuser@'%' identified by 'testuser';
ERROR 1290 (HY000): Unknown error 1290
mysql> flush privileges;
Query OK, 0 rows affected (0.07 sec)
mysql> grant all privileges on testuser.* to testuser@'%' identified by 'testuser';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)5.結束
通過三個不同版本的MySQL版本,你知道怎麼重置數據庫root密碼了嗎?
是不是很簡單,你也可以做到的。
原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/251353.html
微信掃一掃
支付寶掃一掃