MySQL误删root用户怎么恢复
发布时间:2022-01-19 13:27:43 所属栏目:MySql教程 来源:互联网
导读:本篇内容介绍了MySQL误删root用户怎么恢复的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成! 一个朋友在领导要求他删除root@127.0.0.1,root@%等用户,
本篇内容介绍了“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> 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用户怎么恢复”的内容就介绍到这里了,感谢大家的阅读。 (编辑:91站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |