Home >Database >Mysql Tutorial >What should I do if I forget mysql root password?
MySQL is a very popular relational database that is widely used in the fields of web development and data management. However, if you use MySQL frequently, you may encounter some problems, one of which is forgetting the password of the root user. In this article, we will introduce several ways to solve this problem.
Method 1: Use the mysqladmin tool
Mysqladmin is a command line tool provided by MySQL, which can help us manage MySQL services. If you forget the password of the root user, you can reset it through the mysqladmin tool. Please follow the steps below:
mysqladmin -u root password 'newpassword'
Where, newpassword is the new password you want to set. If executed successfully, the message "mysqladmin: Password set successfully" will be returned.
mysql -u root -p
Enter the new password in the pop-up password prompt box and press Enter to log in to MySQL.
Method 2: Use the mysqld_safe command
If you cannot use the mysqladmin tool, you can try to use the mysqld_safe command to start the MySQL service and reset the root user's password. Please follow the steps below:
sudo systemctl stop mysql
sudo mysqld_safe --skip-grant-tables &
mysql -u root
USE mysql; UPDATE user SET authentication_string=password('newpassword') WHERE User='root'; FLUSH PRIVILEGES;
Among them, newpassword is the new password you want to set.
QUIT; sudo systemctl stop mysql
sudo systemctl start mysql
Now, you can use the new Password to log in to the MySQL service.
Method 3: Use the /etc/mysql/debian.cnf file
If MySQL is installed on the Debian operating system, you can use the /etc/mysql/debian.cnf file to reset root The user's password. Please follow these steps:
user = debian-sys-maint password = YOUR_PASSWORD
Where YOUR_PASSWORD is the password of the root user.
mysql -u debian-sys-maint -p
Enter YOUR_PASSWORD in the pop-up password prompt box and press Enter to log in to MySQL.
USE mysql; UPDATE user SET password=password('newpassword') WHERE User='root'; FLUSH PRIVILEGES;
Where, newpassword is the new password you want to set.
QUIT;
Now, you can use the new password to log in to the MySQL service.
Summary
MySQL Forgetting the password of the root user is a common problem, but it is not difficult to solve. This article describes three methods to reset the root user's password: using the mysqladmin tool, using the mysqld_safe command, and using the /etc/mysql/debian.cnf file. No matter which method you use, you need to ensure that you have sufficient permissions to operate the MySQL service.
The above is the detailed content of What should I do if I forget mysql root password?. For more information, please follow other related articles on the PHP Chinese website!