MySQL5.7误删root用户

记录一次误删root用户的处理过程

背景

在处理MySQL安全性问题时,误删除了root用户。
1. 编辑配置文件,新增参数跳过密码校验
vim /etc/my.cnf
在[mysqld]下面新增
skip-grant-tables
2. 重启数据库
/etc/init.d/mysqld restart
3. 登录数据库,创建root用户并授权
use mysql;
插入用户
insert into user set user='root',ssl_cipher='',x509_issuer='',x509_subject='';
修改权限,全部为Y,最高权限
UPDATE user SET 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',authentication_string='' WHERE User='root';
刷新权限
flush privileges;
4. 退出重新登录并修改密码
exit退出或者ctrl + d退出
vim /etc/my.cnf
删除[mysqld]下面新增的skip-grant-tables
重启数据库
/etc/init.d/mysqld restart
登录后修改密码
mysql -uroot -p
此时的root密码为空,直接回车即可登录
use mysql;
update user set authentication_string=password('123456') where user='root';
刷新权限
flush privileges;
至此,误删的root用户就还原回来了。

posted @ 2021-09-13 15:40  霸都运维  阅读(79)  评论(0编辑  收藏  举报