mysql忘记密码真的需要重启服务吗?
admin
2023-06-08 05:21:31
0

先提个问题:如何不重启mysqld,且没有权限修改用户账号和权限的情况下,如何重新设置root密码?不知道没关系,在此之前我也是不知道如何操作的,先看看下面的几种重置root密码的方法。

 

1、skip-grant-tables

我们常用的方法是使用skip-grant-tables选项,mysqld server启动之后并不使用权限系统(privilege system)。用户不需要任何账号、不受任何限制的访问数据库中所有数据。为了安全起见,通常加上skip-networking,mysqld不侦听任何TCP/IP连接请求。操作过程如下,

1)修改my.cnf配置文件,在mysqld选项中添加skip-grant-tables和skip-networking。

2)再重启mysqld server。

3)通过sql语句修改mysql.user表中存储密码。执行flush privileges,重新启用mysql权限系统。

update mysql.user set password=password('newpassword') where user='root';
flush privileges;

4)删除或者注释配置文件中skip-grant-tables和skip-networking的参数选项。如果使用skip-networking,则需要再次重启mysqld。因为skip-networking不是系统变量,只是mysqld的参数选项,而不能通过系统变量动态进行设置。如果没有适用skip-networking,只需要执行flush privileges就可以使权限系统重新生效。

 

2. --init-file

mysqld_safe可以使–init-file参数选项来执行重新设定密码的sql语句。

1)新建一个初始化文件,如/tmp/initfile,文件内容为上面修改密码的sql语句。

update mysql.user set password=password('newpassword') where user='root';

flush privileges;

2)关闭mysqld服务进程。

3)使用mysqld_safe启动mysqld;

mysqld_safe --init-file=/tmp/initfile &

上面的两种方法是在忘记root密码情况下重新设置密码的方法,可以发现都需要重启mysqld服务。很多人都是使用第一种进行重置root密码,但是比较推荐的做法反而是第二种,即安全有快捷简单。

 

3、不重启mysqld的方法

1)首先得有一个可以拥有修改权限的mysql数据库账号,当前的mysql实例账号(较低权限的账号,比如可以修改test数据库)或者其他相同版本实例的账号。把data/mysql目录下面的user表相关的文件复制到data/test目录下面。

mv mysql/user.* test/
chown mysql:mysql test/user.*

2)使用另一个较低权限的账号链接数据库,设置test数据库中的user存储的密码数据。

[root@xiaoya test]# mysql -utest -ptest
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 5
Server version: 5.6.31-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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.
(product)test@localhost [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
+--------------------+
2 rows in set (0.00 sec)
(product)test@localhost [(none)]> update test.user set password=password(123456) where user='root';
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    6
Current database: *** NONE ***
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
(product)test@localhost [(none)]> select user,host from test.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | localhost |
| test | localhost |
+------+-----------+
2 rows in set (0.00 sec)
(product)test@localhost [(none)]> quit
Bye

3)把修改后的user.MYD和user.MYI复制到mysql目录下,记得备份之前的文件。

mv test/user.* ../mysql/
chown mysql:mysql test/user.*

4、查找mysql进程号,并且发送SIGHUP信号,重新加载权限表。

[root@xiaoya mysql]# pgrep -n mysql

19764

[root@xiaoya mysql]# kill -SIGHUP 19764

5.登陆测试

[root@xiaoya mysql]# mysql -uroot -p123456
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 7
Server version: 5.6.31-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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.
(product)root@localhost [(none)]> quit
Bye

相关内容

热门资讯

取用地下水的河南尉氏县,为何因... 极目新闻记者 詹钘连续呕吐腹痛两天后,河南尉氏县居民段梅(化名)终于缓了过来。5月22日早晨,她尝试...
一次补税超5亿,知名医院怎么了... 5月20日,爱尔眼科医院集团股份有限公司发布公告称,经自查,公司需补缴税款3.48亿元、滞纳金1.7...
山西矿难,一场早有预兆的祸事 文丨李一鸣 姜鸥桐 编辑丨卢伊“矿难”“瓦斯爆炸”,这是很多年没有出现在我们视野中的语汇,但它就是这...
朱杨柱、张志远、黎家盈,领命出... ‍‍央视新闻消息,5月24日,神舟二十三号载人飞行任务航天员乘组出征仪式在酒泉卫星发射中心问天阁圆梦...
女子在车厢内大声唱歌、举止怪异... 极目新闻记者 李淑仪5月23日,有网友发视频称,自己乘坐由呼和浩特铁路局承运的K886/K887次列...
天玑7020等于骁龙多少(天玑... 天玑720相当于高通骁龙765G。天玑720采用了台积电7nm制程工艺,EUV是采用波长15nm的极...
坐便器水箱一直流水怎么办 坐便器水箱一直流水可能是由以下原因导致的:1. 水箱配件老化或损坏:水箱内的配件,如浮球、排水阀等,...
安装墙面收纳柜的好处及注意事项 安装墙面收纳柜可以提供合理的收纳空间,色彩和设计上要简洁明快,注意大小和线路布局,选择适合的颜色和灯...
安装中央空调的注意事项 中央空调是现代家庭的常见设备之一,安装中央空调需要注意以下事项: 1.选择合适的品牌和型号 选择...
led灯不亮了但有弱光怎么办 LED灯在使用过程中,容易出现各种故障,比如灯不亮了,但是有弱光,这怎么处理好?小编带大家一起了解下...