数据库忘记密码后重新设置:
1.修改主配置文件,添加 skip-grant-tables,跳过授权表
vim /etc/my.cnf
[root@zhai ~]# vim /etc/my.cnf
[mysqld]
skip-grant-tables
如下图
2.重启mysqld
systemctl restart mysqld
[root@zhai ~]# systemctl restart mysqld
3.无需输入密码进入mysql,执行修改密码的命令,刷新并退出
mysql -u root -p
直接回车进入数据库,无需输入密码
mysql> use mysql;
mysql> update user set authentication_string=password(“123456”) where User=“root”;
mysql> flush privileges;
mysql> quit;
[root@zhai ~]# 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.mysql> use mysql;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> update user set authentication_string=password(\"123456\") where User=\"root\";Query OK, 1 row affected, 1 warning (0.00 sec)Rows matched: 1 Changed: 1 Warnings: 1mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)mysql> quit;Bye
4.恢复主配置文件,即进入 vim /etc/my.cnf,然后把skip-grant-tables注释掉,最后重启mysqld
systemctl restart mysqld![在这里插入图片描述](https://img-blog.csdnimg.cn/20200722223022258.PNG)
[root@zhai ~]# systemctl restart mysqld[root@zhai ~]# vim /etc/my.cnf[root@zhai ~]# 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.