解决mysql没有root用户问题

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

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

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

然后重启mysql:

/etc//mysqld restart

先说解决步骤:
1.创建root用户
2.给予root所有权限
过程:
1.创建root用户:

create user 'root'@'localhost' identified by '123457';

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 '123457';

如果再次报错,这步没报错的也是直接跳到赋予权限那一步,报错的以下操作:

drop user 'root'@'localhost';

再次重新创建用户:

create user 'root'@'localhost' identified by '123457';

结果没有再报错,root用户创建成功。
下面赋予root权限:

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

flush privileges;

exit

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

#删除配置文件中的
vim /etc/
skip-grant-tables

退出,重启mysql:

/etc//mysqld restart

最后,测试:

mysql -uroot -p123457   #可以登入的话,表示前面的操作没问题

然后:

show databases;   #出来下面四张表,表示:恭喜你成功了!hh
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+