忘記MySQL的root密碼的解決方法


經常會有朋友或者同事問起,MySQL 的 root 密碼忘了,不知道改怎么辦。

其實解決方法很簡單,下面是詳細的操作步驟。

 

(1)修改配置文件my.cnf,在配置文件[mysqld]下添加skip-grant-tables,重啟MySQL服務即可免密碼登錄

  其中--skip-grant-tables 選項前面曾經介紹過,意思是啟動 MySQL 服務的時候跳過權限表認證。 啟動后,連接到 MySQL 的 root 將不需要口令。

# SERVER SECTION
# ----------------------------------------------------------------------
#
# The following options will be read by the MySQL Server. Make sure that
# you have installed the server correctly (see above) so it reads this 
# file.
#
[mysqld]

skip-grant-tables

(2)用空密碼的 root 用戶連接到 MySQL,並且更改 root 口令:

[root@localhost mysql]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 53
Server version: 5.0.41-community-log MySQL Community Edition (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123';  
 ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

mysql> update user set authentication_string = NULL where user = 'root';
Query OK, 1 row affected (0.00 sec)

上面是先用grant的方式修改root密碼,但是由於使用了配置了skip-grant-tables 選項,使用“alter user”命令更改密碼失敗,直

接更新 user 表的 authentication_string字段后更改密碼成功。

(3)到my.cnf 中刪除skip-grant-tables選項,然后直接用mysql -uroot免密碼登錄后,在執行如下語句重設密碼

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123';  

(4)重新用 root 登錄時,必須輸入新口令。

 

  MySQL 中,密碼丟失后無法找回,只能通過上述方式修改密碼。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM