When taking over a project, it is a headache if the previous person in charge has not sorted out the project documents, account numbers and passwords. For example, when you want to open the MySQL database type:

Mysql -u root -p mysql -u root -p

# [MySQL for Linux ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)](https://www.cnblogs.com/gumuzi/p/5711495.html)

Immediately have a kind of want to hit the keyboard impulse ~ but don’t panic, programmer’s most powerful place is, the Internet to find a solution ha ha;

MySQL to forget password

1, findmy.cnfLocation of the configuration file

For Windows, modify my.ini. Here is the specific operation of Linux.

Linux systems that use Whereis can easily find the location of a file:

$ whereis my
$ my:/etc/my.cnf

Copy the code
2, modify my.cnf

If you are the root user, go to vim /etc/my.cnf.

If you are a regular user, sudo vim /etc/my.cnf

Enter vim editor to edit, find [mysqld], press I on keyboard to enter edit mode, add skip-grant-tables on any line after [mysqld] to skip password verification:

 

Press ESC to exit the editing, type :wq, and press Enter to save and exit.

3. Restart the mysqld service
$ sudo /etc/init.d/mysqld restart 

Copy the code

For ordinary users, use sudo at the top of the command, not root.

4, no secret access to MySQL
Copy the code

Entering MySQL database when Entering Entering password.

5. Change the root password

MySQL > select root from ‘user’ where user = ‘user’

mysql > use mysql;
mysql > update user set password=password('YOUR_NEW_PASSWORD') where user='root'; 
mysql > flush privileges;

Copy the code

Step analysis:

use mysql; Enter the database named “mysql”,

Then use the UPDATE statement to update the password,

Use Flush PRIVILEGES after updating passwords; Refresh permission.

Note: the password() function is used for password storage encryption function, the encryption process is irreversible! So unless you compare a lot of data, there’s no way to use an inverse function to see the old password, only to update it, which YOUR_NEW_PASSWORD is the new password you set yourself.

After setting the new password, press Ctrl + D to exit the MySQL database.

6. Reconfiguremy.cnffile

Open my.cnf again with vim, delete skip-grant-tables or comment # in front of it, save and exit.

Restart the mysqld service
$ sudo /etc/init.d/mysqld restart 

Copy the code
8. Log in to the MySQL database with the new password
$ mysql -u root -p

Copy the code