0
收藏
微博
微信
复制链接

MySQL数据库忘记密码的解决方法

2023-03-23 11:41
542

MySQL是世界上最著名的数据库系统之一,而数据库是很多项目中会用到的环节(用来存放数据),很多开发人员都需要掌握MySQL来更好进行开发,因此学会MySQL数据库开发是很有必要的,今天分享忘记MySQL数据库密码的解决方法,希望对小伙伴们有所帮助。

1.png

若工程师登录MySQL。输入密码,密码显示错误。如果找不到真正的密码,工程师可通过修改配置重启数据库来重置MySQL数据库密码,具体步骤如下:

1、修改数据库配置文件

vim  /etc/my.cnf

//添加如下参数

skip_grant_tables

2、重启数据库

若是工程师已经部署服务,可直接重启数据库,若是没有提前部署好,建议杀掉数据库近场,再重新启动数据库。

/*  重启数据库服务 */

/etc/init.d/mysqld restart

或 

ps -ef|grep mysql  /* 查出MySQL 的进程号,下一步中使用 */

kill  30516 29246    /*  不建议使用 kill -9   */

3、登录数据库修改密码

/*  此时可以直接登录数据库 无需输入密码 */

[root@TESTDB ~]# mysql -uroot    -P3306  

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 4

Server version: 5.7.23-24-log Percona Server (GPL), Release 24, Revision 57a9574

Copyright (c) 2009-2018 Percona LLC and/or its affiliates

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

//在修改密码

/* MySQL5.7 中修改密码 */

mysql> update mysql.user set authentication_string=password('123456') where user='root' and host='localhost';

Query OK, 0 rows affected, 1 warning (0.02 sec)

Rows matched: 1  Changed: 0  Warnings: 1

mysql> flush privileges;

Query OK, 0 rows affected (0.06 sec)

注意:不得使用set password直接修改密码,必须通过更新数据库表的方式来。

mysql> set password=password('123456');

ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

若使用update表,mysql.user的方式需要flush privileges生效

不同的版本mysql.user的字段以及密码加密方式不同。

4、还原配置文件

vim  /etc/my.cnf

#skip_grant_tables /* 注释掉该参数*/

5、重启数据库

mysql> shutdown;

Query OK, 0 rows affected (0.00 sec)

启动数据库可直接用重置后的密码登录。

[root@TESTDB ~]# mysql -uroot    -P3306   -p'123456'

mysql: [Warning] Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 3

Server version: 5.7.23-24-log Percona Server (GPL), Release 24, Revision 57a9574

Copyright (c) 2009-2018 Percona LLC and/or its affiliates

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

最后,密码重置已成功,当然,小伙伴们一定要妥善保管数据库密码,尤其是生产环境,虽然可以直接找回,但该过程需要重启数据库,很容易影响到数据库的可用性。

登录后查看更多
0
评论 0
收藏
侵权举报
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表凡亿课堂立场。文章及其配图仅供工程师学习之用,如有内容图片侵权或者其他问题,请联系本站作侵删。

热门评论0

相关文章

嵌入式大杂烩

分享嵌入式电子级设计的经验、心得、程序设计架构及测试

开班信息