我刚刚在Ubuntu上安装了MySQL,并且root用户无法登录:)

我该如何找回密码?使用空白输入密码不起作用。

最佳答案

您可以通过使用--skip-grant-tables运行服务器来重置root密码,并通过以root身份运行以下命令(或使用sudo)来登录而不使用密码:

# service mysql stop
# mysqld_safe --skip-grant-tables &
$ mysql -u root

mysql> use mysql;
mysql> update user set authentication_string=PASSWORD("YOUR-NEW-ROOT-PASSWORD") where User='root';
mysql> flush privileges;
mysql> quit

# service mysql stop
# service mysql start
$ mysql -u root -p

现在,您应该可以使用新密码以root用户身份登录。

也可以在重设密码的用户的/home/$USER/.mysql_history/root/.mysql_history中找到重设密码的查询,但是以上操作始终有效。

注意:在MySQL 5.7之前,该列称为password而不是authentication_string。将上面的行替换为

mysql> update user set password=PASSWORD("YOUR-NEW-ROOT-PASSWORD") where User='root';

09-25 18:42
查看更多