I believe that many of my friends have the same experience as me. After installing mysql on a virtual machine, they forgot the root password after a period of time. embarrassment.......
Especially after version 5.7, a large number of blogs or posts on the Internet about the method of resetting the root password are not available, and I am also a victim. Later, I got a method on google, and it is feasible to test it personally, record it and share it with everyone...............
My system version: centos7
1. Stop the mysql service:
systemctl stop mysqld
2. Set mysql environment parameters
systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"
3. Start the mysql service
systemctl start mysqld
4. Login as root user
mysql -u root
5. Set a new password
mysql> UPDATE mysql.user SET authentication_string = PASSWORD('MyNewPassword') WHERE User = 'root' AND Host = 'localhost';
mysql> FLUSH PRIVILEGES;
mysql> quit
6. Stop the mysql service
systemctl stop mysqld
7. Cancel the environment parameters set in the second step
systemctl unset-environment MYSQLD_OPTS
8. Start the mysql service
systemctl start mysqld
9. Login with new password
mysql -u root -p
10. Done!