最近有一批服务器要下线,在迁移数据过程中发现部分数据库的密码并没有记录在文档中,只能将其重置了,好在Root密码通常不使用,也不担心重置密码导致服务不可用,本篇记录下该过程。

修改my.cnf配置文件

mysql重置密码比较简单,只需要在my.cnf中添加如下内容即可

1
2
3
4
vim /etc/my.cnf

# 在最后一行添加
skip-grant-tables

重启MySQL

1
systemctl restart mysqld.service 

登录MySQL重置密码

1
2
3
4
5
6
#连接mysql,不需要输入密码
mysql -u root -p
# 修改root密码
update mysql.user set authentication_string=password('qwerASDF!@') where user='root' and Host = '%';
# 刷新
flush privileges;

删除skip-grant-tables

密码修改完后,将my.cnf中添加的skip-grant-tables这个配置删除或者注释了。

1
2
3
4
5
vim /etc/my.cnf

# 在最后一行添加
- skip-grant-tables
+ # skip-grant-tables

接着重启MySQL服务就好了。