vlambda博客
学习文章列表

实战-MySQL8.0版本忘记密码怎么办?

1.修改MySQL的登录设置,加上skip-grant-tables,跳过登录权限验证

[root@centos-7 ~]# vim /data/mysql8.0.20/mysql3306/tmp/mysql3306.cnf [mysqld]

port=3306

socket=/tmp/mysql3306.sock

basedir=/usr/local/mysql8.0.20

datadir=/data/mysql8.0.20/mysql3306/data

tmpdir=/data/mysql8.0.20/mysql3306/tmp

plugin-load-add=mysql_clone.so

clone=FORCE_PLUS_PERMANENT

skip-grant-tables

2.重启数据库,由于我忘记密码了,无法正常使用mysqladmin -S /tmp/mysql3306.sock -uroot -p shutdown命令关闭mysql服务,我直接kill掉后台3306数据库进程,由于是通过mysqld_safe启动的,所以kill掉之后会重启,也达到了重新加载配置文件的目的(也可以先杀掉mysqld_safe进程,再杀死3306数据库进程,这样可以停掉3306数据库线程)

[root@centos-7 ~]# kill -9 22365

[root@centos-7 ~]# /usr/local/mysql8.0.20/bin/mysqld_safe: 行 199: 22365 已杀死               env MYSQLD_PARENT_PID=21997 nohup /usr/local/mysql8.0.20/bin/mysqld --defaults-file=/data/mysql8.0.20/mysql3306/tmp/mysql3306.cnf --basedir=/usr/local/mysql8.0.20 --datadir=/data/mysql8.0.20/mysql3306/data --plugin-dir=/usr/local/mysql8.0.20/lib/plugin --user=mysql --log-error=/data/mysql8.0.20/mysql3306/logs/mysql_error.log --pid-file=centos-7.shared.pid --socket=/tmp/mysql3306.sock --port=3306 < /dev/null > /dev/null 2>&1

2020-07-13T22:55:17.642239Z mysqld_safe Number of processes running now: 0

2020-07-13T22:55:17.650764Z mysqld_safe mysqld restarted

3.登录数据库

[root@centos-7 ~]# mysql -S /tmp/mysql3306.sock

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 7

Server version: 8.0.20 MySQL Community Server - GPL

 

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

 

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

mysql> 


4.将密码置空(Tips:①不能直接修改为其他密码,亲测修改后无法登录,提示无权限;②MySQL5.7.6版本后废弃user表中的password字段和password()方法,所以旧方法重置密码对MySQL8.0行不通;③直接alter会报错)

mysql> update user set authentication_string = '' where user = 'root';

Query OK, 1 row affected (0.01 sec)

Rows matched: 1  Changed: 1  Warnings: 0

5.删除掉第一步在配置文件加的skip-grant-tables

6.重启数据库

7.登录(使用空密码,第四步已置空)

[root@centos-7 ~]# mysql -S /tmp/mysql3306.sock -uroot -p

Enter password: 

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 8

Server version: 8.0.20 MySQL Community Server - GPL

 

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

 

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

mysql> 


9.修改密码

mysql> alter user 'root'@'localhost' identified by 'Root!2020';

Query OK, 0 rows affected (0.02 sec)

10.退出,验证下修改后的密码 

mysql> quit

Bye

[root@centos-7 ~]# mysql -S /tmp/mysql3306.sock -uroot -p

Enter password: 

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 8

Server version: 8.0.20 MySQL Community Server - GPL

 

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

 

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

mysql> 

 

重置密码成功!