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

How to solve the problem of forgetting root in linux mysql

PHPz
Release: 2023-04-21 14:25:15
Original
1226 people have browsed it

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
Copy after login
  1. Enter the current user’s password and confirm identity
  2. Enter MySQL:
mysql -u root
Copy after login
Copy after login
Copy after login
  1. Reset root password:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassword';
Copy after login

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

  1. Exit MySQL:
quit;
Copy after login
Copy after login
  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
Copy after login
  1. Start MySQL as administrator:
mysqld_safe --skip-grant-tables &
Copy after login
  1. Enter MySQL:
mysql -u root
Copy after login
Copy after login
Copy after login
  1. Switch to MySQL database:
use mysql;
Copy after login
  1. Update root account password:
UPDATE user SET authentication_string=PASSWORD('NewPassword') WHERE User='root';
Copy after login

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

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

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
Copy after login
Copy after login
  1. Add the following statement in the [mysqld] section:
skip-grant-tables
Copy after login
  1. Save and exit.
  2. Restart MySQL:
sudo systemctl restart mysql
Copy after login
Copy after login
  1. Enter MySQL:
mysql -u root
Copy after login
Copy after login
Copy after login
  1. Update root password:
UPDATE mysql.user SET authentication_string=PASSWORD('NewPassword') WHERE User='root';
Copy after login

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

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

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!

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