现象:mysql账号密码登陆失败或者忘记密码
分析:
对于mysql登陆忘记密码的情况有两种恢复方式,一种选择mysql_safe的方式重新启动mysql实例进行修改密码。一种为修改mysql配置文件再进行修改密码。两种方式的原理一样
步骤:
一、通过mysql_safe方式进行修改密码
1、启动mysql实例
# bin/mysql_safe --user=mysql --skip-grant-tables & //启动mysql实例
2、修改mysql密码
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set password=password('123') where user='root' ;
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
二、通过修改my.cnf配置文件进行密码恢复
1、修改my.cnf配置文件
[root@iZ28gd6x9vfZ mysql]# vi my.cnf
在配置文件中添加skip-grant-tables
2、重启mysql脚本并且登录mysql
3、配置密码
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set password=password('123') where user='root' ;
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
4、还原配置文件,并且重启mysql
5、重新登录mysql,密码123
备注:mysql5.7账号密码认证有些调整,但没太大变化