Home > Database > Mysql Tutorial > How to solve the problem of forgetting mysql root password

How to solve the problem of forgetting mysql root password

PHPz
Release: 2023-04-17 17:18:39
Original
8813 people have browsed it

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:

  1. Open the terminal and enter MySQL:
sudo mysql
Copy after login
  1. Enter the password change mode in MySQL:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';
Copy after login

Note that "newpassword" here is your new password. Make sure to replace it with the actual password you wish to set.

  1. Exit MySQL:
quit
Copy after login

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:

  1. Make sure you have stopped the MySQL service:
sudo service mysql stop
Copy after login
  1. Use mysqld_safe Start the MySQL service:
sudo /usr/bin/mysqld_safe --skip-grant-tables &
Copy after login
  1. Connect to MySQL:
sudo mysql -uroot
Copy after login
  1. Change password:
use mysql;
update user set authentication_string=PASSWORD("newpassword") where User='root';
flush privileges;
Copy after login

Note that this "newpassword" is your new password. Make sure to replace it with the actual password you wish to set.

  1. Close the MySQL terminal and mysqld_safe:
quit;
sudo service mysql stop
Copy after login

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:

  1. Delete MySQL:
sudo apt-get remove --purge mysql-server mysql-client mysql-common
Copy after login
  1. Delete the MySQL configuration file:
sudo rm -rf /etc/mysql /var/lib/mysql
Copy after login
  1. Download and install MySQL:
sudo apt-get install mysql-server
Copy after login

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!

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