溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

MySQL誤刪root用戶怎么恢復(fù)

發(fā)布時(shí)間:2021-08-26 17:22:28 來(lái)源:億速云 閱讀:207 作者:chen 欄目:MySQL數(shù)據(jù)庫(kù)

本篇內(nèi)容介紹了“MySQL誤刪root用戶怎么恢復(fù)”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!


一個(gè)朋友在領(lǐng)導(dǎo)要求他刪除root@127.0.0.1,root@'%'等用戶,只保留root@localhost時(shí),
他寫(xiě)了一條類似delete from mysql.user where user='root'的命令……
注意,他并沒(méi)有寫(xiě) “and host=”的條件,導(dǎo)致悲劇發(fā)生,并且還flush了授權(quán)。

以下模擬誤刪操作,嘗試做恢復(fù):

MySQL版本:
MySQL 5.5.49

模擬誤刪操作:

  1. mysql> DELETE FROM mysql.user WHERE user='root';

  2. Query OK, 1 row affected (0.01 sec)


  3. mysql> FLUSH PRIVILEGES;

  4. Query OK, 0 rows affected (0.01 sec)


解決思路:
新安裝或者初始化一個(gè)新的實(shí)例(與誤刪操作的MySQL版本最好一致)
初始化好后,啟動(dòng)實(shí)例,并以root@localhost用戶登錄,然后設(shè)置密碼:

新實(shí)例上:

  1. mysql> SELECT current_user();

  2. +----------------+

  3. | current_user() |

  4. +----------------+

  5. | root@localhost |

  6. +----------------+

  7. 1 row in set (0.00 sec)


  8. mysql> SET PASSWORD=password('123456');

  9. Query OK, 0 rows affected (0.00 sec)


  10. mysql> FLUSH PRIVILEGES;

  11. Query OK, 0 rows affected (0.00 sec)



將存放在mysql.user里的root@localhost用戶信息查詢出:

  1. mysql> SELECT * FROM mysql.user WHERE user='root' AND host='localhost' INTO OUTFILE '/tmp/root.txt';

  2. Query OK, 1 row affected (0.00 sec)



對(duì)于誤刪操作的實(shí)例:
首先將之前查詢出的/tmp/root.txt文件傳到該機(jī)上,此處傳到同目錄下,操作略。

然后要停掉mysqld,并繞過(guò)授權(quán)表啟動(dòng):
可能無(wú)法通過(guò)mysqladmin shutdown來(lái)停止,此處直接kill掉mysqld_safe與mysqld,操作略。

然后啟動(dòng):

  1. [root@vm02 ~]# mysqld_safe --skip-grant-tables &

  2. [1] 2957

  3. [root@vm02 ~]# 160819 17:00:30 mysqld_safe Logging to '/data/mysql_log/err-log.err'.

  4. 160819 17:00:30 mysqld_safe Starting mysqld daemon with databases from /data/mysql


進(jìn)入mysql:

  1. [root@vm02 ~]# mysql

  2. Welcome to the MySQL monitor. Commands end with ; or \g.

  3. Your MySQL connection id is 3

  4. Server version: 5.5.49-log MySQL Community Server (GPL)


  5. Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.


  6. Oracle is a registered trademark of Oracle Corporation and/or its

  7. affiliates. Other names may be trademarks of their respective

  8. owners.


  9. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


  10. mysql> SELECT user(),current_user();

  11. +--------+----------------+

  12. | user() | current_user()  |

  13. +--------+----------------+

  14. | root@  | @               |

  15. +--------+----------------+

  16. 1 row in set (0.00 sec)


可以查看一下mysql.user表,已經(jīng)沒(méi)有了誤刪的root用戶,只剩下xxx@'ip1',yyy@'ip2',這樣的業(yè)務(wù)用戶:

  1. mysql> SELECT user,host FROM mysql.user;

  2. +------+---------------+

  3. | user  | host          |

  4. +------+---------------+

  5. | xxx  | 192.168.1.185 |

  6. | yyy  | 192.168.1.187 |

  7. +------+---------------+

  8. 2 rows in set (0.00 sec)


將之前的新實(shí)例的mysql.user表中的root@localhost信息導(dǎo)入mysql.user:

  1. mysql> LOAD DATA INFILE '/tmp/root.txt' INTO TABLE mysql.user;

  2. Query OK, 1 row affected (0.04 sec)

  3. Records: 1 Deleted: 0 Skipped: 0 Warnings: 0


  4. mysql> FLUSH PRIVILEGES;

  5. Query OK, 0 rows affected (0.00 sec)


  6. mysql> SELECT user,host FROM mysql.user WHERE user='root' AND host='localhost';

  7. +------+---------------+

  8. | user | host          |

  9. +------+---------------+

  10. | root | localhost     |

  11. +------+---------------+

  12. 1 rows in set (0.00 sec)


退出到shell環(huán)境,關(guān)閉以skip-grant-tables方式啟動(dòng)的mysqld:
此時(shí)已經(jīng)可以用mysqladmin來(lái)關(guān)閉mysqld了:

  1. [root@vm02 tmp]# mysqladmin -uroot -p123456 shutdown

  2. 160819 17:08:08 mysqld_safe mysqld from pid file /data/mysql/mysql-pid ended

  3. [1]+  Done                    mysqld_safe --skip-grant-tables  (wd: ~)

  4. (wd now: /tmp)

  5. [root@vm02 tmp]# ps -ef|grep mysql

  6. root       3938   1973  0 17:08 pts/0    00:00:00 grep mysql


再重新啟動(dòng)mysqld:

  1. [root@vm02 tmp]# mysqld_safe &

  2. [1] 3939

  3. [root@vm02 tmp]# 160819 17:08:53 mysqld_safe Logging to '/data/mysql_log/err-log.err'.

  4. 160819 17:08:53 mysqld_safe Starting mysqld daemon with databases from /data/mysql



已經(jīng)可以正常使用了,密碼是之前在初始化的新實(shí)例設(shè)置的:

  1. [root@vm02 tmp]# mysql -uroot -p123456

  2. Welcome to the MySQL monitor. Commands end with ; or \g.

  3. Your MySQL connection id is 2

  4. Server version: 5.5.49-log MySQL Community Server (GPL)


  5. Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.


  6. Oracle is a registered trademark of Oracle Corporation and/or its

  7. affiliates. Other names may be trademarks of their respective

  8. owners.


  9. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


  10. mysql> SELECT user(),current_user();

  11. +----------------+----------------+

  12. | user()            | current_user()  |

  13. +----------------+----------------+

  14. | root@localhost | root@localhost  |

  15. +----------------+----------------+

  16. 1 row in set (0.00 sec)



查看一下權(quán)限,可以對(duì)比一下,與之前的無(wú)異:

  1. mysql> SHOW GRANTS;

  2. +----------------------------------------------------------------------------------------------------------------------------------------+

  3. | Grants for root@localhost                                                                                                              |

  4. +----------------------------------------------------------------------------------------------------------------------------------------+

  5. | GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' WITH GRANT OPTION |

  6. | GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION                                                                           |

  7. +----------------------------------------------------------------------------------------------------------------------------------------+

  8. 2 rows in set (0.00 sec)

“MySQL誤刪root用戶怎么恢復(fù)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI