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.
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.
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:
(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.
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!