莫名其妙mysql.user表的root没了,远程肯定登陆不上了,千万别卸载mysql,数据会没的
我的user表没有任何一个root用户,新手都有的我没了。。。
一开始我要设置不给密码,方便后续操作:
vim /etc/my.cnf
skip-grant-tables #在[mysqld]下面添加这一行,忽略权限表
然后重启mysql:
systemctl restart mysqld.service
先说解决步骤:
1.创建root用户
2.给予root所有权限
过程:
1.创建root用户:
create user 'root'@'localhost' identified by '123456';
localhost表示本地,mysql登入的时候,不用指定ip登入
此步骤可能会报以下错误,没报错的跳过(直接到权限那一步),用一下方法解决:
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
输入:
flush privileges;刷新配置
此时再次重新创建用户:
create user 'root'@'localhost' identified by '123456';
再次报错,这步没报错的也是直接跳到赋予权限那一步,报错的以下操作:
drop user 'root'@'localhost';
再次重新创建用户:
create user 'root'@'localhost' identified by '123456';
结果没有再报错,root用户创建成功。
2.下面赋予root权限:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION; #赋予所有库所有表操作权限
mysql> flush privileges;
mysql> exit;
到这一步没有报错,表明已经成功了,
不过要把最开始的配置文件恢复:
vim /etc/my.cnf
删除配置文件中的:
skip-grant-tables
退出,重启mysql:
systemctl restart mysqld.service