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.