记一次 MySQL 5.7 误删管理员 ‘root‘@‘localhost‘ 账户后的解决方案


前言

在我们的 MySQL 中,有个 ‘root’@‘localhost’,如果删除该用户后,就无法对其他用户授权了,即使你有一个 ALL PRIVILEGES 的普通用户也是不能授权的(仅可创建用户、增删改查等),本次是在创建了一个 'root'@'%',并赋予 ALL PRIVILEGES ON *.* 的情况下不小心删掉了原来的 'root'@'localhost' 用户的实验环境下进行演示的。

删除后的现象是:能正常的增删改查,但是 'root'@'%' 这个用户不能对其他用户授权(可创建用户),为了解决这个问题,需要将删掉的 'root'@'localhost' 用户恢复过来,接下来是详细操作步骤。

一、恢复步骤

1、stop 数据库

systemctl stop mysqld.service

2、跳过密码验证登录数据库

/opt/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql --skip-grant-tables &

重新再打开另一个终端登录数据库

mysql -u root -p
# 回车直接登录即可

3、更改 root 口令

use mysql;
insert into user set user='root',ssl_cipher='',x509_issuer='',x509_subject='';
flush privileges;

GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'passwd' WITH GRANT OPTION;
flush privileges;

4、查看新添加用户权限

select * from mysql.user where user='root'\G;

*************************** 1. row ***************************
                  Host: localhost
                  User: root
           Select_priv: Y
           Insert_priv: Y
           Update_priv: Y
           Delete_priv: Y
           Create_priv: Y
             Drop_priv: Y
           Reload_priv: Y
         Shutdown_priv: Y
          Process_priv: Y
             File_priv: Y
            Grant_priv: Y
       References_priv: Y
            Index_priv: Y
            Alter_priv: Y
          Show_db_priv: Y
            Super_priv: Y
 Create_tmp_table_priv: Y
      Lock_tables_priv: Y
          Execute_priv: Y
       Repl_slave_priv: Y
      Repl_client_priv: Y
      Create_view_priv: Y
        Show_view_priv: Y
   Create_routine_priv: Y
    Alter_routine_priv: Y
      Create_user_priv: Y
            Event_priv: Y
          Trigger_priv: Y
Create_tablespace_priv: Y
              ssl_type: 
            ssl_cipher: 
           x509_issuer: 
          x509_subject: 
         max_questions: 0
           max_updates: 0
       max_connections: 0
  max_user_connections: 0
                plugin: mysql_native_password
 authentication_string: *BB7F095E4808E1C1A5B7AAE4DE56F814CEF5C5CC
      password_expired: N
 password_last_changed: 2021-12-04 09:30:28
     password_lifetime: NULL
        account_locked: N
1 row in set (0.00 sec)

*************************** 1. row ***************************
                  Host: %
                  User: root
           Select_priv: Y
           Insert_priv: Y
           Update_priv: Y
           Delete_priv: Y
           Create_priv: Y
             Drop_priv: Y
           Reload_priv: Y
         Shutdown_priv: Y
          Process_priv: Y
             File_priv: Y
            Grant_priv: N
       References_priv: Y
            Index_priv: Y
            Alter_priv: Y
          Show_db_priv: Y
            Super_priv: Y
 Create_tmp_table_priv: Y
      Lock_tables_priv: Y
          Execute_priv: Y
       Repl_slave_priv: Y
      Repl_client_priv: Y
      Create_view_priv: Y
        Show_view_priv: Y
   Create_routine_priv: Y
    Alter_routine_priv: Y
      Create_user_priv: Y
            Event_priv: Y
          Trigger_priv: Y
Create_tablespace_priv: Y
              ssl_type: 
            ssl_cipher: 
           x509_issuer: 
          x509_subject: 
         max_questions: 0
           max_updates: 0
       max_connections: 0
  max_user_connections: 0
                plugin: mysql_native_password
 authentication_string: *A1F1CB851D62F002C09A0C9C4A76262473432F55
      password_expired: N
 password_last_changed: 2021-12-04 09:30:28
     password_lifetime: NULL
        account_locked: N
1 row in set (0.00 sec)

5、重启 MySQL 服务

Ctrl c 结束第二步,然后重新启动 MySQL

systemctl start mysqld.service

二、登录验证

mysql -u root -p
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 147
Server version: 5.7.34-log MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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.

root@mysql 13:02:  [(none)]>

小结

不管是再测试环境还是生产环境,都要特别小心对数据库的 root 管理用户使用,一定要建立一个程序用户(读写)和一个查询用户(只读),避免使用 root 来直接测试,这样的话风险非常大(尤其是在生产环境中)。

<点击跳转至开头>

  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云计算-Security

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值