【MySQL解惑笔记】忘记MySQL数据库密码

破解MySQL密码

一、MySQL5.7.5之前

只要有系统root密码就可以破解:
[root@host-131 ~]# vim /etc/my.cnf               //在配置文件中加入如下内容
[mysqld]
skip-grant-tables

[root@host-131 ~]# systemctl restart mysqld           //重启MySQL服务
[root@host-131 ~]# mysql
mysql> update mysql.user set password=password("Yanglt456.") where user="root" and host="localhost";    //设置密码
mysql> flush privileges;                     //刷新                          
mysql> q

[root@host-131 ~]# vim /etc/my.cnf                          //注释掉下边内容,或者直接删除
[mysqld]
#skip-grant-table
[root@host-131 ~]#systemctl restart mysqld           //重启服务

一、MySQL5.7.6之后

[root@host-131 ~]# vim /etc/my.cnf
[mysqld]
skip-grant-tables
mysql> select user,host,authentication_string from mysql.user;
+---------------+-----------+-------------------------------------------+
| user          | host      | authentication_string                     |
+---------------+-----------+-------------------------------------------+
| root          | localhost | *8F59EEA84BC6AA6A57ECD4C0377518281DADC1BA |
| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys     | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
+---------------+-----------+-------------------------------------------+
3 rows in set (0.01 sec)

mysql> desc mysql.user;
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field                  | Type                              | Null | Key | Default               | Extra |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host                   | char(60)                          | NO   | PRI |                       |       |
| User                   | char(32)                          | NO   | PRI |                       |       |
| authentication_string  | text                              | YES  |     | NULL                  |       |
| password_expired       | enum('N','Y')                     | NO   |     | N                     |       |
| password_last_changed  | timestamp                         | YES  |     | NULL                  |       |
| password_lifetime      | smallint(5) unsigned              | YES  |     | NULL                  |       |
| account_locked         | enum('N','Y')                     | NO   |     | N                     |       |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
45 rows in set (0.01 sec)

mysql> update mysql.user set authentication_string=password("Yanglt456.") where user="root";
mysql> flush privileges;
mysql> q

[root@host-131 ~]# vim /etc/my.cnf
[mysqld]
#skip-grant-tables  //注释掉
[root@host-131 ~]# systemctl restart mysqld            //重启mysql服务
[root@host-131 ~]# mysql -p"Yanglt456."                //密码已改变
原文地址:https://www.cnblogs.com/yangleitao/p/9109607.html