MySQL5.7和8.0 破解root密码
目录
1.关闭 MySQL 服务
2.修改配置文件
3.重启 MySQL 服务
4.登录并修改密码
5.恢复配置文件并重启服务
1.关闭 MySQL 服务
systemctl stop mysqld
2.修改配置文件
使用命令vim /etc/my.cnf打开配置文件,在[mysqld]部分添加如下内容
[root@centos8 ~]#vim /etc/my.cnf[mysqld]
skip-grant-tables
skip-networking # 可选,建议添加,防止他人趁权限验证跳过期间连接数据库
3.重启 MySQL 服务
systemctl restart mysqld
4.登录并修改密码
mysql -uroot -p # 直接回车(密码为空)登录数据库UPDATE mysql.user SET authentication_string=PASSWORD('新密码') WHERE User='root';
或者
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码'; //密码修改FLUSH PRIVILEGES; //刷新权限# 查看结果
select host, user, authentication_string, plugin from mysql.user; # 退出
exit
5.恢复配置文件并重启服务
再次打开/etc/my.cnf文件,删除之前添加的skip-grant-tables这一行,保存并退出,然后执行systemctl restart mysqld重启 MySQL 服务,即可使用新密码登录。
[root@centos8 ~]#vim /etc/my.cnf[mysqld]
#skip-grant-tables
#skip-networking