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.
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:
sudo su
mysql -u root
ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassword';
Among them, NewPassword is the new password and can be modified according to the actual situation.
quit;
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:
sudo systemctl stop mysql
mysqld_safe --skip-grant-tables &
mysql -u root
use mysql;
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.
FLUSH PRIVILEGES;
quit;
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.
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:
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
skip-grant-tables
sudo systemctl restart mysql
mysql -u root
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.
FLUSH PRIVILEGES;
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
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!