iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >MySQL误删root用户怎么恢复
  • 126
分享到

MySQL误删root用户怎么恢复

2024-04-02 19:04:59 126人浏览 八月长安
摘要

本篇内容介绍了“Mysql误删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

模拟误删操作:

  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)


解决思路:
新安装或者初始化一个新的实例(与误删操作的MySQL版本最好一致)
初始化好后,启动实例,并以root@localhost用户登录,然后设置密码:

新实例上:

  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)



对于误删操作的实例:
首先将之前查询出的/tmp/root.txt文件传到该机上,此处传到同目录下,操作略。

然后要停掉mysqld,并绕过授权表启动:
可能无法通过mysqladmin shutdown来停止,此处直接kill掉mysqld_safe与mysqld,操作略。

然后启动:

  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


进入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表,已经没有了误删的root用户,只剩下xxx@'ip1',yyy@'ip2',这样的业务用户:

  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)


将之前的新实例的mysql.user表中的root@localhost信息导入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环境,关闭以skip-grant-tables方式启动的mysqld:
此时已经可以用mysqladmin来关闭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


再重新启动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



已经可以正常使用了,密码是之前在初始化的新实例设置的:

  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)



查看一下权限,可以对比一下,与之前的无异:

  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用户怎么恢复”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!

您可能感兴趣的文档:

--结束END--

本文标题: MySQL误删root用户怎么恢复

本文链接: https://www.lsjlt.com/news/59687.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • sql中外码怎么设置
    sql 中外码设置步骤:确定父表和子表。在子表中创建外码列,引用父表主键。使用 foreign key 约束将外码列链接到父表主键。指定引用动作,以处理父表数据更改时的子表数据操作。 ...
    99+
    2024-05-15
  • sql中having是什么
    having 子句用于过滤分组结果,应用于分组后的数据集。它与 where 子句类似,但基于分组结果而不是原始数据。用法:1. 过滤分组后的聚合值。2. 根据分组后的...
    99+
    2024-05-15
  • 在sql中空值用什么表示
    在 sql 中,空值表示未知或不存在的值,可使用 null、空字符串或特殊值表示。处理空值的方法包括使用操作符(is null/is not null)、coalesce 函数(返回第一...
    99+
    2024-05-15
    oracle
  • sql中number什么意思
    sql 中的 number 类型用于存储数值数据,包括小数和整数,特别适合货币、度量和科学数据。其精度由 scale(小数点位数)和 precision(整数字段和小数字段总位数)决定。...
    99+
    2024-05-15
  • sql中空值赋值为0怎么写
    可以通过使用 coalesce() 函数将 sql 中的空值替换为指定值(如 0)。coalesce() 的语法为 coalesce(expression, replacement),其...
    99+
    2024-05-15
  • sql中revoke语句的功能
    revoke 语句用于撤销指定用户或角色的权限或角色成员资格。可撤销的权限包括 select、insert、update、delete 等,撤销的对象类型包括表、视图、存储过程...
    99+
    2024-05-15
    敏感数据
  • sql中REVOKE是什么意思
    revoke 是 sql 中用于撤销用户或角色对数据库对象权限的命令。它通过撤销权限类型、对象级别和目标权限来实现:权限类型:撤销 select、insert、update、d...
    99+
    2024-05-15
  • sql中sp是什么意思
    sql中的sp是存储过程的缩写,它是一种预编译的、已命名的sql语句块,存储在数据库中,可以被用户通过简单命令调用。存储过程的特点有:可重用性、模块化、性能优化、安全性、事务支持。存储过...
    99+
    2024-05-15
    敏感数据
  • sql中references是什么意思
    sql 中的 references 关键字用于在外键约束中定义表之间的父-子关系。外键约束确保子表中的行都引用父表中存在的行,从而维护数据完整性。references 语法的格式为:fo...
    99+
    2024-05-15
  • sql中判断字段为空怎么写
    sql 中可通过 4 种方法判断字段是否为空:1)is null 运算符;2)is not null 运算符;3)coalesce() 函数;4)case 语句。例如,查询所有 colu...
    99+
    2024-05-15
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作