天将降大任于是人也,必先苦其心志,劳其筋骨,饿其体肤,空乏其身,行拂乱其所为,所以动心忍性,曾益其所不能。

时间在流逝,在不正经就晚咯

MySQL密码修改(四)

一、修改破解MySQL密码

  1.1:修改密码

在知道原始密码的情况下
[root@web1 ~]# mysqladmin -uroot -p -S /home/mysql/3307/mysql.sock password "brian123"   # 要修改的密码
Enter password:123456

# 测试
[root@web1 ~]# mysql -uroot -p -S /home/mysql/3307/mysql.sock
Enter password:123456
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

[root@web1 ~]# mysql -uroot -p -S /home/mysql/3307/mysql.sock
Enter password:brian123
[root@web1 ~]# mysql -uroot -p -S /home/mysql/3307/mysql.sock
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.23 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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>



# 登录数据库修改密码
update mysql.user set authentication_string=password('123qwe') where user='root' and Host = 'localhost';
alter user 'root'@'localhost' identified by '123'; 
set password for 'root'@'localhost'=password('123');
# 一定记得刷新权限,不然不生效
flush privileges;

  1.2:破解密码

# 数据库密码忘记了
PS:在单实例的情况下我们是可以停掉数据库的,但是在多实例的情况下没有密码是停不了的,只能用kill 来停的

# 使用kill停止不记得密码的数据库
[root@web1 ~]# ps aux | grep 3306 | awk '{print $2}' |xargs kill

# 使用mysqld_safe 加参数绕过认证
PS: 我用的是多实例的,根据实际情况修改下面的路径就行
[root@web1 ~]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/home/mysql/3306/my.cnf --user=mysql --skip-grant-tables &
[root@web1 ~]# netstat -lntup | grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      57475/mysqld

# 现在就是没有密码可以登录了
[root@web1 ~]# mysql -uroot -p -S /home/mysql/3306/mysql.sock
Enter password:                                                     # 这一步直接回车
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> update mysql.user set authentication_string=password('123qwe') where user='root' and Host = 'localhost';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql>

# 停掉MySQL重新启动 这次不用指定--skip-grant-tables参数了
[root@web1 ~]# ps aux | grep 3306 | awk '{print $2}' |xargs kill
[root@web1 ~]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/home/mysql/3306/my.cnf --user=mysql &
[root@web1 ~]# mysql -uroot -p -S /home/mysql/3306/mysql.sock
Enter password: 123qwe
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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>

  

posted @ 2018-09-10 16:41  一本正经的搞事情  阅读(1174)  评论(0编辑  收藏  举报