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
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/
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
Find the following configuration:
[mysqld] skip-grant-tables
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
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
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';
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;
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
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!