一、修改破解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>
