1. Stop the MySQL service
/etc/init.d/mysqld stop
Restart Service
/usr/local/mysql/bin/mysqld_safe--skip-grant-tables--skip-networking&
Note: The--skip-networking parameter can prevent remote client connections and increase security
Or
Vi/etc/my.cnf
Add the following two lines in [Mysqld]
[Mysqld]
Skip-grant-tables
Skip-networking
Restart service after saving
/etc/init.d/mysqld start
You must remember to change the password after this change.
2. Change the password
/usr/local/mysql/bin/mysql-uroot
mysql> FLUSH privileges;
MySQL 5.7.6 and later versions perform this:
Mysql> ALTER USER ' root ' @ ' localhost ' identified by ' mynewpass ';
MySQL 5.7.5 and Previous versions do this:
mysql> SET PASSWORD for ' root ' @ ' localhost ' = PASSWORD (' Mynewpass ');
Then quit MySQL
Mysql> quit
Note: If ALTER USER fails to execute, try the following command:
UPDATE mysql.user SET authentication_string = PASSWORD (' Mynewpass ')
WHERE User = ' root ' and Host = ' localhost ';
FLUSH privileges;
3. Restart the service
/etc/init.d/mysqld restart
/usr/local/mysql/bin/mysql-uroot-p
You can already log in with the new password you just changed.
The above operations are referenced from the official MySQL manual:
Https://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html
mysql5.7 forgot root Password modification method