(1)登錄到數據庫所在服務器,手工kill掉MySQL進程:
kill ' cat /mysql-data-directory/hostname.pid'
其中,/mysql-data-directory/hostname.pid指的是MySQL數據目錄下的.pid文件,它記錄了MySQL服務的進程號。
(2)使用--skip-grant-tables選項重啟MySQL服務:
[root@iZ28dr6w0qvZ mysql]# ./bin/mysqld_safe --skip-grant-tables --user=root & [1] 17299 [root@iZ28dr6w0qvZ mysql]# 151006 13:14:41 mysqld_safe Logging to '/alidata/log/mysql/error.log'. 151006 13:14:41 mysqld_safe Starting mysqld daemon with databases from /alidata/server/mysql/data
其中--skip-grant-tables選項的意思是啟動MySQL服務的時候跳過權限表認證。啟動后,連接到MySQL的root將不需要命令。
(3)用空密碼的root用戶連接到MySQ,並且更新root口令:
[root@iZ28dr6w0qvZ ~]# mysql -uroot Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.5.37-log MySQL Community Server (GPL) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> set password = password('ysj123'); ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement mysql> update mysql.user set password=password('123456') where user='root' and host='localhost'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0
此時,由於使用了--skip-grant-tables選項啟動,使用“set password”命令更改密碼失敗,直接更新user表的password字段后更改密碼成功。
(4)刷新權限表,使得權限認證重新生效:
mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
(5)重新用root登錄時,必須輸入新口令:
[root@iZ28dr6w0qvZ ~]# mysql -uroot ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) [root@iZ28dr6w0qvZ ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 5.5.37-log MySQL Community Server (GPL) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
注意:在MySQL中,密碼丟失后無法找回,只能通過上述方式修改密碼。
別忘了給個贊哦!~