网站首页 > 技术教程 正文
MySQL数据库密码忘记了且没有其他可以修改账号密码的账户时怎么办呢?
登录MySQL,密码输入错误
/* 密码错误,报如下错误 */
[root@TESTDB ~]# mysql -uroot -p -P3306
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
如果忘记密码,对于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)
注:
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 statement
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 /* 注释掉该参数*/
5. 重启数据库
Mysql5.7中可以直接在MySQL命令行中使用shutdown命令关闭数据库,之后再启动数据库即可。
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>
至此,密码重置完毕。
TIPS: 生产环境的数据库密码一定要妥善保管,虽然可以找回,但需要重启,影响数据库可用性。
猜你喜欢
- 2025-08-01 phpmyadmin出现空密码登录被禁止 (参见 允许空密码)的解决办法
- 2025-08-01 Linux忘记密码解决方法
- 2025-08-01 MySQL5.7升级到8.0过程详解
- 2025-08-01 0825-7.1.4-如何重置Ranger Admin Web UI登录密码
- 2025-08-01 Prometheus监控mysql
- 2025-08-01 MySQL合集-小版本升级指南
- 2025-08-01 麒麟服务器操作系统安装 MySQL 8 实战指南
- 2025-08-01 忘记MySQL的root密码的解决方法
- 2025-08-01 Mysql8忘记密码/重置密码
- 2025-08-01 MySQL忘记密码怎么办?Windows环境下MySQL密码重置图文教程
你 发表评论:
欢迎- 08-06linux 和 windows文件格式互相转换
- 08-06谷歌 ChromeOS 已支持 7z、iso、tar 文件格式
- 08-06Linux下比较文件内容的6种方法
- 08-06文件格式及功能汇总
- 08-0610个Linux文件内容查看命令的实用示例
- 08-06Linux-如何区分不同文件类型
- 08-06Zabbix技术分享——监控windows进程资源使用情况
- 08-06Linux系统卡顿?学会ps命令这三招,轻松定位问题进程
- 最近发表
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)