Modify the MySQL 5.7 login password

1. Background

Cannot log in to MySQL without password

2. Principle

Modify the MySQL configuration file my.cnf so that you can skip the authorization table and enter MySQL, which is equivalent to password-free login. Because all user information is stored in the user table of the mysql database, you only need to find the user's password field in the user table, and then modify the password value.

3. Change password

3.1 Enable password-free login

All Linux configuration files are placed in /etcthe directory by default. MySQL configuration files are usually named as my.cnf. When there are duplicate files in the /etc directory, they will be automatically renamed.

  • Skip authorization:skip-grant-tables
vim /etc/my.cnf #vim 打开配置文件

Press i to enter the insert mode of vim, add skip-grant-tables at the end, press esc to return to normal mode, enter to :wqsave and exit the my.cnf file.

After the configuration file is updated, you need to restart the MySQL service, and then log in without password

service mysqld restart 			  #重启 MySQL
mysql -u root -p 							#不用输入密码直接两次回车登录 MySQL

3.2 Modify the user table

The password field in the user table in version 5.6 is password, but it is in version 5.7 authentication_string. You can also select to view specific fields.

--打开数据库
use mysql;

--查看 root 用户的表字段
select * from user where user = "root"; 

--修改 root 用户密码
update user set authentication_string = password("root1234") where user = "root"; 

--刷新 MySQL 配置
flush privileges; 

--退出 MySQL
quit 							

3.3 Exit password-free login

The password has been reset. To turn off password-free login is to delete skip-grant-tables in the configuration file.

vim /etc/my.cnf

Find the line where skip-grant-tables is located, press dd in vim normal mode to delete the current line, save and exit my.cnf with :wq, then restart MySQL, and the new password will take effect.

service mysqld restart 		#重启 MySQL

Guess you like

Origin blog.csdn.net/qq_43220213/article/details/129608729