1、修改密码
保存密码表 mysql.user 表
MySQL5.7开始 密码字段为 authentication_string
mysql> select host,user,authentication_string from user; +-----------+------------------+------------------------------------------------------------------------+ | host | user | authentication_string | +-----------+------------------+------------------------------------------------------------------------+ | % | user01 | $A$005$g0t1XJX#2on)}`zh_hc5DC6VCU1rGAP5PEUucvoV748HZS78ERpatFx/4.o6 | | localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | localhost | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | localhost | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | localhost | root | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | +-----------+------------------+------------------------------------------------------------------------+ 5 rows in set (0.17 sec)
在my.cnf 文件中有配置:
[mysqld] default_authentication_plugin=mysql_native_password
密码修改也可以用如下:
mysql> alter user 'user01'@'%' identified with mysql_native_password by 'a123'; Query OK, 0 rows affected (0.01 sec)
2、丢失了root用户密码
a、停止MySQL数据库服务
systemctl stop mysqld
b、编辑配置文件 /etc/my.cnf 末尾添加下面行
skip-grant-tables
c、重启MySQL服务
systemctl start mysqld
d、登录MySQL
mysql
e、查询mysql.user表
use mysql; select host,user,authentication_string from user;
f、置空root用户密码
update user set authentication_string='' where user='root';
g、刷新一下权限
flush privileges;
h、重新设置密码
alter user 'root'@'%' identified by 'abc'; alter user 'root'@'localhost' identified by 'abc';
j、退出mysql命令行后,重新登录即可