前言
在我们的 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 来直接测试,这样的话风险非常大(尤其是在生产环境中)。