When mysql5.7 forgets the password, modify the root password

(1) Since MySQL 5.7 is installed for the first time, a random password will be generated in the root directory, the file name is mysql_secret 

Therefore, you need to log in with a random password when logging in, and then change the password with the following command "SET PASSWORD = PASSWORD('new password');"

 

2). When you forget the root password, 

Take windows as an example:

 

    1. Shut down the running MySQL service. 

 

    2. Open a DOS window and go to the mysql\bin directory

 

    3. Enter mysqld --skip-grant-tables and press Enter

            --skip-grant-tables means to skip permission table authentication when starting the MySQL service. 

 

    4. Open another DOS window (because the DOS window just now cannot be moved), and go to the mysql\bin directory. 

 

    5. Type mysql and press Enter. If successful, the MySQL prompt > will appear. 

 

    6. Connect to the permission database: use mysql; (don't forget to add a semicolon at the end).

 

    7. Change the password: update user set password=password("123") where user="root"; (don't forget to add a semicolon at the end). 

 

    If the change password appears 

    **mysql change password error ERROR 1054 (42S22)**

 

    则使用 mysql>update mysql.user set authentication_string=password('123456') where user='root' and Host ='localhost';

或者 update MySQL.user set password=PASSWORD(‘123456’) where User=’root’;

 

    8. mysql>flush privileges; #Update privileges

 

    9. Exit quit. 

 

    10. Log out of the system, and then enter,

    Enter mysql -u root -p

    Log in with the username root and the new password 123456 you just set. 

 

 

(3). When you know the root password

 

Method 1: Use the SET PASSWORD command 

First log in to MySQL. 

Format: mysql> set password for username@localhost = password('new password'); 

例子:mysql> set password for root@localhost = password('123'); 

 

Method 2: Using mysqladmin 

Format: mysqladmin -u username -p old password password new password 

Example: mysqladmin -uroot -p123456 password 123 

 

Method 3: Edit the user table directly with UPDATE 

First log in to MySQL. 

mysql> use mysql; 

    mysql> update user set password=password('123') where user='root' and host='localhost'; 

mysql> flush privileges; 

 

Guess you like

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