MySQL忘记root密码--不重启mysqd重置root密码

先提个问题:如何不重启mysqld,且没有权限修改用户帐号和权限的状况下,如何从新设置root密码?不知道不要紧,在此以前我也是不知道如何操做的,先看看下面的几种重置root密码的方法。mysql

一、skip-grant-tables

咱们经常使用的方法是使用skip-grant-tables选项,mysqld server启动以后并不使用权限系统(privilege system)。用户不须要任何帐号、不受任何限制的访问数据库中全部数据。为了安全起见,一般加上skip-networking,mysqld不侦放任何TCP/IP链接请求。操做过程以下,sql

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('newpwd')WHERE user='root';

FLUSH PRIVILEGES;

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

2. --init-file

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

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

UPDATE mysql.user SET Password=PASSWORD('newpwd') WHERE User='root';
 
FLUSH PRIVILEGES;

2)关闭mysqld服务进程。code

3)使用mysqld_safe启动mysqld;orm

mysqld_safe --init-file=/home/me/mysql-init &

上面的两种方法是在忘记root密码状况下从新设置密码的方法,能够发现都须要重启mysqld服务。不少人都是使用第一种进行重置root密码,可是比较推荐的作法反而是第二种,即安全有快捷简单。

三、不重启mysqld的方法

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

[root@localhost mysql]# cp mysql/user.* test/

[root@localhost mysql]# chown mysql.mysql test/user.*

二、使用另外一个较低权限的帐号连接数据库,设置test数据库中的user存储的密码数据。

[root@localhost mysql]# mysql -utest -p12345
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.5.25a-log Source distribution

Copyright (c) 2000, 2011, 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> use test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set password=password('yayun') where user='root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 5  Changed: 0  Warnings: 0

mysql>

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

mv mysql/user.MYD mysql/user.MYD.bak
mv mysql/user.MYI mysql/user.MYI.bak
cp test/user.MY* mysql/
chown mysql.mysql mysql/user.*

四、查找mysql进程号,而且发送SIGHUP信号,从新加载权限表。

[root@localhost mysql]# pgrep -n mysql
2184
[root@localhost mysql]#

[root@localhost mysql]# kill -SIGHUP 2184

5.登录测试

[root@localhost mysql]# mysql -uroot -pyayun
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 5.5.25a-log Source distribution

Copyright (c) 2000, 2011, 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>
相关文章
相关标签/搜索