使用MySQL过程中我们偶尔会遇到忘记密码的情况,怎么办呢?能不能吧密码找回来或者重新设置密码呢?
MySQL忘记root密码之后,可以重新设置一个新的密码,怎么做呢?
修改配置文件my.cnf,在[mysqld]下面增加参数:skip-grant-tables(启动 MySQL 服务的时候跳过权限表认证。启动后,连接到 MySQL 的 root 将不需要口令。),重启MySQL服务,免密码登录。
[root@ocp11g~]# vi /etc/my.cnf
[root@ocp11g~]# service mysql restart
Shuttingdown MySQL.... SUCCESS!
StartingMySQL.. SUCCESS!
[root@ocp11g~]# mysql -uroot
Welcometo the MySQL monitor. Commands end with; or \g.
YourMySQL connection id is 2
Serverversion: 5.7.26-log MySQL Community Server (GPL)
Copyright(c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracleis 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>复制修改root用户密码为空
mysql>alter user root@localhost identified by '123456';
ERROR1290 (HY000): The MySQL server is running with the --skip-grant-tables optionso it cannot execute this statement
mysql>update user set authentication_string = NULL where user = 'root';
ERROR1046 (3D000): No database selected
mysql>use mysql
Readingtable information for completion of table and column names
Youcan turn off this feature to get a quicker startup with -A
Databasechanged
mysql>update user set authentication_string = NULL where user = 'root';
QueryOK, 1 row affected (0.01 sec)
Rowsmatched: 1 Changed: 1 Warnings: 0
mysql>复制修该配置文件删除skip-grant-tables参数,重启MySQL服务,使用空密码登录,修改root密码
[root@ocp11g~]# vi /etc/my.cnf
[root@ocp11g~]# service mysql restart
Shuttingdown MySQL.. SUCCESS!
StartingMySQL. SUCCESS!
[root@ocp11g~]# mysql -uroot
Welcometo the MySQL monitor. Commands end with; or \g.
YourMySQL connection id is 3
Serverversion: 5.7.26-log MySQL Community Server (GPL)
Copyright(c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracleis 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>alter user root@localhost identified by '123456';
QueryOK, 0 rows affected (0.00 sec)
mysql>复制密码修改完成,MySQL root密码已修改,MySQL正常使用
[root@ocp11g~]# mysql -uroot
ERROR1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@ocp11g~]# mysql -uroot -p
Enterpassword:
Welcometo the MySQL monitor. Commands end with; or \g.
YourMySQL connection id is 5
Serverversion: 5.7.26-log MySQL Community Server (GPL)
Copyright(c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracleis 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>exit
Bye复制
文章转载自写程序的猫,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
【MySQL 30周年庆】MySQL 8.0 OCP考试限时免费!教你免费领考券
墨天轮小教习
504次阅读
2025-04-25 18:53:11
墨天轮个人数说知识点合集
JiekeXu
445次阅读
2025-04-01 15:56:03
MySQL 30 周年庆!MySQL 8.4 认证免费考!这次是认真的。。。
严少安
426次阅读
2025-04-25 15:30:58
MySQL数据库当前和历史事务分析
听见风的声音
426次阅读
2025-04-01 08:47:17
MySQL 生产实践-Update 二级索引导致的性能问题排查
chengang
390次阅读
2025-03-28 16:28:31
【活动】分享你的压箱底干货文档,三篇解锁进阶奖励!
墨天轮编辑部
364次阅读
2025-04-17 17:02:24
MySQL 9.3 正式 GA,我却大失所望,新特性亮点与隐忧并存?
JiekeXu
357次阅读
2025-04-15 23:49:58
3月“墨力原创作者计划”获奖名单公布
墨天轮编辑部
332次阅读
2025-04-15 14:48:05
云和恩墨杨明翰:安全生产系列之MySQL高危操作
墨天轮编辑部
310次阅读
2025-03-27 16:45:26
openHalo问世,全球首款基于PostgreSQL兼容MySQL协议的国产开源数据库
严少安
303次阅读
2025-04-07 12:14:29