十五年测试老手,长期负责WEB\APP 项目测试,目前主要负责团队管理工作。

不重启Mysql修改root密码的方法

上一篇 / 下一篇  2014-03-15 20:33:40 / 个人分类:mysql

文章来源
  • 文章来源:【转载】

一、一般忘记密码的解决办法,需要重启Mysql51Testing软件测试网I$x8_|:yj_#ox
1、skip-grant-tables51Testing软件测试网[Zb vn z
我们常用的方法是使用skip-grant-tables选项,mysqld server启动之后并不使用权限系统(privilege system)。用户不需要任何账号、不受任何限制的访问数据库中所有数据。为了安全起见,通常加上 skip-networking ,mysqld不侦听任何TCP/IP连接请求。操作过程如下,51Testing软件测试网mU(y6q?Z%S
1)修改my.cnf配置文件,在mysqld选项中添加skip-grant-tables和skip-networking。
Rr5u4^S*G02)再重启mysqld server。51Testing软件测试网$l2E/Z`!I$ma%j,i~5[
3)通过sql语句修改mysql.user表中存储密码。执行flush privileges,重新启用mysql权限系统。51Testing软件测试网TWr7suax
代码如下:UPDATE mysql.USER SET Password=PASSWORD('newpwd')WHERE User='root';
+C7vWkDw5]6}0FLUSH PRIVILEGES;
'Y#qc%O[d*LpR q04)删除或者注释配置文件中skip-grant-tables和skip-networking的参数选项。如果使用skip-networking,则需要再次重启mysqld。因为skip-networking不是系统变量,只是mysqld的参数选项,而不能通过系统变量动态进行设置。如果没有适用skip-networking,只需要执行flush privileges就可以使权限系统重新生效。51Testing软件测试网:aC7?p3w%|
2. --init-file51Testing软件测试网q],t;? gv
mysqld_safe可以使–init-file参数选项来执行重新设定密码的sql语句。51Testing软件测试网K)_c)L&H kOg
1)新建一个初始化文件,如/tmp/initfile,文件内容为上面修改密码的sql语句。51Testing软件测试网PU%dRw'V8a2d
代码如下:UPDATE mysql.user SET Password=PASSWORD('newpwd') WHERE User='root';
Q6N:[:RnB1[(?l0FLUSH PRIVILEGES;
@D$Az.Pmw02)关闭mysqld服务进程。
&m;vZv M~E03)使用mysqld_safe启动mysqld;51Testing软件测试网%HV8Y!rnxagO:^`
代码如下:mysqld_safe --init-file=/home/me/mysql-init &
-m*`0v@ s*A_0上面的两种方法是在忘记root密码情况下重新设置密码的方法,可以发现都需要重启mysqld服务。很多人都是使用第一种进行重置root密码,但是比较推荐的做法反而是第二种,即安全有快捷简单。51Testing软件测试网X&h)V}4}

二、不重启mysqld的方法51Testing软件测试网w.{~'S/`k)D

1、首先得有一个可以拥有修改权限的mysql数据库账号,当前的mysql实例账号(较低权限的账号,比如可以修改test数据库)或者其他相同版本实例的账号。把data/mysql目录下面的user表相关的文件复制到data/test目录下面。
8ws/@9f)R$|6b)@0代码如下:51Testing软件测试网bp%U\Y$b O6dw
[root@localhost mysql]# cp mysql/user.* test/51Testing软件测试网V$d|0?)_ _c[
[root@localhost mysql]# chown mysql.mysql test/user.*
K1p|t\J6i02、使用另一个较低权限的账号链接数据库,设置test数据库中的user存储的密码数据。51Testing软件测试网4^V/Wz*d.b8nW4c:V
代码如下:51Testing软件测试网nf:uW0Ku4T6i
[root@localhost mysql]# mysql -utest -p1234551Testing软件测试网9l6l T2ca
Welcome to the MySQL monitor.  Commands end with ; or \g.51Testing软件测试网V9{L c(\[J t;q
Your MySQL connection id is 1751Testing软件测试网&Mfc7QWs\
Server version: 5.5.25a-log Source distribution

-Iu!i;e/n:y)Hdk0

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

0J ],E.M jz}dh0

Oracle is a registered trademark of Oracle Corporation and/or its
!LZ-Enld7~@0affiliates. Other names may be trademarks of their respective51Testing软件测试网G I$iPN&O)l)p
owners.51Testing软件测试网gI2wHX8{ X

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.51Testing软件测试网w5C%vHm(V

mysql> use test
_0AH sF1HTO/en{0Reading table information for completion of table and column names
2_6Sv4~ v{'uV0You can turn off this feature to get a quicker startup with -A

'h ?ZV^-O }g^0

Database changed
Q&]:q"rl5vp0mysql> update user set password=password('yayun') where user='root';51Testing软件测试网PUG\ y8J \GP
Query OK, 0 rows affected (0.00 sec)51Testing软件测试网#K Ws@x
Rows matched: 5  Changed: 0  Warnings: 051Testing软件测试网{1h)M%U#NyH:e)sP

mysql>51Testing软件测试网![C,M8fx#A
3、把修改后的user.MYD和user.MYI复制到mysql目录下,记得备份之前的文件。51Testing软件测试网]3zB9o"Vw
代码如下:
MS5T^)[XM_-y*p~.IS0mv mysql/user.MYD mysql/user.MYD.bak
$x+@"j7VK M0mv mysql/user.MYI mysql/user.MYI.bak
,sjZXd1R0cp test/user.MY* mysql/
\(mK1g$j3z1p|Jx0chown mysql.mysql mysql/user.*
o]g~+?N04、查找mysql进程号,并且发送SIGHUP信号,重新加载权限表。51Testing软件测试网"de T{ s
代码如下:51Testing软件测试网Gf/J]-L6m;|
[root@localhost mysql]# pgrep -n mysql51Testing软件测试网kaZIym8U#~
2184
2} @;h [+LN;Y kEt&j0[root@localhost mysql]#51Testing软件测试网t1]\+{f0iw6D@2\
[root@localhost mysql]# kill -SIGHUP 218451Testing软件测试网+r|p f"q#w6[-U
5、登陆测试51Testing软件测试网B!wp#aZ,\o w;{B"R
代码如下:51Testing软件测试网h1P5T$n\T3J
[root@localhost mysql]# mysql -uroot -pyayun
8o k#atpZP z0Welcome to the MySQL monitor.  Commands end with ; or \g.
K k,Md A Y0nl0Your MySQL connection id is 2051Testing软件测试网 f*p.^N%bjS d
Server version: 5.5.25a-log Source distribution

y\6cvh wFB[0

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

I7u6x5G6P*h vR0

Oracle is a registered trademark of Oracle Corporation and/or its51Testing软件测试网pm2Se|
affiliates. Other names may be trademarks of their respective51Testing软件测试网@8@ Z.b`e-l+h;W"B
owners.

f R Z+g*K9jx;K0

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.51Testing软件测试网'\:t%yd3D

mysql>

;TB FV^)Un0

TAG: MySQL root ROOT Mysql

 

评分:0

我来说两句

Open Toolbar