不重启修改mysql root密码
--------------------2014/09/28
1、通常忘记密码的解决办法,须要重启Mysql
一、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权限系统。mysql
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语句。sql
2)关闭mysqld服务进程。
3)使用mysqld_safe启动mysqld;数据库
上面的两种方法是在忘记root密码状况下从新设置密码的方法,能够发现都须要重启mysqld服务。不少人都是使用第一种进行重置root密码,可是比较推荐的作法反而是第二种,即安全有快捷简单。安全
2、不重启mysqld的方法测试
一、首先得有一个能够拥有修改权限的mysql数据库帐号,当前的mysql实例帐号(较低权限的帐号,好比能够修改test数据库)或者其余相同版本实例的帐号。把data/mysql目录下面的user表相关的文件复制到data/test目录下面。ui
二、使用另外一个较低权限的帐号连接数据库,设置test数据库中的user存储的密码数据。this
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.spa
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.code
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.orm
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目录下,记得备份以前的文件。
四、查找mysql进程号,而且发送SIGHUP信号,从新加载权限表。
五、登录测试
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>