As a Linux server administrator, you must have encountered the situation of forgetting the MySQL database password more than once. When you need to manage or modify the MySQL database, you suddenly find that you cannot log in, or the password you set previously no longer applies. At this time, don’t panic, this article will introduce you to several methods to retrieve the MySQL database password from the Linux system.
Method 1: Use troubleshooting mode to reset password
MySQL provides a special troubleshooting mode (Troubleshooting Mode), which can reset the MySQL root user when the password is forgotten. password.
The steps are as follows:
$ systemctl stop mysqld
$ mysqld_safe --skip-grant-tables &
$ mysql -u root
> use mysql; > update user set password=PASSWORD("new_password") where User='root'; > flush privileges; > quit
$ systemctl stop mysqld $ systemctl start mysqld
After that, try to log in to MySQL using the new password.
Method 2: Use the Linux system account to reset the password
If you used the system user when creating the MySQL user and you have remembered the user's password, then you can use this user to log in and change the MySQL user's password.
The steps are as follows:
$ mysql -u user_name -p
Among them, user_name is the MySQL user name for which you need to reset the password.
> SET PASSWORD FOR 'user_name'@'localhost' = PASSWORD('new_password');
> FLUSH PRIVILEGES;
> quit
Method 3: Change the password through the configuration file
If you know the password of the MySQL root user, then you can directly modify the configuration file and then restart the MySQL service.
The steps are as follows:
$ sudo vi /etc/my.cnf
[mysqld]
skip-grant-tables
$ sudo systemctl restart mysqld
$ mysql -u root > use mysql; > update user set password=PASSWORD("new_password") where User='root'; > flush privileges; > quit
$ sudo systemctl restart mysqld
Summary
Whether the MySQL database is inaccessible due to forgotten passwords, incorrect operations, or other reasons, don't panic, the above three methods can help you solve the problem. If you encounter a similar situation, please try to follow the above steps to retrieve the MySQL database password.
The above is the detailed content of Forgot mysql password linux. For more information, please follow other related articles on the PHP Chinese website!