Home  >  Article  >  Database  >  centos mysql password forgotten

centos mysql password forgotten

PHPz
PHPzOriginal
2023-05-23 09:45:072747browse

In the process of building a MySQL database using CentOS server, we will inevitably encounter the situation of forgetting the password. This situation is very troublesome, but don't be nervous. Below I will introduce you to a solution in detail so that you can successfully recover your MySQL password.

First, open the terminal on the CentOS server and enter the following command in the terminal to log in to MySQL using the root user:

mysql -u root -p

Next, a prompt asking for a password will appear, but because We forgot our password so we can't enter the correct password. At this time, we need to operate as follows:

  1. Stop the MySQL service

Enter the following command to stop the MySQL service:

systemctl stop mysqld
  1. Start MySQL using the --skip-grant-tables option

After stopping the MySQL service, we need to start MySQL using the --skip-grant-tables option so that we can bypass the authentication that requires a password Steps to enter MySQL directly.

Enter the following command:

mysqld_safe --skip-grant-tables &

Note that the & symbol is used here to make the MySQL process run in the background so that we can perform subsequent operations in the terminal.

  1. Enter MySQL

After starting MySQL using the --skip-grant-tables option, we need to enter MySQL again. Enter the following command:

mysql -u root

At this time, we can successfully enter the MySQL database.

  1. Change password

After entering MySQL, use the following command to change the password:

UPDATE mysql.user SET authentication_string=PASSWORD("your_new_password") WHERE User='root';

In this command, "your_new_password" is what you want to set New Password.

  1. Refresh permissions

After successfully changing the password, we also need to refresh the permissions using the following command:

FLUSH PRIVILEGES;
  1. Exit MySQL

After completing all the above steps, we need to exit MySQL. Enter the following command:

EXIT;
  1. Restart the MySQL service

Finally, we need to restart the MySQL service and enter the following command:

systemctl start mysqld

In this way, we This completes the MySQL password recovery. From now on, we can use the new password to log in to MySQL.

Summary

If you encounter a situation where the MySQL password is forgotten on the CentOS server, you can use the --skip-grant-tables option to bypass the password authentication step to achieve password recovery. However, before performing the above operation, we need to confirm the MySQL version installed on the server, network environment and other information to ensure the success of the operation. I hope this article can help everyone and you can easily solve similar problems when you encounter them!

The above is the detailed content of centos mysql password forgotten. 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