第一步:在/etc/my.cnf配置文件中加入skip-grant-tables
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
default-storage-engine=InnoDB
innodb-file-per-table=1
character_set_server=utf8mb4
skip-grant-tables
[mysqld_safe]
[client]
default-character-set=utf8mb4
第二步:重启mysql
[root@slave1 mysql]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
第三步:不使用密码直接登录mysql,然后修改mysql.user表中的authentication_string字段的值
在mysql 5.7之前,mysql数据库用户的密码的字段是password,5.7之后变成了authentication_string
mysql> update mysql.user set authentication_string=password('123456') where user='root' and host='localhost';
Query OK, 1 row affected, 1 warning (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> \q
注意一定要执行flush privileges;不然不会生效,重置完密码之后,要把my.cnf配置文件中的skip-grant-tables注释到