码迷,mamicode.com
首页 > 数据库 > 详细

Mysql修改和破解登录密码(详)

时间:2016-10-01 06:45:21      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:mysql数据库密码破解

Mysql修改或破解登录密码

一、重置MySQL管理密码

跳过授权表启动MySQL服务程序

这一步主要利用mysqld的 --skip-grant-tables选项,具体操作时可选择不同方式,但本质上是一样的。以下提供三种方式,任选其中一种都可以。

方式1(推荐),执行mysql脚本起服务,末尾加 --skip-grant-tables 参数:

[root@dbsvr1 ~]# service mysql stop  服务关闭
[root@dbsvr1 ~]# service mysql start --skip-grant-tables
StartingMySQL............                     [确定]
[root@dbsvr1 ~]# service mysql status
MySQL running (45640)         [确定]
[root@dbsvr1 ~]# mysql -u root
mysql>  update mysql.user set password=password("123456") 
    ->  where user="root" and host="localhost";
 mysql>  flush privileges;
 mysql>  exit
 使用新密码123456重新登录

方式2,执行mysqld_safe进程,添加 --skip-grant-tables 参数:

[root@dbsvr1 ~]# service mysql restart  服务必须开启
[root@dbsvr1 ~]# mysqld_safe --user=mysql --skip-grant-tables &
[1] 46076
160930 13:23:51 mysqld_safe Logging to ‘/var/lib/mysql/www.ceshi1.com.err‘.
160930 13:23:51 mysqld_safe A mysqld process already exists
输入:mysql  回车即可进入数据库
mysql>  update mysql.user set password=password("123456") 
    ->  where user="root" and host="localhost";
 mysql>  flush privileges;
 mysql>  exit
 使用新密码123456重新登录

方式3,修改my.cnf配置,添加 skip_grant_tables=1启动设置:

[root@dbsvr1 ~]# vim /etc/my.cnf
[mysqld]
skip_grant_tables=1
.. ..
[root@dbsvr1 ~]# service mysql restart
[root@dbsvr1 ~]# mysql -u root
mysql>  update mysql.user set password=password("123456") 
    ->  where user="root" and host="localhost";
 mysql>  flush privileges;
 mysql>  exit
 使用新密码123456重新登录
 
 注:正常登录后vim /etc/my.cnf 注释掉或删除 skip_grant_tables=1  重启服务即可

为了避免冲突,上述三种方式不要同时使用。若要分别测试不同方式,同样要先停用其他方式启动的MySQL服务程序(直接service mysql stop即可)。


通过执行“FLUSH PRIVILEGES;”可使授权表立即生效,对于正常运行的MySQL服务,也可以用上述方法来修改密码,不用重启服务。本例中因为是恢复密码,最好重启MySQL服务程序,所以上述“FLUSH PRIVILEGES;”操作可跳过。



二、正常设置MySQL管理密码

正常的前提是:已知当前MySQL管理用户(root)的密码。

1)方法1,在Shell命令行下设置

使用mysqladmin管理工具,需要验证旧的密码。比如,以下操作将会把root的密码设置为 1234567:

[root@dbsvr1 ~]# mysqladmin -u root -p password ‘1234567‘
Enter password:                                  //验证原来的密码

2)方法2,以root登入mysql> 后,使用SET PASSWORD指令设置

这个与新安装MySQL-server后首次修改密码时要求的方式相同,平时也可以用:

mysql> SET PASSWORD FOR root@localhost=PASSWORD(‘1234567‘);
Query OK, 0 rows affected (0.16 sec)

3)方法3,以root登入mysql> 后,使用GRANT授权工具设置

这个是最常见的用户授权方式(下一节会做更多授权的练习):

mysql> GRANT all ON *.* TO root@localhost IDENTIFIED BY ‘1234567‘;
Query OK, 0 rows affected (0.00 sec)

4)方法4,以root登入mysql> 后,使用UPDATE更新相应的表记录

这种方法与恢复密码时的操作相同:

mysql> UPDATE mysql.user SET password=PASSWORD(‘1234567‘)
-> WHERE user=‘root‘ AND host=‘localhost‘;   //重设root的密码Query OK, 0 rows affected (0.17 sec)
Rows matched: 1  Changed: 0  Warnings: 0
mysql> FLUSH PRIVILEGES;                      //刷新授权表Query OK, 0 rows affected (0.00 sec)

在上述方法中,需要特别注意:当MySQL服务程序以 --skip-grant-tables 选项启动时,如果未执行“FLUSH PRIVILEGES;”操作,是无法通过SET PASSWORD或者GRANT方式来设置密码的。比如,验证这两种方式时,都会看到ERROR 1290的出错提示:

mysql> SET PASSWORD FOR root@localhost=PASSWORD(‘1234567‘);
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> GRANT all ON *.* TO root@localhost IDENTIFIED BY ‘1234567‘;
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables


本文出自 “kingson” 博客,请务必保留此出处http://81703069.blog.51cto.com/5524831/1858092

Mysql修改和破解登录密码(详)

标签:mysql数据库密码破解

原文地址:http://81703069.blog.51cto.com/5524831/1858092

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!