Mysql 忘记密码

一、更改my.cnf配置文件

1、用命令编辑my.cnf配置文件,即

vim /etc/my.cnf
vi /etc/my.cnf
nano /etc/my.cnf

2.在[mysqld]下添加skip-grant-tables,然后保存并退出

skip-grant-tables

3. 重启mysql服务:

service mysqld restarte

二. 修改密码

1.重启以后,执行mysql -uroot -p(不需要密码直接Enter)进入mysql命令行

2.sql语句修改密码并执行退出mysql

mysql> USE mysql ;
mysql> UPDATE user SET authentication_string = password ( 'new_password' ) WHERE User = 'root' ;
mysql> flush privileges ;
mysql> quit

备注:

注意:如果执行修改密码命令报错,执行后报错 ERROR 1054(42S22) Unknown column 'password' in ‘field list’ 错误的原因是 5.7版本下的mysql数据库下没有password这个字段,password字段改成了authentication_string

 

3. 将MySQL的登录设置修改回来

重新执行vim /etc/my.cnf,然后删除刚刚加入的 skip-grant-tables,重新启动mysql

  

 

posted @ 2021-06-18 08:57  魏三斗  阅读(41)  评论(0编辑  收藏  举报