MySQL誤刪root使用者恢復一例
這是一個真實又蛋疼的事情:
一個朋友在領導要求他刪除root@127.0.0.1,root@'%'等使用者,只保留root@localhost時,
他寫了一條類似delete from mysql.user where user='root'的命令……
注意,他並沒有寫 “and host=”的條件,導致悲劇發生,並且還flush了授權。
以下模擬誤刪操作,嘗試做恢復:
MySQL版本:
MySQL 5.5.49
模擬誤刪操作:
解決思路:
新安裝或者初始化一個新的例項(與誤刪操作的MySQL版本最好一致)
初始化好後,啟動例項,並以root@localhost使用者登入,然後設定密碼:
新例項上:
將存放在mysql.user裡的root@localhost使用者資訊查詢出:
對於誤刪操作的例項:
首先將之前查詢出的/tmp/root.txt檔案傳到該機上,此處傳到同目錄下,操作略。
然後要停掉mysqld,並繞過授權表啟動:
可能無法通過mysqladmin shutdown來停止,此處直接kill掉mysqld_safe與mysqld,操作略。
然後啟動:
進入mysql:
可以檢視一下mysql.user表,已經沒有了誤刪的root使用者,只剩下xxx@'ip1',yyy@'ip2',這樣的業務使用者:
將之前的新例項的mysql.user表中的root@localhost資訊匯入mysql.user:
退出到shell環境,關閉以skip-grant-tables方式啟動的mysqld:
此時已經可以用mysqladmin來關閉mysqld了:
再重新啟動mysqld:
已經可以正常使用了,密碼是之前在初始化的新例項設定的:
檢視一下許可權,可以對比一下,與之前的無異:
當然該方式缺點很明顯,需要停機操作,如果真的遇到這種蛋疼問題,可能只有在停機維護的時候做操作了。
可能還一種思路是新初始化例項之後(或者根據同業務的其他例項),將datadir/mysql中的
user.frm、user.MYD、user.MYI
覆蓋掉誤刪的例項上,當然侷限性就是需要將其他的使用者資訊做好備份,再恢復到新的mysql.user表裡
P.S.
〇 如果在執行了DELETE FROM mysql.user WHERE user='root';之後,沒有flush privileges,就可以不需要停mysqld並繞過授權表了。
〇 如果在重要的環境中通過--skip-grant-tables方式啟動mysqld,建議加上--skip-networking的引數。
作者微信公眾號(持續更新)

