关于mysql8.0忘记root密码,重新设置密码报错解决及重新设置新密码的过程?

来源:8-3 开启genelog

赵佳子彧

2019-06-19

已经按照老师的以下步骤修改完密码后无法登陆报错

# 修改mysql配置文件
[xiaokang@localhost ~]$ sudo vim /etc/my.cnf
[sudo] password for xiaokang: 
(进入到里面在末尾添加一行 skip-grant-tables
 意思是跳过授权表,这样就可以无密码登陆)
# 重启mysql服务器
[xiaokang@localhost ~]$ sudo service mysqld restart
Redirecting to /bin/systemctl restart  mysqld.service
# 直接在输入密码行,回车可以无密码登陆
[xiaokang@localhost ~]$ mysql -uroot -p
Enter password:       
# 进入mysql数据库,设置了root的账户新密码为456789
mysql> update user set authentication_string='456789' where user='root';
Query OK, 1 row affected (0.11 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> flush privileges;   # 刷新权限
Query OK, 0 rows affected (0.02 sec)

注释掉mysql设置文件中的skip-grant-tables,然后再重新启动服务器,再用新密码登陆报错

[xiaokang@localhost ~]$ mysql -uroot -p
Enter password:   

MySQL8.0ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES)
报错的原因是:MySQL8.0之前的密码认证插件是“mysql_native_password”,而现在使用的是“caching_sha2_password”。所以不能再修改authentication_string的值重新设置密码了。

解决办法:
和之前的一样现在mysql设置文件添加一行skip-grant-tables,然后可以无密码登陆

# 进入mysql数据库,先将authentication_string的值置空。
mysql> UPDATE mysql.user SET authentication_string="" WHERE user='root' and host='%';
Query OK, 1 row affected (0.32 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> flush privileges;  # 刷新权限
Query OK, 0 rows affected (0.11 sec)

可能会碰到以下报错ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement,直接刷新权限就行

mysql>  ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '456789'; 
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
# 先设置策略和密码长度
mysql>  set global validate_password.policy=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password.length=1;
Query OK, 0 rows affected (0.00 sec)
# mysql8.0 重新设置密码
mysql>  ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '456789'; 
Query OK, 0 rows affected (0.33 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.33 sec)

然后注释掉mysql设置文件中的skip-grant-tables,重新启动服务器,这样在使用新密码登录就可以了

查了好多资料终于给解决了 哎哎!!!

写回答

2回答

赵佳子彧

提问者

2019-06-19

mysql8.0重新设置新密码  https://mp.csdn.net/postedit/92839888


1
0

编程浪子

2019-06-19

你好
你说为什么你这么优秀。哈哈。目前下来你是学习最认真的之一

0
0

快速上手Linux 玩转典型应用

以主流CentOS 7 操作系统为例,系统讲解Linux,真实线上环境助你快速上手,独立配置运维服务器

1663 学习 · 572 问题

查看课程