Home  >  Article  >  Database  >  How to solve the problem of forgetting root in linux mysql

How to solve the problem of forgetting root in linux mysql

PHPz
PHPzOriginal
2023-04-21 14:22:091198browse

When using Linux systems and MySQL databases, we will inevitably encounter various problems, such as forgetting the password of the root account. At this time, some people may feel confused and helpless, but we can use some methods to reset the password of the root account. This article will introduce readers to how to use the MySQL database on Linux after forgetting the root account password.

  1. Use the sudo command to obtain root permissions

You need to have root permissions to operate MySQL on Linux. If you have forgotten the original root account password, you can use the sudo command to obtain root permissions. permissions and reset the new root password.

The steps are as follows:

  1. Open the terminal and enter the following command to obtain root permissions:
sudo su
  1. Enter the current user’s password and confirm identity
  2. Enter MySQL:
mysql -u root
  1. Reset root password:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassword';

Among them, NewPassword is the new password and can be modified according to the actual situation.

  1. Exit MySQL:
quit;
  1. Use the --skip-grant-tables option

If you cannot obtain root using the above method permissions, you can use MySQL's --skip-grant-tables option to reset the root password.

The steps are as follows:

  1. Close MySQL:
sudo systemctl stop mysql
  1. Start MySQL as administrator:
mysqld_safe --skip-grant-tables &
  1. Enter MySQL:
mysql -u root
  1. Switch to MySQL database:
use mysql;
  1. Update root account password:
UPDATE user SET authentication_string=PASSWORD('NewPassword') WHERE User='root';

Among them, NewPassword is the new password, which can be modified according to the actual situation.

  1. Refresh update cache:
FLUSH PRIVILEGES;
  1. Exit MySQL:
quit;
  1. Restart MySQL:
sudo systemctl start mysql

This method will turn off the permission verification of the root account, so after using this method, you must restore the permission verification in time to avoid security risks.

  1. Modify the configuration file

If none of the above methods can solve the problem, you can also try to modify the MySQL configuration file to reset the root password.

The steps are as follows:

  1. Edit the MySQL configuration file:
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
  1. Add the following statement in the [mysqld] section:
skip-grant-tables
  1. Save and exit.
  2. Restart MySQL:
sudo systemctl restart mysql
  1. Enter MySQL:
mysql -u root
  1. Update root password:
UPDATE mysql.user SET authentication_string=PASSWORD('NewPassword') WHERE User='root';

Among them, NewPassword is the new password, which can be modified according to the actual situation.

  1. Refresh the cache:
FLUSH PRIVILEGES;
  1. Modify the MySQL configuration file:
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
  1. Delete the --skip added before -grant-tables statement.
  2. Save and exit.
  3. Restart MySQL:
sudo systemctl restart mysql

Summary

Forgetting the root password is a common problem that will inevitably be encountered when using Linux and MySQL. This article introduces three methods to reset the root password. Among them, using the sudo command and the --skip-grant-tables option is the most common method, but you need to pay attention to restoring permission verification in time after use. If none of the above methods solve the problem, you need to consider other solutions, such as reinstalling MySQL, etc. But no matter which method is used, you should operate it with caution and follow operating specifications to ensure data security.

The above is the detailed content of How to solve the problem of forgetting root in linux mysql. 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