Home > Database > Mysql Tutorial > What should I do if I forget mysql root password?

What should I do if I forget mysql root password?

PHPz
Release: 2023-04-17 17:18:00
Original
33598 people have browsed it

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:

  1. Open a terminal or command line interface and enter the MySQL installation directory.
  2. Run the following command:
mysqladmin -u root password 'newpassword'
Copy after login

Where, newpassword is the new password you want to set. If executed successfully, the message "mysqladmin: Password set successfully" will be returned.

  1. Use a new password to log in to MySQL, for example:
mysql -u root -p
Copy after login

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:

  1. Stop the MySQL service, for example:
sudo systemctl stop mysql
Copy after login
  1. Use the following command to start the MySQL service, use the skip-grant-tables option to skip Password verification:
sudo mysqld_safe --skip-grant-tables &
Copy after login
  1. Connect to the MySQL service in a new terminal or command line interface:
mysql -u root
Copy after login
  1. Execute the following SQL command to reset root User's password:
USE mysql;
UPDATE user SET authentication_string=password('newpassword') WHERE User='root';
FLUSH PRIVILEGES;
Copy after login

Among them, newpassword is the new password you want to set.

  1. Quit MySQL and stop the MySQL service:
QUIT;
sudo systemctl stop mysql
Copy after login
  1. Restart the MySQL service:
sudo systemctl start mysql
Copy after login

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:

  1. Open the /etc/mysql/debian.cnf file and look for the following line:
user = debian-sys-maint
password = YOUR_PASSWORD
Copy after login

Where YOUR_PASSWORD is the password of the root user.

  1. Use the following command to log in to MySQL:
mysql -u debian-sys-maint -p
Copy after login

Enter YOUR_PASSWORD in the pop-up password prompt box and press Enter to log in to MySQL.

  1. Execute the following SQL command to reset the password of the root user:
USE mysql;
UPDATE user SET password=password('newpassword') WHERE User='root';
FLUSH PRIVILEGES;
Copy after login

Where, newpassword is the new password you want to set.

  1. Quit MySQL:
QUIT;
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template