1. In any directory, enter the following to close the mysql service
systemctl stop mysqld
2. Find the my.cnf configuration file
Enter the following to enter the directory
whereis my.cnf
3. Edit the my.cnf configuration file and add: skip-grant-tables under the [mysqld] label
vim /etc/my.cnf
然后点击 i 键
然后在[mysqld]标签下添加:skip-grant-tables,
然后点击 esc 键
然后 shift+: 两个键一起按
然后输入:wq
然后回车保存后退出
4. Enter the following to start the mysql service
systemctl start mysqld
5. Log in to mysql without a password, and press Enter directly after executing the following command
mysql -u root
6. When using the mysql library in the database, the semicolon must be written
use mysql;
7. Execute sql update password
update user set password=password('你的新密码') where user='root';
Note: If the above execution report does not have a password field
Then execute this, replace password with authentication_string
update user set authentication_string=password('123456') where user='root';
8. Refresh
flush privileges;
9. Exit after successful modification
exit
10. Stop mysql service
systemctl stop mysqld
11. Remove the skip-grant-tables content in my.cnf
vim /etc/my.cnf
12. Start mysql service
systemctl start mysqld
13. Log in
mysql -u root -p123456