【MySql】解决mysql没有root用户问题

时间:2025-03-19 15:09:04

发现mysql一开始没有root用户

先说这个问题产生的影响,这个跟解题有关,有必要说明,我的user表没有任何一个用户,包括root,所以一开始我要给权限,方便后续操作:

vim /etc/
skip-grant-tables     #在[mysqld]下面添加这一行,忽略权限表


然后重启mysql:

/etc//mysqld restart

开始解题:
先说解决步骤:
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用户创建成功。
下面赋予root权限:

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION; #赋予所有库所有表操作权限

mysql> flush privileges;

mysql> exit;

到这一步没有报错,表明已经成功了,不过要把最开始的配置文件恢复:

vim /etc/

删除配置文件中的:

skip-grant-tables

退出,重启mysql:

/etc//mysqld restart

————————————————
原文链接:/qq_37369726/article/details/101944373