不重启修改mysql root密码
--------------------2014/09/28

一、一般忘记密码的解决办法,需要重启Mysql
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('newpwd')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('newpwd') WHERE User='root';
FLUSH PRIVILEGES;

2)关闭mysqld服务进程。
3)使用mysqld_safe启动mysqld;

复制代码代码如下:
mysqld_safe --init-file=/home/me/mysql-init &

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

二、不重启mysqld的方法

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

复制代码代码如下:
[root@localhost mysql]# cp mysql/user.* test/
[root@localhost mysql]# chown mysql.mysql test/user.*

2、使用另一个较低权限的账号链接数据库,设置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>

3、把修改后的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.*

4、查找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>

不重启修改mysql root密码的更多相关文章

  1. 修改MySql Root密码(包含忘记密码的方式)

    曾几何时,我也是记得MySQL root密码的人,想要修改root密码还不是轻而易举的事?下面前三种修改改方式都是在记得密码的情况下进行修改,如果你忘记了原本的root,请直接跳至 终极 第一种: 在 ...

  2. Linux下修改mysql root密码

    1.修改MySQL的配置文件(默认为/etc/my.cnf),在[mysqld]下添加一行skip-grant-tables 2.保存配置文件后,重启MySQL服务 service mysqld re ...

  3. 修改mysql root密码的方法

    方法1: 用SET PASSWORD命令 首先登录MySQL. 格式:mysql> set password for 用户名@localhost = password('新密码'); 例子:my ...

  4. [mysql] 记osx 10.10系统修改mysql root 密码

    http://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html亲测方法3,已成功重置密码.(感谢@非常,告诉我官网就有重置方法,网上 ...

  5. 一次莽撞的行为:在phpmyadmin中修改MySQL root密码后无法操作数据库

    一.手贱行为(✿◡‿◡) 在一次开发中通过xampp方式安装了PHP环境,需要操作数据库时通过phpmyadmin访问MySQL,在一次数据库操作时想起没有设置密码,于是直接在mysql数据库中的us ...

  6. 修改mysql root密码

    mysql密码丢失后,在mysql命令行下执行如下命令,即可将root用户密码清空: mysqld_safe --skip-grant-tables& mysql修改密码 mysql修改,可在 ...

  7. mac 修改mysql root密码

    01-在系统偏好设置中停止mysql服务 或者使用指令 sudo /usr/local/mysql/support-files/mysql.server stop 02-输入指令: cd /usr/l ...

  8. 修改mysql root 密码

    C:\Program Files\MySQL\MySQL Server 5.6\bin mysqld --skip-grant-tables 开启一新窗口:然后输入mysql -uroot -p up ...

  9. windows7 dos修改mysql root密码

    第一步:打开mysql 安装路径  选择bin文件  同时按下Shift+鼠标右键  点击"在此处打开命令" 第二步:输入mysql -u root -p 按回车键会提示输入密码 ...

随机推荐

  1. ES6的十大特性和认知

    ---恢复内容开始--- ES6(ECMAScript2015)的出现,无疑给前端开发人员带来了新的惊喜,它包含了一些很棒的新特性,可以更加方便的实现很多复杂的操作,提高开发人员的效率.本文主要针对E ...

  2. 最长非降子序列的O(n^2)解法

    这次我们来讲解一个叫做"最长非下降子序列"的问题及他的O(n^2)解法. 首先我们来描述一下什么是"最长非下降子序列". 给你一个长度为n的数组a,在数组a中顺 ...

  3. servlet+jsp update修改页面的实现,整整搞了两个小时才搞定

    package DAO; public class books { private int bid; private String bname; private int booksl; private ...

  4. VB6之CRC32

    翻译篇:http://www.cnblogs.com/duzouzhe/archive/2009/08/05/1539543.html Private Declare Function GetTick ...

  5. unslider插件的使用

    深入理解unslider.js源码 最近用到了一个挺好用的幻灯片插件,叫做unslider.js,就想看看怎么实现幻灯片功能,就看看源码,顺便自己也学习学习.看完之后收获很多,这里和大家分享一下. u ...

  6. iOS动画学习-CALayer

    iOS中有很多方法可以实现动画,我们可以用CAKeyframeAnimation, CABasicAnimation,CASpringAnimation(iOS9.0中添加的,实现弹簧的效果),也可以 ...

  7. java IO之 编码 (码表 编码 解码 转换流)

    编码 什么是编码? 计算机中存储的都是二进制,但是要显示的时候,就是我们看到的却可以有中国 ,a  1 等字符 计算机中是没有存储字符的,但是我们却看到了.计算机在存储这些信息的时候,根据一个有规 则 ...

  8. JavaScript实现一个复数类

    <script type="text/javascript"> /** * 这里定义Complex类,用来描述复数 */ /** * 这个构造函数为它所创建的每个实例定 ...

  9. HDU 1814 Peaceful Commission / HIT 1917 Peaceful Commission /CJOJ 1288 和平委员会(2-sat模板题)

    HDU 1814 Peaceful Commission / HIT 1917 Peaceful Commission /CJOJ 1288 和平委员会(2-sat模板题) Description T ...

  10. springJdbc like模糊查询,Spring namedParameterJdbcTemplate like查询

    springJdbc like模糊查询,Spring namedParameterJdbcTemplate like查询, SpringJdbc命名参数like模糊查询,namedParameterJ ...