共計 4320 個字符,預計需要花費 11 分鐘才能閱讀完成。
本篇內容介紹了“MySQL 誤刪 root 用戶怎么恢復”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓丸趣 TV 小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
一個朋友在領導要求他刪除 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)
“MySQL 誤刪 root 用戶怎么恢復”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注丸趣 TV 網站,丸趣 TV 小編將為大家輸出更多高質量的實用文章!