Recently, MySQL installed on Mac is not used for a period of time. When I tried to use it, I found that I forgot my password. Fortunately, google soon found a solution. At that time, no relevant records were made. Today, we encountered the same problem on a MySQL server in the company. However, this solution was not as smooth as last time, so we made a record.
To reset the password of MySQL, follow these steps ¶
Add the following lines in the [mysqld] configuration section of my. cnf:
> Skip-grant-tables
You can also add-skip-grant-tables in the startup parameter background.
After restarting MySQL, run the following command to log on:
> Mysql-u root
In this way, you can enter MySQL, and then use the following statement to change the root password.
Mysql> use mysql;
Mysql> update user set password = PASSWORD ("NEW-ROOT-PASSWORD") where User = 'root ';
Mysql> flush privileges;
Mysql> quit
Normally, the above steps are OK. However, if MySQL is not started with port 3306, the above method will not work. You must change the port to 3306 and then follow the above method. Finally, you should also note that you should remove the skip-grant-tables option after the modification is complete, so as not to let the MySQL Server Always run without the Password Logon method.
Solution for Windows/Linux MySQL to forget the root password
How to solve the problem of forgetting the root password of MySQL
Change the root password for MySQL
How to reset the root password of the MySQL server in Ubuntu
Restore the MySQL root Password under Red Hat