# MySQL无法登录时重置root密码的步骤 # 第一步:修改 /etc/my.cnf 文件,跳过权限表检查 # 使用文本编辑器编辑my.cnf文件 # 在 [mysqld] 部分末尾添加 skip-grant-tables [mysqld] skip-grant-tables # 第二步:启动 MySQL 服务 # systemctl start mysqld # 第三步:连接到 MySQL # mysql -u root # 第四步:重置 root 用户的密码 # USE mysql; # SELECT host, user, authentication_string, plugin FROM user; # UPDATE user SET authentication_string=” WHERE user=’root’; # FLUSH PRIVILEGES; # ALTER USER ‘root’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘新密码’; # EXIT; # 第五步:修改 /etc/my.cnf 文件,移除 skip-grant-tables # 注释掉或删除 skip-grant-tables # 第六步:重启 MySQL 服务 # systemctl restart mysqld # 第七步:使用新密码登录 MySQL # mysql -u root -p # 输入新密码登录

内容纲要

第一步:修改 /etc/my.cnf 文件,跳过权限表检查

使用文本编辑器编辑my.cnf文件

在 [mysqld] 部分末尾添加 skip-grant-tables

[mysqld]
skip-grant-tables

第二步:启动 MySQL 服务

systemctl start mysqld

第三步:连接到 MySQL

mysql -u root

第四步:重置 root 用户的密码

USE mysql;

SELECT host, user, authentication_string, plugin FROM user;

UPDATE user SET authentication_string=” WHERE user=’root’;

FLUSH PRIVILEGES;

ALTER USER ‘root’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘新密码’;

EXIT;

第五步:修改 /etc/my.cnf 文件,移除 skip-grant-tables

注释掉或删除 skip-grant-tables

第六步:重启 MySQL 服务

systemctl restart mysqld

第七步:使用新密码登录 MySQL

mysql -u root -p

输入新密码登录