Home  >  Article  >  Database  >  Mysql 5.7 What to do if you forget your password

Mysql 5.7 What to do if you forget your password

PHPz
PHPzOriginal
2023-04-17 16:36:123330browse

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.

  1. Reset the password by modifying the MySQL configuration file.

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.

  1. Reset MySQL password by using safe mode.

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

  1. Use the MySQL security tool to reset the password.

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:

  • Enter the root password or press Enter to skip this step.
  • Enter y and press Enter to delete anonymous users.
  • Enter y and press Enter to disable root remote login.
  • Enter y and press Enter to delete the test database and let MySQL load the new privileged table.
  • Enter y and press Enter to complete the process.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn