MySQL 密碼找回

環(huán)境

centos 7.x
MySQL 5.7.20

前提

  • 1.MySQL 可以重啟
  • 2.可以登錄服務器

操作

  • 1.原密碼
[root@1]# mysql -uroot -p12345
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.20-log MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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.

(root@localhost) [(none)]>

  • 2.假設密碼忘記
- 1.配置 my.cnf
vim /etc/my.cnf // 注意每個配置文件不一定位置相同
[mysqld]
skip-grant-tables // 寫入 my.cnf 配置文件

- 2.重啟 MySQL
service mysqld restart // 重啟數據庫方法根據實際情況決定
Shutting down MySQL.. SUCCESS!
Starting MySQL... SUCCESS!
  • 3.修改密碼
[root@1]# mysql // 以上做完之后,可以直接登錄
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.20-log MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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.

(root@localhost) [(none)]> alter user 'root'@'localhost' identified by '123'; // 這時由于系統(tǒng)檢測 skip grant,是不會讓你修改密碼的。
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

(root@localhost) [(none)]>flush PRIVILEGES; // 再次讓密碼生效
Query OK, 0 rows affected (0.01 sec)

(root@localhost) [(none)]>alter user 'root'@'localhost' identified by '123'; // 修改密碼
Query OK, 0 rows affected (0.00 sec)

(root@localhost) [(none)]>flush tables; // 刷內存數據
Query OK, 0 rows affected (0.00 sec)

(root@localhost) [(none)]>quit // 退出
Bye
  • 4.成功現(xiàn)象
[root@1]# mysql // 密碼生效,skip grant 失效
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

[root@1]# mysql -uroot -p123 // 這時使用密碼登錄,就可以正常使用了
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.20-log MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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.

(root@localhost) [(none)]>

注意:記得把配置文件中 skip grant 注釋掉

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容