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.
- 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.
- 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.
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.