How to Reset the MySQL Database's Root Password

08-01-2024 02:12:11

If you've forgotten the root password for your MySQL database, you can reset it by following these steps, which are divided into scenarios for Windows and Linux operating systems.

Windows Operating System:

Open the MySQL configuration file, my.ini, with Notepad. Add the following line under the [mysqld] section.

[mysqld]
skip-grant-tables

Open the "Control Panel => System and Security => Administrative Tools => Services". Find MySQL in the list and restart this service.

Open "Start => Run", type cmd to enter the command line interface, and then enter the following command to reset the root password to '111111' (six ones), or modify as needed.

mysql
> use mysql;
> update user set Password=Password('111111') where User='root';(MySQL5.1、5.6版本)
> update mysql.user set authentication_string = password('111111') where user='root';(MySQL5.7版本)
> flush privileges;
> exit;

Finally, open the my.ini file, remove the skip-grant-tables line that was added under the [mysqld] section earlier, and then restart MySQL.

Linux Operating System:

Stop MySQL and enable password-free login.

service mysql stop
mysqld_safe --skip-grant-tables &

After stopping MySQL, use the following command to connect to the database.

mysql -u root mysql

Enter the following command to reset the root password to '111111' (six ones), or modify as needed.

UPDATE user SET password=PASSWORD('111111') WHERE user='root';
flush privileges;
exit;

Finally, restart MySQL.

service mysql restart