天天看點

mysql5.6忘記密碼的恢複方法

實驗環境:

    1、centos7.3

    2、mysql5.6.35

實驗描述:

    原mysql僅root賬号可以登入且有密碼保護,現在密碼已經忘記無法找回。今天的目标就是通過破解,重置mysql的root密碼。

實驗進行時:

    1、開始之前确定mysql不用密碼已經不能登入了

[root@c73 mysql]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@c73 mysql]#
[root@c73 ~]# mysql -u root -p654321 //用654321這個密碼也進不了。
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)      

    2、跳過授權進入mysql

[root@c73 mysql]# systemctl stop mysql //停止mysql
//由于我本身使用的是root賬号,mysql運作需要mysql賬号是以,增加了--user=mysql,
//這裡主要是使用--skip-grant-tables參數跳過授權表
[root@c73 mysql]# mysqld --user=mysql --skip-grant-tables &
[1] 6022
[root@c73 mysql]# 2017-04-11 09:36:41 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-04-11 09:36:41 0 [Note] mysqld (mysqld 5.6.35) starting as process 6022 ...
2017-04-11 09:36:41 6022 [Note] Plugin 'FEDERATED' is disabled.
2017-04-11 09:36:41 6022 [Note] InnoDB: Using atomics to ref count buffer pool pages
2017-04-11 09:36:41 6022 [Note] InnoDB: The InnoDB memory heap is disabled
2017-04-11 09:36:41 6022 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2017-04-11 09:36:41 6022 [Note] InnoDB: Memory barrier is not used
2017-04-11 09:36:41 6022 [Note] InnoDB: Compressed tables use zlib 1.2.3
2017-04-11 09:36:41 6022 [Note] InnoDB: Using Linux native AIO
2017-04-11 09:36:41 6022 [Note] InnoDB: Using CPU crc32 instructions
2017-04-11 09:36:41 6022 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2017-04-11 09:36:41 6022 [Note] InnoDB: Completed initialization of buffer pool
2017-04-11 09:36:41 6022 [Note] InnoDB: Highest supported file format is Barracuda.
2017-04-11 09:36:41 6022 [Note] InnoDB: 128 rollback segment(s) are active.
2017-04-11 09:36:41 6022 [Note] InnoDB: Waiting for purge to start
2017-04-11 09:36:42 6022 [Note] InnoDB: 5.6.35 started; log sequence number 1626007
2017-04-11 09:36:42 6022 [Note] Server hostname (bind-address): '*'; port: 3306
2017-04-11 09:36:42 6022 [Note] IPv6 is available.
2017-04-11 09:36:42 6022 [Note]   - '::' resolves to '::';
2017-04-11 09:36:42 6022 [Note] Server socket created on IP: '::'.
2017-04-11 09:36:42 6022 [Note] mysqld: ready for connections.
Version: '5.6.35'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MySQL Community Server (GPL)

[root@c73 mysql]# netstat -lnpt|grep 3306 //确定服務開啟成功
tcp6       0      0 :::3306                 :::*                    LISTEN      6022/mysqld         
[root@c73 mysql]# mysql  //無密碼進入mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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> //成功進入^_^      

    3、修改root密碼

mysql> set password for 'root'@'localhost' = password('654321');
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
// 在--skip-grant-tables模式下,以上修改密碼的方法是行不通了。
// 是以我們直接修改使用者表來更新密碼。
mysql> update mysql.user set password=password("654321") where user='root';
Query OK, 4 rows affected (0.00 sec) #更新成功了4條root使用者的密碼
Rows matched: 4  Changed: 4  Warnings: 0
// 最後别忘了重新整理
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit //退出
Bye      
[root@c73 ~]# ps aux|grep mysql
mysql     6022  0.0 11.6 1038816 452312 pts/0  Sl   09:36   0:00 mysqld --user=mysql --skip-grant-tables
root      6113  0.0  0.0 112664   968 pts/1    R+   10:00   0:00 grep --color=auto mysql
[root@c73 ~]# kill -9 6022 //結束掉原來的mysql服務
[root@c73 ~]# systemctl start mysql 以正常模式開啟mysql服務
[root@c73 ~]# ps aux|grep mysql
mysql     6132  0.0  0.0 113252  1588 ?        Ss   10:00   0:00 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
mysql     6310  1.8  2.8 698944 110984 ?       Sl   10:00   0:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
root      6337  0.0  0.0 112664   972 pts/1    R+   10:01   0:00 grep --color=auto mysql
[root@c73 ~]# mysql #測試不用密碼無法進入
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@c73 ~]# mysql -u root -p654321 #測試使用新密碼進入成功^_^
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.35 MySQL Community Server (GPL)

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

繼續閱讀