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.

The user password in MySQL is stored in the user table, the password reset is actually to change the value of record in this table. To change the password in case the password is forgotten, the idea is to bypass the authentication of MySQL and get into the system and update the record password value with SQL command.

In MySQL 5, one can start MySQL service with --skip-grant-tables option, this option will tell the service to skip loading the grant tables when starting, hence the root user can login with empty password.

mysqld –skip-grant-tables

 After login, can run below SQL command to change the password

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

This will set the password as empty.

However, it seems --skip-grant-tables cannot work properly without some tuning in MySQL 8.  What can we do then? There are two possible options.

  1. Create an init file and run MySQL service with option --init-file. In init file, put the SQL command which is to update the password value.
  2. Dig deep in checking how to use --skip-grant-tables in MySQL 8.

Let's take a look at how these options work.

Option 1: --init-file option

This option will specify a file containing SQL command to be executed before the service starts. Hence, we just need to put the command to update the password in this file and start MySQL service with this option. The password will be reset/updated.

Step 1: Stop MySQL service

net stop mysql

Step 2: Create a txt file and put below command in

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

Step 3: Start the service on command line and with --init-file option

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

It's done. Note: please run the command as privileged user when you encounter error of permission denied. If you see errors like:

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.

Please run below command to initialize the data directory

mysqld --initialize --console

Option 2: Use --skip-grant-tables option

In contrast to MySQL 5, some more options need to be added in MySQL 8. 

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

After starting the service, login with an empty password

mysql -u root

Then execute SQL command to update password

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

Normally option 1 is recommended. And hope these help.

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.