1. 程式人生 > >MySQL 忘記root密碼的兩種處理方法

MySQL 忘記root密碼的兩種處理方法

wid pki gist 用戶 兩種 不用 clas 5.7 The

背景

  由於各個原因,我遇到過不只一次用戶忘記了MySQL的root密碼;如果是普通用戶還好,我們可以用root用戶去改它的密碼,要命

  的是把root給丟了!

  對於MySQL來說如果你忘記了root密碼,但是你又想通過改密碼的方式把root密碼找回來的話,你就要作好重啟的準備了。

方法一: skip_grant_tables + skip-networking 兩次重啟

  1): 第一步把MySQL給關掉

ps -ef | grep mysql                                                            
mysql       
939 1 0 16:39 ? 00:00:02 /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf kill -9 939

  2): 以skip-grant-tables skip-networking 模式啟動 mysqld

/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --skip-grant-tables --skip-networking &

  3): 進入mysql

  如果是mysql-5.7的話密碼的hash值保存在了authentication_string這個列裏面,5.7之前的版本保存在password列裏面

select user,host,authentication_string from mysql.user;
+---------------+-----------+-------------------------------------------+
| user          | host      | authentication_string                     |
+---------------+-----------+-------------------------------------------+
| root          | localhost |
*91B73478B18B04D13F6926FAB5A6178250EAB697 | | mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | | mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | | monitor | 127.0.0.1 | *DAD735712BB263A8DA12A091AABC625FE99DD344 | | root | 127.0.0.1 | *91B73478B18B04D13F6926FAB5A6178250EAB697 | | backup | 127.0.0.1 | *2139A3EF5FE5A0229BE550AD5ED2947B07F43B93 | | backup | localhost | *2139A3EF5FE5A0229BE550AD5ED2947B07F43B93 | | appuser | 127.0.0.1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | +---------------+-----------+-------------------------------------------+ 8 rows in set (0.01 sec)

  采用直接更新密碼hash值的方式來更新密碼

update mysql.user set authentication_string = password(MTls0352) where user=root; -- 更新密碼為MTls0352
Query OK, 2 rows affected, 1 warning (1.01 sec)
Rows matched: 2  Changed: 2  Warnings: 1

select user,host,authentication_string from mysql.user;
+---------------+-----------+-------------------------------------------+
| user          | host      | authentication_string                     |
+---------------+-----------+-------------------------------------------+
| root          | localhost | *597B32612905C92ABC495354FC276D24D0A541C1 |
| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys     | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| monitor       | 127.0.0.1 | *DAD735712BB263A8DA12A091AABC625FE99DD344 |
| root          | 127.0.0.1 | *597B32612905C92ABC495354FC276D24D0A541C1 |
| backup        | 127.0.0.1 | *2139A3EF5FE5A0229BE550AD5ED2947B07F43B93 |
| backup        | localhost | *2139A3EF5FE5A0229BE550AD5ED2947B07F43B93 |
| appuser       | 127.0.0.1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+---------------+-----------+-------------------------------------------+
8 rows in set (0.00 sec)

  4): 重啟mysqld

pkill mysql

/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf &

  5): 用新的密碼進入MySQL

mysql  -uroot -pMTls0352                                                    
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 2
Server version: 5.7.23-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> show grants;
+---------------------------------------------------------------------+
| Grants for root@localhost                                           |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO root@localhost WITH GRANT OPTION |
| GRANT PROXY ON ‘‘@‘‘ TO root@localhost WITH GRANT OPTION        |
+---------------------------------------------------------------------+
2 rows in set (0.01 sec)

  

方法二:init-file + 一次重啟

  1): 創建用於修改root密碼的sql文件

touch /tmp/change_password.sql

  內容如下

alter user root@127.0.0.1 identified by mtls0352;
alter user root@localhost identified by mtls0352;

  2): 關閉mysql服務

pkill mysqld # 我的主機上只有一個mysql服務所以用pkill mysqld 沒有問題,如果你是單機多實例請用 kill $MYSQLPID

  3): 代入修改密碼的init-file來啟動MySQL服務

/usr/local/mysql/bin/mysqld --init-file=/tmp/change_password.sql &

  4): 用新密碼登錄MySQL

mysql -uroot -pmtls0352                                              
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 3
Server version: 5.7.23-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> show grants;
+---------------------------------------------------------------------+
| Grants for root@localhost                                           |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO root@localhost WITH GRANT OPTION |
| GRANT PROXY ON ‘‘@‘‘ TO root@localhost WITH GRANT OPTION        |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec)

  5): 刪除修改密碼的sql文件

rm /tmp/change_password.sql

我的評介

  a: 第一種方法比較傳統,第二種方法“角度刁鉆” 但是兩個都能解決問題;並且第二種方法看起來步驟又少一些,但是這並不只是問題的全部

  通常一個線上的MySQL實例並不是以“/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf &” 這樣直接下命令的方式啟動的,它們

  通常和service ,systemctl 一起用的;所以這種情況下針對“方法二”還是要用service 或 systemctl把數據拉起來的,不然就不能用它們來管理

  了,所以“方法二”最終也是要兩次重啟的。

  b: 方法二能使用“alter user”語法,dba就可以不用care密碼到底保存在mysql.user表中的那個列。

  兩種方法各有好處,所以重點還是要把一個方法搞的出神入畫!

學習交流

-----------------------------http://www.sqlpy.com-------------------------------------------------

技術分享圖片技術分享圖片

-----------------------------http://www.sqlpy.com-------------------------------------------------

MySQL 忘記root密碼的兩種處理方法