MySQL忘记root密码的找回方法
(1)登录到数据库所在服务器,手工kill掉MySQL进程:
1 | kill ' cat /mysql-data-directory/hostname.pid' |
其中,/mysql-data-directory/hostname.pid指的是MySQL数据目录下的.pid文件,它记录了MySQL服务的进程号。
(2)使用--skip-grant-tables选项重启MySQL服务:
1 2 3 4 | [root@iZ28dr6w0qvZ mysql]# ./bin/mysqld_safe --skip-grant-tables --user=root & [1] 17299 [root@iZ28dr6w0qvZ mysql]# 151006 13:14:41 mysqld_safe Logging to '/alidata/log/mysql/error.log' . 151006 13:14:41 mysqld_safe Starting mysqld daemon with databases from /alidata/server/mysql/data |
其中--skip-grant-tables选项的意思是启动MySQL服务的时候跳过权限表认证。启动后,连接到MySQL的root将不需要命令。
(3)用空密码的root用户连接到MySQ,并且更新root口令:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | [root@iZ28dr6w0qvZ ~]# mysql -uroot Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.5.37-log MySQL Community Server (GPL) Copyright (c) 2000, 2014, 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> set password = password ( 'ysj123' ); ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement mysql> update mysql. user set password = password ( '123456' ) where user = 'root' and host= 'localhost' ; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 |
此时,由于使用了--skip-grant-tables选项启动,使用“set password”命令更改密码失败,直接更新user表的password字段后更改密码成功。
(4)刷新权限表,使得权限认证重新生效:
1 2 | mysql> flush privileges ; Query OK, 0 rows affected (0.00 sec) |
(5)重新用root登录时,必须输入新口令:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [root@iZ28dr6w0qvZ ~]# mysql -uroot ERROR 1045 (28000): Access denied for user 'root' @ 'localhost' (using password : NO ) [root@iZ28dr6w0qvZ ~]# mysql -uroot -p Enter password : Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 5.5.37-log MySQL Community Server (GPL) Copyright (c) 2000, 2014, 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中,密码丢失后无法找回,只能通过上述方式修改密码。
别忘了给个赞哦!~
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· [翻译] 为什么 Tracebit 用 C# 开发
· 腾讯ima接入deepseek-r1,借用别人脑子用用成真了~
· Deepseek官网太卡,教你白嫖阿里云的Deepseek-R1满血版
· DeepSeek崛起:程序员“饭碗”被抢,还是职业进化新起点?
· 深度对比:PostgreSQL 和 SQL Server 在统计信息维护中的关键差异