如何在MySQL 8.0.11中重置root密码?

Man*_*iff 14 mysql passwords root

我实际上丢失了我的root密码,我需要更改它.我按照以下步骤操作:

  • 步骤#1:停止MySQL服务器进程.

    步骤#2:使用--skip-grant-tables选项启动MySQL(mysqld)服务器/守护程序进程,以便它不会提示输入密码.

    步骤#3:以root用户身份连接MySQL服务器.

我们可以在这些网站上找到:https://www.howtoforge.com/setting-changing-resetting-mysql-root-passwords#recover-mysql-root-password

mysql> use mysql;
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD("TOOR");
mysql> flush privileges;
mysql> quit
Run Code Online (Sandbox Code Playgroud)

第一个错误,所以我试过:

mysql> use mysql;
mysql> update user set password=PASSWORD("TOOR") where User='root';
mysql> flush privileges;
mysql> quit
Run Code Online (Sandbox Code Playgroud)

总是同样的错误说:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '("TOO
R") WHERE User='root'' at line 1
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

小智 28

因为在这里说:

在MySQL 8.0.11中删除了此功能

1.如果你
在mysqld_safe 中的skip-grant-tables模式:

UPDATE mysql.user SET authentication_string=null WHERE User='root';
FLUSH PRIVILEGES;
exit;
Run Code Online (Sandbox Code Playgroud)

然后,在终端:

mysql -u root
Run Code Online (Sandbox Code Playgroud)

在mysql中:

ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';
Run Code Online (Sandbox Code Playgroud)

2.不在mysql中的skip-grant-tables模式
:

ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';
Run Code Online (Sandbox Code Playgroud)

  • 我必须使用 `mysql_native_password` 而不是 `caching_sha2_password` 才能正常工作:`ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'yourpasswd';`。但不知道为什么。 (5认同)
  • 对于在 SKIP-GRANT-TABLES 模式下 - sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables 。适用于 8.0.1 (2认同)
  • 我在命令顺序上遇到了麻烦,但在 ubuntu 20 中对我有用:“sudo service mysql stop”“sudo mysqld_safe --skip-grant-tables &”,然后用“mysql -u root”“UPDATE mysql.txt”打开 mysql 控制台。用户 SETauthentication_string=null WHERE User='root'; 刷新权限;更改由 'yourpasswd' 使用 caching_sha2_password 标识的用户 'root'@'localhost';退出;``ps -fea | grep mysqld``sudo Kill -9 [pid from previous command]`然后最后`sudo service mysql stop` (2认同)

小智 22

用sudo登录mysql

Sudo mysql
Run Code Online (Sandbox Code Playgroud)

然后

ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';
exit;
Run Code Online (Sandbox Code Playgroud)

测试一下

mysql -u root -p
Run Code Online (Sandbox Code Playgroud)

  • 请注意,使用此密码算法会破坏 Java 应用程序,并出现错误“不允许公钥检索”。最好使用良好的本机密码算法 ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password; 或者您可能被迫更改应用程序设置 (2认同)

小智 6

尝试这个:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'newPasswd';
Run Code Online (Sandbox Code Playgroud)

  • 在添加skip-grant-tables之前以及用户收到以下消息之后,此语句不可用: ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it can not run this statements (2认同)

Pis*_*iya 6

如果您使用的是 Windows,您可以尝试以下步骤

在 Windows 中重置 MySQL 8.0 root 密码

  1. MySQL 8.0从服务中停止服务
  2. 转到路径C:\Program Files\MySQL\MySQL Server 8.0\bin并打开cmd
  3. 跑步mysqld --console --skip-grant-tables --shared-memory
  4. cmd在同一路径下打开新的
  5. 运行以下命令
  6. mysql -u root
  7. select authentication_string,host from mysql.user where user='root';
  8. UPDATE mysql.user SET authentication_string='' WHERE user='root';
  9. 现在关闭两个cmd.
  10. 尝试启动MySQL 8.0服务。
  11. 使用用户名root和密码空白进行连接。
  12. 从用户管理中更改密码。

在https://gist.github.com/pishangujeniya/0f839d11a7e692dadc49821c274a2394找到这个