MySQL忘记密码如何解决

一,更改my.cnf,并重启mysql

[root@localhost ~]# vim /etc/my.cnf

# 在[mysqld]下添加如下内容:
[mysqld]
skip-grant-tables

[root@localhost ~]# systemctl restart mysqld 

二,登录MySQL

此时不需要输入密码,直接回车即可

mysql -u root -p 

三,修改密码

mysql> use mysql;
mysql> update user set password=password("123456") where user="root";
mysql> flush privileges;
# '123456' 是我设置的密码,这里自定义即可 

此时可能会报错:ERROR 1054 (42S22): Unknown column 'password' in 'field list'

错误原因:mysql数据库下已经没有password这个字段了,password字段改成了authentication_strin 所以更改语句为:update user set authentication_string=password(‘123456’) where user=‘root’ ;

mysql> update user set authentication_string=PASSWORD('123456') where user='root';
mysql> flush privileges;
mysql> exit;
Query OK, 1 row affected, 1 warning (0.15 sec)
Rows matched: 1 Changed: 1 Warnings: 1 

之后,记得注释掉my.cnf 中的skip-grant-tables

四, 重启数据库

systemctl restart mysqld 

五,新密码登录

mysql -u root -p 123456 
posted @ 2022-11-16 18:05  杨业壮  阅读(1076)  评论(0编辑  收藏  举报