How to reset root password in MySQL 8

English 简体中文 繁体中文 ภาษาไทย Tiếng Việt
Summary

Resetting the MySQL root password, particularly in MySQL 8, requires bypassing authentication to update the user table. While MySQL 5 allowed a straightforward approach using the --skip-grant-tables option, MySQL 8 necessitates a different strategy. The recommended method involves stopping the MySQL service, creating an initialization file with an ALTER USER SQL command to set the new password, and then restarting MySQL with the --init-file option pointing to this script. Alternatively, for MySQL 8, one can start the service using --skip-grant-tables along with --console and --shared-memory, then log in as root with an empty password to execute an UPDATE statement on the mysql.user table.

在MySQL中,用户密码存储在 user 表中,重置密码实际上是更改此表中记录的值。要在忘记密码的情况下更改密码,其思路是绕过MySQL的身份验证并进入系统,然后使用SQL命令更新记录的密码值。

在MySQL 5中,可以使用 --skip-grant-tables 选项启动MySQL服务,此选项会告诉服务在启动时跳过加载授权表,因此root用户可以使用空密码登录。

mysqld –skip-grant-tables

 登录后,可以运行以下SQL命令来更改密码

UPDATE user SET authentication_string='' WHERE user='root';

这将把密码设置为空。

但是,在MySQL 8中,如果没有进行一些调整,--skip-grant-tables 似乎无法正常工作。那么我们该怎么办?有两种可能的选择。

  1. 创建一个初始化文件,并使用 --init-file 选项运行MySQL服务。在初始化文件中,放入用于更新密码值的SQL命令。
  2. 深入研究如何在MySQL 8中使用 --skip-grant-tables。

让我们看看这些选项是如何工作的。

选项 1:--init-file 选项

此选项将指定一个包含SQL命令的文件,该命令将在服务启动之前执行。因此,我们只需要将更新密码的命令放入此文件中,并使用此选项启动MySQL服务。密码将被重置/更新。

步骤 1:停止MySQL服务

net stop mysql

步骤 2:创建一个txt文件,并将以下命令放入其中

ALTER USER 'root'@'localhost' IDENTIFIED BY '';

步骤 3:在命令行上使用 --init-file 选项启动服务

mysqld --init-file=/some/path/to/cmd.txt --console

完成。注意:当您遇到权限被拒绝的错误时,请以特权用户身份运行该命令。如果您看到如下错误:

2018-12-25T02:51:23.739089Z 0 [System] [MY-010116] [Server] C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe (mysqld 8.0.13) starting as process 1912
2018-12-25T02:51:23.759426Z 1 [ERROR] [MY-011011] [Server] Failed to find valid data directory.
2018-12-25T02:51:23.761196Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed.
2018-12-25T02:51:23.762550Z 0 [ERROR] [MY-010119] [Server] Aborting
2018-12-25T02:51:23.766230Z 0 [System] [MY-010910] [Server] C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe: Shutdown complete (mysqld 8.0.13)  MySQL Community Server - GPL.

请运行以下命令来初始化数据目录

mysqld --initialize --console

选项 2:使用 --skip-grant-tables 选项

与MySQL 5相比,在MySQL 8中需要添加更多选项。

mysqld --console --skip-grant-tables --shared-memory

启动服务后,使用空密码登录

mysql -u root

然后执行SQL命令来更新密码

UPDATE mysql.user SET authentication_string='' WHERE user='root' and host='localhost';

通常建议使用选项 1。希望这些能帮到您。

MYSQL PASSWORD MYSQL 8

  RELATED

  COMMENTS

5
Anonymous
Feb 7, 2019 at 2:38 am

2019-02-07T08:33:35.790267Z 0 [System] [MY-010116] [Server] C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe (mysqld 8.0.14) starting as process 9932
2019-02-07T08:33:35.793300Z 0 [Warning] [MY-010091] [Server] Can't create test file C:\Program Files\MySQL\MySQL Server 8.0\data\DESKTOP-7CTR7J5.lower-test
2019-02-07T08:33:35.793361Z 0 [Warning] [MY-010091] [Server] Can't create test file C:\Program Files\MySQL\MySQL Server 8.0\data\DESKTOP-7CTR7J5.lower-test
2019-02-07T08:33:35.793534Z 0 [ERROR] [MY-013276] [Server] Failed to set datadir to 'C:\Program Files\MySQL\MySQL Server 8.0\data\' (OS errno: 2 - No such file or directory)
2019-02-07T08:33:35.807438Z 0 [ERROR] [MY-010119] [Server] Aborting
2019-02-07T08:33:35.807626Z 0 [System] [MY-010910] [Server] C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe: Shutdown complete (mysqld 8.0.14)  MySQL Community Server - GPL.

Hi! Thanks for the guide. I am getting this error tho.  Grateful for your help.

Ke Pi
Feb 7, 2019 at 10:09 am

Can you create 'C:\Program Files\MySQL\MySQL Server 8.0\data directory and try again? 

jamil
Apr 12, 2019 at 8:18 pm

I tried the first option. Now my old password AND new password both don't work... Worst comes to worst. I'm just going to delete mysql, and re-download it...

Nikhil Karanjkar
Oct 12, 2019 at 10:45 am

This saved my day. Thank you

Anonymous
Oct 24, 2019 at 2:08 pm

Tested directly Option2 and it worked.