1.修改主配置文件,添加 skip-grant-tables,跳过授权表
#vim /etc/my.cnf[mysqld]skip-grant-tables
2.重启mysqld
#systemctl restart mysqld
3.无需输入密码进入mysql,执行修改密码的命令,刷新并退出
#mysql -u root -p //直接回车进入数据库,无需输入密码mysql> use mysql;Database changedmysql> update user set authentication_string=password(\"123456\") where User=\"root\";Query OK, 1 row affected, 1 warning (0.01 sec)Rows matched: 1 Changed: 1 Warnings: 1mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)mysql> quit;Bye[root@zhangyx ~]# systemctl restart mysqld[root@zhangyx ~]# vim /etc/my.cnf[root@zhangyx ~]# mysql -u root -pEnter password:Welcome to the MySQL monitor. Commands end with ; or \\g.Your MySQL connection id is 3Server version: 5.7.17 MySQL Community Server (GPL)Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type \'help;\' or \'\\h\' for help. Type \'\\c\' to clear the current input statement.
4.恢复主配置文件,重启mysqld
#systemctl restart mysqld
5.再进入 vim /etc/my.cnf,然后把skip-grant-tables注释掉,最后再重启mysqld就可以了
[mysqld]# skip-grant-tables