Home > Database > Mysql Tutorial > body text

What should I do if I forget my Linux Mysql password?

PHPz
Release: 2023-04-21 14:09:19
Original
871 people have browsed it

When using Linux system and Mysql database, password forgetting often occurs. What should we do at this time?

Step one: Stop the Mysql service

The Mysql service runs as a process in the Linux system. We need to stop this process for subsequent operations.
You can use the following command to stop the Mysql service:

sudo service mysql stop
Copy after login

Step 2: Edit the Mysql configuration file

Next, we need to enter the Mysql configuration file and modify some parameters so that Mysql can Start without password. Open the terminal and use the following command to enter the directory where the Mysql configuration file is located:

cd /etc/mysql/
Copy after login

In this directory, find and edit the Mysql configuration file my.cnf. You can use an editor such as VI or nano to edit. Open the command line and enter the following command:

sudo nano my.cnf
Copy after login

Find the following configuration:

[mysqld]
skip-grant-tables
Copy after login

Set skip-grant-tables to 1 or comment out this line content, then save and exit the editor.

Step 3: Start the Mysql service

Next, we need to restart the Mysql service. Enter the following command in the terminal:

sudo service mysql start
Copy after login

The Mysql service will be started without a password.

Step 4: Log in to Mysql

Now we can use the Mysql command line to log in to the Mysql server. Enter the following command:

mysql
Copy after login

At this time we will log in to the Mysql server as an anonymous user, and then use the following command to change the password:

use mysql;
update user set authentication_string='password' where user='root';
Copy after login

The password here refers to the new password, root Indicates the username to be modified. After executing the above command, execute the following command to refresh the permissions:

flush privileges;
Copy after login

Step 5: Restart the Mysql service

Next, we need to exit the Mysql command line and restart the Mysql service. Enter the following command in the terminal:

sudo service mysql restart
Copy after login

The Mysql service will restart with the newly set password.

Final summary

As can be seen from the above steps, the method for forgetting Mysql password is actually very simple. You only need to stop the Mysql service, edit the Mysql configuration file, restart the Mysql service, log in to Mysql and change the password, and finally restart the Mysql service.

The above is the detailed content of What should I do if I forget my Linux Mysql 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