Home  >  Article  >  Database  >  mysql forgot root password

mysql forgot root password

王林
王林Original
2023-05-12 11:01:36441browse

MySQL is a widely used relational database management system. Due to its widespread use, MySQL has become one of the essential tools for every developer's daily work. When using MySQL, sometimes we may encounter the situation of forgetting the root password. This is a common question, but one that might bother you at the time. Therefore, this article will explain how to solve the problem of forgetting MySQL root password.

  1. Use the rescue program officially provided by MySQL

MySQL officially provides a rescue program to solve the problem of forgotten passwords. This program is commonly called mysqld --skip-grant-tables. Here are the steps to reset your password using this program:

(1) Stop the MySQL service:

$ sudo service mysql stop

(2) Start by skipping the authorization table MySQL service:

$ sudo mysqld --skip-grant-tables &

(3) Connect to the MySQL server and use the following command to change the password:

$ mysql -u root mysql

UPDATE user SET authentication_string=password('new_password') WHERE User='root';
FLUSH PRIVILEGES;
quit

(4) Restart the MySQL service:

$ sudo service mysql restart

Now you can log in to MySQL using the new root password.

  1. Using the dpkg-reconfigure command

The dpkg-reconfigure command is a command in Debian and Ubuntu Linux. This command reconfigures the MySQL server and allows you to set a new password. The following are the steps to reset the password using the dpkg-reconfigure command:

(1) Stop the MySQL service:

$ sudo service mysql stop

(2) Use dpkg-reconfigure Command to reconfigure MySQL:

$ sudo dpkg-reconfigure mysql-server-5.7

(3) Now, you will see a MySQL configuration wizard:

  • Choose to use a blank password
  • and then enter the new password twice

(4) After setting the new password, restart the MySQL service:

$ sudo service mysql restart

Now you can log in to MySQL using the new password.

  1. Modify the MySQL configuration file

If the above two methods cannot solve the problem, you can modify the MySQL configuration file and reset the password. The following are the steps to use this method:

(1) Stop the MySQL service:

$ sudo service mysql stop

(2) Edit the MySQL configuration file /etc/mysql /mysql.conf.d/mysqld.cnf:

$ sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

(3) At the end of the [mysqld] section Add the following code in one line:

skip-grant-tables

(4) Save and close the file.

(5) Restart the MySQL service:

$ sudo service mysql start

(6) Now connect to MySQL and use the following command to change the password:

$ mysql -u root mysql

UPDATE user SET authentication_string=password('new_password') WHERE User='root';
FLUSH PRIVILEGES;
quit

(7)Delete Modified configuration file /etc/mysql/mysql.conf.d/mysqld.cnf and restart the MySQL service:

$ sudo rm /etc/mysql/mysql.conf.d/mysqld.cnf
$ sudo service mysql restart

These three methods can solve the problem of forgetting the MySQL root password. You can choose a method based on your needs and use one of these methods to reset the MySQL root password.

The above is the detailed content of mysql forgot root password. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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