When using the MySQL database in a Linux system, you may encounter a situation where you forget your password. At this time we can solve this problem through the following methods:
Method 1: Use the mysqladmin command to reset the password
Enter the following command in the Linux terminal to stop the MySQL service
sudo /etc/init.d/mysql stop
Then restart the MySQL service and disable permission checks at startup
sudo mysqld_safe --skip-grant-tables &
Use the mysqladmin command to update the password of the root user
mysqladmin -u root password newpassword
Note: Replace newpassword here with the new password you want to set
After completing the password change , enter the following command in the MySQL terminal to exit
exit;
Finally, restart the MySQL service to apply the modifications
sudo /etc/init.d/mysql restart
Method 2: Use the default account during MySQL installation (only applicable if MySQL has just been installed)
If you have just installed MySQL and forgot the password of the root user, you can reset it through the following steps Password:
First, stop the MySQL service:
sudo /etc/init.d/mysql stop
Then, start the MySQL service as the root user
sudo mysqld_safe --skip-grant-tables --skip-networking &
Enter the following command in the MySQL terminal to update the root user password:
mysql -u root
UPDATE mysql.user SET authentication_string=PASSWORD('newpassword') WHERE User='root' AND Host='localhost';
FLUSH PRIVILEGES;
Note: Replace newpassword here with what you want Set the new password
Enter the following command in the MySQL terminal to exit
exit;
Finally, restart the MySQL service to apply the changes
sudo /etc /init.d/mysql restart
Method 3: Solve the problem by deleting a configuration file of MySQL
Enter the following command in the terminal to update the root user password:
sudo nano /etc/mysql/debian.cnf
Change the user and password values in the file to the new user and password you want to set
Execute the following command to log in to the MySQL terminal
mysql -u root -p
Log in with the new user and password in the MySQL terminal
Finally, restart the MySQL service
sudo /etc/init. d/mysql restart
Summary:
The above are several ways to fully solve the problem of forgotten passwords. I hope they can help you. When using any database or system, you need to be vigilant at all times to avoid dangerous situations such as forgotten passwords. When solving MySQL password problems through the above methods, be careful to avoid accidentally destroying database resources.
The above is the detailed content of linux mysql forgot password. For more information, please follow other related articles on the PHP Chinese website!