If you are using MySQL 5.7 and have forgotten your MySQL root password, there are some methods that can help you reset the password.
Stop the MySQL service, and then use a text editor to open the MySQL configuration file my.cnf or my.ini (the file may be located in a different location depending on your operating system). Add skip-grant-tables in the [mysqld] section then save and close the file. Restart the MySQL service. At this time, MySQL no longer requires a password to log in.
Use the following command to log in to MySQL:
$ mysql -u root
Use the following command to update the root password and exit MySQL:
mysql > ; USE mysql;
mysql > UPDATE user SET authentication_string=PASSWORD("new_password") WHERE User='root';
mysql > FLUSH PRIVILEGES;
mysql > quit;
Replace "new_password" with the new password. Now, edit the MySQL configuration file again and delete the skip-grant-tables line and restart MySQL.
First, stop the MySQL service to make sure no other clients are connecting to your database. Use the following command to start MySQL safe mode.
$ sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking &
You will see a message similar to:
[1 ] 1234
This means that the command is running in the background and displays the process ID as 1234.
Use the following command to connect to MySQL:
$ mysql -u root mysql
Next use the following command to update the root password:
mysql > USE mysql;
mysql > UPDATE user SET authentication_string=PASSWORD("new_password") WHERE User='root';
mysql > FLUSH PRIVILEGES;
Replace "new_password" with the new password. Finally, exit MySQL and stop MySQL safe mode.
mysql > quit;
$ sudo kill -9 1234
If you do not want to manually change the MySQL configuration file, you can also use the MySQL security tool (mysql_secure_installation) to reset the MySQL password.
First, install the MySQL security tool. Use the following command under the Ubuntu system:
$ sudo apt-get install mysql-server
Then run the following command:
$ sudo mysql_secure_installation
Perform the following steps:
You can now access the MySQL server using your new password.
No matter which method you use to reset your MySQL password, you need to make sure the new password is strong enough and keep it in a safe place.
The above is the detailed content of Mysql 5.7 What to do if you forget your password. For more information, please follow other related articles on the PHP Chinese website!