mysql5.7 forgot root password modification under linux

My friend started to learn server recently. I forgot my mysql password and I can't get it. Let me help you solve it. Resetting or modifying the root password of mysql is rarely done, and I still have to google it for assistance, so I finished writing a blog to record it, so that someone will encounter this kind of problem after a few months. one time.

 

In the root user password reset, the difference between mysql5.7 and 5.6 is that the fields in the user table are different. In the previous version, the field name of the password field was password, and in version 5.7, it was changed to authentication_string

 

The process is as follows:

1. Close the mysql service

service mysql stop

 Or directly kill the mysql related process

killall -TERM mysqld 

 

2. Start MySQL with the following command without checking permissions; 
  

mysqld_safe --skip-grant-tables &
(You need to enter the bin directory of mysql first, usually cd /usr/local/mysql/bin )

  

3. Then use the root user to log in to MySQL with an empty password; 
  

mysql -u root

 
4. Modify the password of the root user; 

 MySQL>update user set authentication_string = password('root'), password_expired = 'N', password_last_changed = now() where User = 'root';

 MySQL> flush privileges;  
 MySQL> quit 

 (Remember that the statement is case-sensitive. In previous versions, the field name of the password field was password, and in version 5.7, it was changed to authentication_string)

Restart MySQL and you will be able to log in with the new password.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325303975&siteId=291194637