This article describes how to solve the problem of forgetting the mysql password in linux. If you have encountered such a problem, you can look at the password retrieval method.
This article describes how to solve the problem of forgetting the mysql password in linux. If you have encountered such a problem, you can look at the password retrieval method.
If you have not forgotten the password, you can change the mysql password by using the following methods:
UPDATE user Statement (back to directory)
In this way, you must first log on to mysql with the root account and then execute:
1. If you have not forgotten the password, you can use UPDATE to directly edit the user table to change the password: Enter
The Code is as follows: |
|
Mysql-u rootmysql> use mysql; Mysql> UPDATE user SET Pass = PASSWORD ('newpass') WHERE user = 'root '; Mysql> flush privileges; |
Forgot password
When using the skip-grant-tables parameter, you must add the skip-networking parameter:
The Code is as follows: |
|
Shell> mysqld_safe -- skip-grant-tables -- skip-networking & |
After using SQL to reset the password, remember to remove skip-networking and restart the MySQL service in a normal way:
The Code is as follows: |
|
Shell>/etc/init. d/mysqld restart |
The above method needs to restart the service twice. In fact, it is more elegant. Restart the service once:
First, save the SQL statement used to a text file (/path/to/init/file ):
The Code is as follows: |
|
UPDATE 'mysql '. 'user' SET 'Password' = Password ('yourpassword') WHERE 'user' = 'root' AND 'host' = '2017. 0.0.1 '; flush privileges; |
Then, use the init-file parameter to start the MySQL service,
The Code is as follows: |
|
Shell>/etc/init. d/mysql stop shell> mysqld_safe -- init-file =/path/to/init/file & |
At this point, the password has been reset, and finally do not forget to delete the file content, so as not to leak the password.
If you cannot retrieve the password from the above method, refer to the following method.
If you have forgotten your password:
The Code is as follows: |
|
#/Etc/init. d/mysql stop 1. End the currently running mysql process. #/Usr/bin/mysqld_safe -- skip-grant-tables 2. Run in mysql security mode and skip permission verification. # Mysql-u root 3. restart a terminal to log on to mysql as the root user. Mysql> use mysql; |
4. Modify the root user password.
The Code is as follows: |
|
Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with- Database changed Mysql> update user set Password = PASSWORD ('root') where User = 'root '; Query OK, 3 rows affected (0.00 sec) Rows matched: 3 Changed: 3 Warnings: 0 Mysql> exit #/Etc/init. d/mysql restart 5. End mysql security mode and run mysql in normal mode. Mysql> update mysql. user set password = PASSWORD ('new password') where User = 'root '; |
6. Try your new password.
The Code is as follows: |
|
Mysql> flush privileges; Mysql> quit |