Home >Database >Mysql Tutorial >How to solve the problem of forgetting mysql root password
MySQL is a very popular relational database management system, but when users forget the MySQL root password, they will encounter some trouble. If you are facing this situation, don't worry. This article will introduce some effective methods to help recover MySQL root password.
Method 1: Use the command line
Generally speaking, if you already know the password of the MySQL root account, you can change the password through the command line. The following are the steps that need to be performed:
sudo mysql
ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';
Note that "newpassword" here is your new password. Make sure to replace it with the actual password you wish to set.
quit
After exiting MySQL, the password of your MySQL root account has been successfully changed.
Method 2: Use mysqld_safe
If you forget the password of the MySQL root account, you can use mysqld_safe to reset the password. mysqld_safe is part of MySQL Server that allows you to start MySQL without a password.
Here are the steps to reset the MySQL root password using mysqld_safe:
sudo service mysql stop
sudo /usr/bin/mysqld_safe --skip-grant-tables &
sudo mysql -uroot
use mysql; update user set authentication_string=PASSWORD("newpassword") where User='root'; flush privileges;
Note that this "newpassword" is your new password. Make sure to replace it with the actual password you wish to set.
quit; sudo service mysql stop
Now, you have successfully reset the password of the MySQL root account. You can log in again with your new password.
Method 3: Reinstall MySQL
If you have tried the above two methods and still cannot recover the MySQL root password, then you need to consider reinstalling MySQL.
Please note that this method will delete all MySQL databases. If you want to do this, be sure to back up your existing MySQL database first.
The following are the steps to reinstall MySQL:
sudo apt-get remove --purge mysql-server mysql-client mysql-common
sudo rm -rf /etc/mysql /var/lib/mysql
sudo apt-get install mysql-server
After reinstalling MySQL, you can log in using the default password of the root account (which is empty).
Conclusion
No matter which method you choose to recover your MySQL root password, always proceed with caution. Resetting a MySQL password may result in data loss and unrecoverable failure. Before doing anything, make sure you have backed up all your databases and make sure you fully understand what you are doing.
The above is the detailed content of How to solve the problem of forgetting mysql root password. For more information, please follow other related articles on the PHP Chinese website!