Mysql忘记密码重置方法

03/22/2023

 

方法一:

1、停止mysql服务
service mysqld stop

2、进入sql安全模式
/data/app/mysql/bin/mysqld_safe –skip-grant-table &

3、使用无密码方式登录
/data/app/mysql/bin/mysql -uroot;

4、切换库
use mysql;

5、重置root密码
5.1、重置前先flush privileges,
5.2、update user set password=password (“密码”) where user=’root’
(或grant all on *.* to ‘root’@’localhost’ identified by ‘密码’ with grant option;)若是grant可不用再执行flush privileges
5.3、再flush privileges

6、退出
exit;

7、重启mysql
service mysqld restart

8、使用root用户新设置的密码登录

方法二:
1、vi /etc/my.cnf    #编辑文件,找到[mysqld],在下面添加一行skip-grant-tables

2、重启mysql

3、如方法一中的4、5、6步骤

4、之后去除my.cnf中前面加的那一行

5、重启mysql

6、使用root用户新设置的密码登录