1. 程式人生 > >來自轉發的關於修改mysql密碼 忘記MySQL的root密碼的解決方法

來自轉發的關於修改mysql密碼 忘記MySQL的root密碼的解決方法

忘記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 口令:

複製程式碼
[[email protected] 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 中,密碼丟失後無法找回,只能通過上述方式修改密碼。

經常會有朋友或者同事問起,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 口令:

複製程式碼
[[email protected] 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 中,密碼丟失後無法找回,只能通過上述方式修改密碼。