游客发表

MySQL数据库密码忘记了,怎么办?

发帖时间:2025-11-05 06:56:03

MySQL数据库密码忘记了且没有其他可以修改账号密码的怎么办账户时怎么办呢?

登录MySQL,密码输入错误

复制/* 密码错误,据库报如下错误 */[root@TESTDB ~]# mysql -uroot -p -P3306Enter password

:

ERROR 1045 (28000): Access denied for user root@localhost (using password: YES)1.2.3.4.

如果忘记密码,密码对于MySQL而言处理起来也相对比较简单。忘记但需要修改配置,怎么办重启数据库。据库可以按照如下步骤处理。密码

1. 修改数据库配置文件 复制vim /etc/my.cnf-- 添加如下参数skip_grant_tables1.2.3. 2. 重启数据库

如果部署了服务 可以重启数据库服务重启,忘记如果没有部署,怎么办需要杀掉数据库进程,据库在重新启动数据库。b2b信息网密码

复制/* 重启数据库服务 *//etc/init.d/mysqld restart或ps -ef|grep mysql /* 查出MySQL 的忘记进程号,下一步中使用 */kill 30516 29246 /* 不建议使用 kill -9 */1.2.3.4.5.6.7. 3. 登录数据库修改密码 复制/* 此时可以直接登录数据库 无需输入密码 */[root@TESTDB ~]# mysql -uroot -P3306Welcome to the MySQL monitor. Commands end with ; or \g

.

Your MySQL connection id is 4Server version: 5.7.23-24-log Percona Server (GPL), Release 24, Revision 57a9574Copyright (c) 2009-2018 Percona LLC and/or its affiliatesCopyright (c) 2000, 2018, 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>1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20. 复制再修改密码1. 复制/* 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: 1mysql> flush privileges

;

Query OK, 0 rows affected (0.06 sec)1.2.3.4.5.6.7.8.

注:

a) 不可以使用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 statement1.2.

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

c) 不同的版本mysql.user的字段以及密码加密方式不同,例如MySQL5.6中密码存储在password中,据库MySQL8.0中加密方式有变更等,密码处理时需要根据版本来相应修改脚本处理。

4 . 将配置文件还原

去掉第1步中my.cnf配置文件中添加的云服务器skip_grant_tables参数

复制vim /etc/my.cnf

#skip_grant_tables /* 注释掉该参数*/1.2. 5. 重启数据库

Mysql5.7中可以直接在MySQL命令行中使用shutdown命令关闭数据库,之后再启动数据库即可。

复制mysql> shutdown

;

Query OK, 0 rows affected (0.00 sec)1.2.

启动后,即可使用重置后的密码登录

复制[root@TESTDB ~]# mysql -uroot -P3306 -p123456mysql: [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 3Server version: 5.7.23-24-log Percona Server (GPL), Release 24, Revision 57a9574Copyright (c) 2009-2018 Percona LLC and/or its affiliatesCopyright (c) 2000, 2018, 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>1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.

至此,密码重置完毕。

TIPS: 生产环境的数据库密码一定要妥善保管,虽然可以找回,但需要重启,影响数据库可用性。免费信息发布网

    热门排行

    友情链接