今天手残,一不小心,把root用户给干掉了,解决办法,就是先创建root用户,然后进行授权操作。

免密登陆

vim /etc/my.cnf


#在[mysqld] 配置免密登陆
skip-grant-tables

#重启mysql 
systemctl restart mysql

Mysql之误删root用户-yellowcong_mysql

创建root用户

#使用mysql 这个数据库
use mysql

#插入用户
insert into mysql.user(host,user,password) values("localhost","root",password("admin"));

flush privileges;

#授权到root用户
grant all on *.* to root@localhost;


#重启mysql服务
systemctl restart mysql

使用mysql这个数据库

Mysql之误删root用户-yellowcong_mysql_02

给用户添加权限

Mysql之误删root用户-yellowcong_重启_03

可以看到root用户又可以愉快得操作数据库了

Mysql之误删root用户-yellowcong_重启_04

常见问题

The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

导致这个问题得原因是,我们再 --skip-grant-tables得状态下,需要刷新一下权限flush privileges;,就可以授权了,

Mysql之误删root用户-yellowcong_root用户_05