MySQL运维实战(2.2)忘记密码如何处理

作者:俊达

引言

当你突然忘记了一个普通用户的密码,而又想着通过管理员账号去改密码时,却猛的发现所有管理员账号的密码都离谱地被你忘了。嗨呀,这可真是个尴尬的大麻烦!root账户通常是MySQL中的大boss,你会发现自己掉进了密码的黑洞里。或许这时你会想撞墙了,反复~

但别急!All is not lost(一切尚未失去)。

当你忘记了root密码,有一个神奇的办法:用skip-grant-tables参数来启动数据库,然后重新设置root密码。搞定之后,你又会发现,所有人都可以无密码进入数据库,简直就像个开放式派对!!!但别担心,你大可以配合bind-address或skip-networking参数一起使用,只允许本机的小伙伴们参与这场数据库的"派对"。下面,将手把手地教你如何重新设置MySQL root密码。

1、停止数据库实例。

停止mysql实例。可以使用多种方法停止实例。这里使用serivce停止mysql

powershell 复制代码
service mysqld stop

2、配置文件中增加skip-grant-tables和skip-networking参数

powershell 复制代码
# /etc/my.cnf
skip-grant-tables
skip-networking

3、重新启动实例

powershell 复制代码
2021-04-06T15:53:17.250207Z 0 [Note] /usr/sbin/mysqld: ready for connections.
Version: '5.7.32'  socket: '/var/lib/mysql/mysql.sock'  port: 0  MySQL Community Server (GPL)

4、登陆数据库

由于加了skip-networking参数,只能通过socket登陆数据库

powershell 复制代码
[root@box1 ~]# mysql -h 127.0.0.1
ERROR 2003 (HY000): Cant connect to MySQL server on '127.0.0.1' (111)

[root@box1 ~]# mysql  -S /var/lib/mysql/mysql.sock
Welcome to the MySQL monitor.  Commands end with ; or \g.
...
mysql>

5、重新加载权限

实例使用skip-grant-tables参数启动,无法直接修改账号密码。需要执行flush privileges后才能修改密码。

使用alter user或set password命名修改密码。

powershell 复制代码
mysql> alter user 'root'@'localhost' identified by 'helloww';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)


mysql> alter user 'root'@'localhost' identified by 'helloww';
Query OK, 0 rows affected (0.00 sec)


mysql> set password for 'root'@'localhost' = 'helloww';
Query OK, 0 rows affected (0.00 sec)

mysql> set password for 'root'@'localhost' = password('helloww');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show warnings;
+---------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level   | Code | Message                                                                                                                                                                                 |
+---------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Warning | 1287 | 'SET PASSWORD FOR <user> = PASSWORD('<plaintext_password>')' is deprecated and will be removed in a future release. Please use SET PASSWORD FOR <user> = '<plaintext_password>' instead |
+---------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

6、修改参数,重新启动数据库

powershell 复制代码
[root@box1 ~]# sed -i '/skip-networking/d' /etc/my.cnf
[root@box1 ~]# sed -i '/skip-grant-tables/d' /etc/my.cnf
[root@box1 ~]# grep skip /etc/my.cnf
[root@box1 ~]# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service


[root@box1 ~]# tail -2 /var/log/mysqld.log
2021-04-06T16:02:20.527113Z 0 [Note] /usr/sbin/mysqld: ready for connections.
Version: '5.7.32'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MySQL Community Server (GPL)

7、验证数据库恢复正常

重启后需要使用密码才能登陆数据。

powershell 复制代码
[root@box1 ~]# mysql -uroot -h127.0.0.1
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

[root@box1 ~]# mysql -uroot -h127.0.0.1 -phelloww
Welcome to the MySQL monitor.  Commands end with ; or \g.
...
mysql>

恭喜你!通过上述神奇的操作,成功地夺回数据库控制权!更多技术信息请查看云掣官网https://yunche.pro/?t=yrgw

相关推荐
南客先生3 分钟前
MySQL索引优化、SQL分析与运行原理 - Java架构师面试实战
mysql·mvc·锁机制·sql分析·事务隔离级别·索引优化
husterlichf11 分钟前
MYSQL 常用字符串函数 和 时间函数详解
数据库·sql·mysql
BranH20 分钟前
Linux系统中命令设定临时IP
linux·运维·服务器
极小狐31 分钟前
极狐GitLab 项目功能和权限解读
运维·git·安全·gitlab·极狐gitlab
宁酱醇33 分钟前
GitLab_密钥生成(SSH-key)
运维·ssh·gitlab
秋风起,再归来~38 分钟前
【Linux庖丁解牛】—进程优先级!
linux·运维·服务器
Lalolander1 小时前
设备制造行业如何避免项目管理混乱?
运维·制造·工程项目管理·四算一控·epc·环保设备工程·设备制造
LucianaiB1 小时前
【金仓数据库征文】_AI 赋能数据库运维:金仓KES的智能化未来
运维·数据库·人工智能·金仓数据库 2025 征文·数据库平替用金仓
prinrf('千寻)2 小时前
nacos设置权重进行负载均衡不生效
运维·负载均衡
Lary_Rock2 小时前
Android 编译问题 prebuilts/clang/host/linux-x86
android·linux·运维