一個朋友在領導要求他刪除root@127.0.0.1,root@'%'等使用者,只保留root@localhost時,
他寫了一條類似delete from mysql.user where user='root'的命令……
注意,他並沒有寫 “and host=”的條件,導致悲劇發生,並且還flush了授權。
以下模擬誤刪操作,嘗試做恢復:
MySQL版本:
MySQL 5.5.49
模擬誤刪操作:
-
mysql> DELETE FROM mysql.user WHERE user='root';
-
Query OK, 1 row affected (0.01 sec)
-
-
mysql> FLUSH PRIVILEGES;
- Query OK, 0 rows affected (0.01 sec)
解決思路:
新安裝或者初始化一個新的例項(與誤刪操作的MySQL版本最好一致)
初始化好後,啟動例項,並以root@localhost使用者登入,然後設定密碼:
新例項上:
-
mysql> SELECT current_user();
-
+----------------+
-
| current_user() |
-
+----------------+
-
| root@localhost |
-
+----------------+
-
1 row in set (0.00 sec)
-
-
mysql> SET PASSWORD=password('123456');
-
Query OK, 0 rows affected (0.00 sec)
-
-
mysql> FLUSH PRIVILEGES;
- Query OK, 0 rows affected (0.00 sec)
將存放在mysql.user裡的root@localhost使用者資訊查詢出:
-
mysql> SELECT * FROM mysql.user WHERE user='root' AND host='localhost' INTO OUTFILE '/tmp/root.txt';
- Query OK, 1 row affected (0.00 sec)
對於誤刪操作的例項:
首先將之前查詢出的/tmp/root.txt檔案傳到該機上,此處傳到同目錄下,操作略。
然後要停掉mysqld,並繞過授權表啟動:
可能無法通過mysqladmin shutdown來停止,此處直接kill掉mysqld_safe與mysqld,操作略。
然後啟動:
- [root@vm02 ~]# mysqld_safe --skip-grant-tables &
- [1] 2957
- [root@vm02 ~]# 160819 17:00:30 mysqld_safe Logging to '/data/mysql_log/err-log.err'.
- 160819 17:00:30 mysqld_safe Starting mysqld daemon with databases from /data/mysql
進入mysql:
-
[root@vm02 ~]# mysql
-
Welcome to the MySQL monitor. Commands end with ; or \g.
-
Your MySQL connection id is 3
- Server version: 5.5.49-log MySQL Community Server (GPL)
-
- Copyright (c) 2000, 2016, 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> SELECT user(),current_user();
-
+--------+----------------+
-
| user() | current_user() |
-
+--------+----------------+
-
| root@ | @ |
-
+--------+----------------+
- 1 row in set (0.00 sec)
可以檢視一下mysql.user表,已經沒有了誤刪的root使用者,只剩下xxx@'ip1',yyy@'ip2',這樣的業務使用者:
-
mysql> SELECT user,host FROM mysql.user;
-
+------+---------------+
-
| user | host |
-
+------+---------------+
-
| xxx | 192.168.1.185 |
-
| yyy | 192.168.1.187 |
-
+------+---------------+
- 2 rows in set (0.00 sec)
將之前的新例項的mysql.user表中的root@localhost資訊匯入mysql.user:
-
mysql> LOAD DATA INFILE '/tmp/root.txt' INTO TABLE mysql.user;
-
Query OK, 1 row affected (0.04 sec)
-
Records: 1 Deleted: 0 Skipped: 0 Warnings: 0
-
-
mysql> FLUSH PRIVILEGES;
-
Query OK, 0 rows affected (0.00 sec)
-
-
mysql> SELECT user,host FROM mysql.user WHERE user='root' AND host='localhost';
-
+------+---------------+
-
| user | host |
-
+------+---------------+
-
| root | localhost |
-
+------+---------------+
- 1 rows in set (0.00 sec)
退出到shell環境,關閉以skip-grant-tables方式啟動的mysqld:
此時已經可以用mysqladmin來關閉mysqld了:
- [root@vm02 tmp]# mysqladmin -uroot -p123456 shutdown
- 160819 17:08:08 mysqld_safe mysqld from pid file /data/mysql/mysql-pid ended
- [1]+ Done mysqld_safe --skip-grant-tables (wd: ~)
- (wd now: /tmp)
- [root@vm02 tmp]# ps -ef|grep mysql
- root 3938 1973 0 17:08 pts/0 00:00:00 grep mysql
再重新啟動mysqld:
- [root@vm02 tmp]# mysqld_safe &
- [1] 3939
- [root@vm02 tmp]# 160819 17:08:53 mysqld_safe Logging to '/data/mysql_log/err-log.err'.
- 160819 17:08:53 mysqld_safe Starting mysqld daemon with databases from /data/mysql
已經可以正常使用了,密碼是之前在初始化的新例項設定的:
-
[root@vm02 tmp]# mysql -uroot -p123456
-
Welcome to the MySQL monitor. Commands end with ; or \g.
-
Your MySQL connection id is 2
-
Server version: 5.5.49-log MySQL Community Server (GPL)
-
-
Copyright (c) 2000, 2016, 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> SELECT user(),current_user();
-
+----------------+----------------+
-
| user() | current_user() |
-
+----------------+----------------+
-
| root@localhost | root@localhost |
-
+----------------+----------------+
- 1 row in set (0.00 sec)
檢視一下許可權,可以對比一下,與之前的無異:
-
mysql> SHOW GRANTS;
-
+----------------------------------------------------------------------------------------------------------------------------------------+
-
| Grants for root@localhost |
-
+----------------------------------------------------------------------------------------------------------------------------------------+
-
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' WITH GRANT OPTION |
-
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
-
+----------------------------------------------------------------------------------------------------------------------------------------+
- 2 rows in set (0.00 sec)
當然該方式缺點很明顯,需要停機操作,如果真的遇到這種蛋疼問題,可能只有在停機維護的時候做操作了。
可能還一種思路是新初始化例項之後(或者根據同業務的其他例項),將datadir/mysql中的
user.frm、user.MYD、user.MYI
覆蓋掉誤刪的例項上,當然侷限性就是需要將其他的使用者資訊做好備份,再恢復到新的mysql.user表裡
P.S.
〇 如果在執行了DELETE FROM mysql.user WHERE user='root';之後,沒有flush privileges,就可以不需要停mysqld並繞過授權表了。
〇 如果在重要的環境中通過--skip-grant-tables方式啟動mysqld,建議加上--skip-networking的引數。
作者微信公眾號(持續更新)

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29773961/viewspace-2123772/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- mysql誤刪資料恢復MySql資料恢復
- Mysql 誤刪資料進行恢復MySql
- mysql誤刪root使用者或者忘記root密碼解決方法MySql密碼
- mysql資料庫恢復一例MySql資料庫
- MySQL資料庫表誤刪除恢復(一)MySql資料庫
- 14、MySQL Case-線上表誤刪除恢復MySql
- hbase 恢復 誤刪除
- Oracle恢復誤刪資料Oracle
- eclipse 恢復誤刪檔案Eclipse
- Mysql update誤操作恢復MySql
- 電腦照片誤刪了怎麼恢復?電腦誤刪檔案照片恢復教程
- MySQL資料災難挽救之ibdata檔案誤刪恢復MySql
- 電腦檔案誤刪除了怎麼恢復找回?誤刪電腦資料恢復方法教程資料恢復
- 磁碟誤刪卷資料恢復工具資料恢復
- mysql 誤刪除表內資料,透過binlog日誌恢復MySql
- ibbackup恢復報錯一例
- 05、MySQL Case-MySQL binlog誤清除恢復MySql
- Mongodb資料庫誤刪後的恢復MongoDB資料庫
- git恢復誤刪未提交的檔案Git
- 電腦誤刪檔案怎麼恢復?
- 佳能相機誤刪照片怎麼恢復?
- 被誤刪的檔案快速恢復方法
- 如何有效恢復誤刪的HDFS檔案
- 【北亞資料恢復】誤刪除oracle表和誤刪除oracle表資料的資料恢復方法資料恢復Oracle
- Sybase ASE資料庫恢復,Sybase資料恢復,資料誤刪除恢復工具READSYBDEVICE資料庫資料恢復dev
- 【伺服器資料恢復】LINUX誤刪除、誤格式化怎麼恢復資料?伺服器資料恢復Linux
- 【NetApp資料恢復案例】針對NetApp誤刪除資料的恢復APP資料恢復
- 寶塔資料庫恢復 mysql資料庫丟失恢復 mysql資料庫刪除庫恢復 寶塔mysql資料庫恢復資料庫MySql
- Oracle恢復一例--ORA-03113、ORA-24324,ORA-01041錯誤Oracle
- Linuxt恢復誤刪內容:命令lsof、extundelete工具Linuxdelete
- win10誤刪檔案怎麼恢復Win10
- MySQL 如何重建/恢復刪除的 sys SchemaMySql
- Sybase SQL Anywhere(ASA)資料庫恢復,ASA資料恢復,資料誤刪除恢復工具ReadASADBSQL資料庫資料恢復
- u盤東西誤刪怎麼恢復 恢復u盤檔案的方法
- 【儲存資料恢復案例】Netapp誤操作刪除lun的資料恢復資料恢復APP
- 【儲存資料恢復】NetApp儲存誤刪除的資料恢復案例資料恢復APP
- win10中mdf檔案被誤刪如何恢復_win10系統mdf檔案被誤刪的恢復方法Win10
- SQL Server資料庫恢復,SQL Server資料恢復,SQL Server資料誤刪除恢復工具SQLRescueSQLServer資料庫資料恢復
- docker筆記40-ceph osd誤刪除恢復Docker筆記