忘记 MySQL root 密码的解决方法:通过 mysqld_safe 选项停止和启动 MySQL 服务,在启动时添加 skip-grant-tables 选项,连接服务器后重置 root 密码。停止 MySQL 服务,编辑配置文件添加 init-file 选项,在 /var/lib/mysql/ 目录中创建 init_root_password.sql 文件,包含重置 root 密码的命令,然后启动 MySQL 服务即可。停止 MySQL 服务,以 root 用户启动 MySQL,在 MyS
忘记 MySQL root 密码怎么办?
恢复密码的方法:
方法 1:使用 mysqld_safe 选项
sudo systemctl stop mysql
skip-grant-tables 选项:sudo mysqld_safe --skip-grant-tables &
mysql -u root
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
FLUSH PRIVILEGES;
exit
sudo systemctl restart mysql
方法 2:使用重置文件
sudo systemctl stop mysql
/etc/mysql/mysql.cnf):在 [mysqld] 段落中,添加以下行:
init-file=/var/lib/mysql/init_root_password.sql
/var/lib/mysql/ 目录中创建一个名为 init_root_password.sql 的文件。在 init_root_password.sql 文件中,输入以下内容:
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
init_root_password.sql 文件。sudo systemctl start mysql
方法 3:使用低级修复工具
sudo systemctl stop mysql
sudo mysqld --user=root --console
在 MySQL 控制台中输入以下命令:
mysql> UPDATE mysql.user SET Password=PASSWORD('新密码') WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> quitsudo systemctl start mysql