自学内容网 自学内容网

忘记 MySQL 密码怎么办:破解 root 账户密码

忘记 MySQL 密码怎么办:破解 root 账户密码

如果忘记密码导致无法登录 MySQL ,可以采用如下方法使 root 密码失效,然后不使用密码登录 MySQL,重新设置新密码。步骤如下:

1、修改 MySQL 配置文件

在 MySQL 配置文件的 [mysqld] 选项下加入如下代码:

[mysqld]
...
skip-grant-tables
...

然后重启 MySQL 服务:

C:\Users\Administrator>net stop mysql
MySQL 服务正在停止.
MySQL 服务已成功停止。


C:\Users\Administrator>net start mysql
MySQL 服务正在启动 .
MySQL 服务已经启动成功。
2、不使用密码登录 MySQL
C:\Users\Administrator>mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.48 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 数据库中的 user 表

-- 选择 mysql 数据库
mysql> use mysql
Database changed

-- 更新 user 表
mysql> select user,host,password from user;
+------+-----------+-------------------------------------------+
| user | host      | password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *97CFEED34A83BE1C8CA2D879A71A978A9B14568F |
| root | 127.0.0.1 |                                           |
| root | ::1       |                                           |
|      | localhost |                                           |
+------+-----------+-------------------------------------------+
4 rows in set (0.01 sec)

-- 刷新权限
mysql> update user set password=password('123456') where user='root' and host='localhost';
Query OK, 1 row affected (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 0

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

方法二:使用 set password 命令

-- 格式:set password for 用户名@localhost = password(‘新密码’);
mysql> set password for root@localhost = password('123456');
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
4、修改 MySQL 配置文件并重启 MySQL 服务

(1)删除 MySQL 配置文件中的如下内容

[mysqld]
...
# skip-grant-tables   # 删除此行
...

(2)重启 MySQL 服务

C:\Users\Administrator>net stop mysql
MySQL 服务正在停止.
MySQL 服务已成功停止。


C:\Users\Administrator>net start mysql
MySQL 服务正在启动 .
MySQL 服务已经启动成功。
5、使用新密码登录 MySQL
C:\Users\Administrator>mysql -uroot -p123456
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.48 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.

原文地址:https://blog.csdn.net/weixin_44377973/article/details/142681170

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!