One of the most embarrassing things you can do after installing MySQL or using MySQL is forget your password. Murphy’s Law also tells us that if something can go wrong, it will. What if you accidentally forget your MySQL password? Don’t worry. Here’s how to do it.

1. Modify the MySQL configuration file

First, we need to open the MySQL configuration file. On Windows, the MySQL configuration file is my.ini, which is in the default MySQL installation directory. For MacOS, the configuration file name is my.cnf and its path is /etc/my.cnf.

In the case of MacOS, we needAdd “skip-grant-tables” after [mysqld] to the configuration file, meaning skip permission validation, as shown in the figure below:

MacOS can directly run the sudo vim /etc/my. CNF command to edit the configuration file.

Matters needing attention

If you can’t find the MySQL configuration file in MacOS, you can create my.cnf in /etc directory and add the following contents:

[client] default-character-set=utf8

[mysqld] bind-address = 127.0.0.1 character-set-server=utf8 skip-grant-tables

Of course, you can also modify other configuration items.

2. Restart the MySQL

After modifying the configuration file, we need to restart MySQL service for the configuration to take effect.

For Windows, you can run the following command to stop MySQL and start it:

net stop mysql net start mysql

In Linux, you can run the following command to restart the system:

service mysql restart

If the Mac system is used, you can restart it on the interface, as shown in the following figure:

3. Set a new password

After the service is restarted, we can use the command line tool to set the new password for MySQLMysql -u root -p mysql -u root -p, as shown in the figure below:

Now we can use the following command to set the new password:

Update user set password=password(‘ new password ‘) where user=’root’; flush privileges; quit

Matters needing attention

If ERROR 1290 (HY000) occurs while performing this step: The MySQL server is running with The –skip-grant-tables option so it cannot execute this statement error. You need to run the Flush PRIVILEGES command before running other commands.

The last

After setting the new password, remove the “skip-grant-tables” from the MySQL configuration file and restart the MySQL service.

Bonus: search the public account “Java Chinese Community” and send “interview” to receive the latest interview materials.