MySQL本地管理员忘记密码
解决办法
1.重启数据库到无验证模式
[root@oldboy ~]# vim /etc/my.cnf
[root@oldboy ~]# cat /etc/my.cnf
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/3306/data
port=3306
socket=/tmp/mysql.sock
skip-grant-tables
skip-networking
[client]
socket=/tmp/mysql.sock
[root@oldboy ~]# systemctl restart mysqld.service
2.修改登录密码
[root@oldboy ~]# mysql
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, 2020, 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>
mysql> flush privileges; #重新加载授权表
Query OK, 0 rows affected (0.01 sec)
mysql> alter user root@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
[root@oldboy ~]# vim /etc/my.cnf
[root@oldboy ~]# cat /etc/my.cnf
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/3306/data
port=3306
socket=/tmp/mysql.sock
[client]
socket=/tmp/mysql.sock
3.正常启动数据库
[root@oldboy ~]# systemctl restart mysqld.service
[root@oldboy ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
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, 2020, 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>