1, vi /etc/my.cnf # Edit the file, find the [mysqld], add a line skip-grant-tables below
[mysqld]
skip-grant-tables
:! Wq # save and exit
service mysqld restart # restart the MySQL service
2, into the MySQL console
mysql -uroot -p # press enter, then you do not need to enter the root password.
3, change the root password
update user set authentication_string=password('新密码') where user='root' and Host='localhost'; mysql > 5.7
update mysql.user set password=password('新密码') where user='root' and host='localhost'; mysql < 5.7
flush privileges; # refresh system authorization form
grant all on *.* to 'root'@'localhost' identified by '123456' with grant option;
4, cancel skip-grant-tables /etc/my.cnf in
vi /etc/my.cnf edit the file, find the [mysqld], skip-grant-tables to delete this line
:! Wq # save and exit
5, restart mysql
service mysqld restart # restart mysql, mysql root password at this time has been changed to 123456
6, into the mysql console
mysql -uroot -p # into the mysql console
Enter the password 123456 #