MySQL----MySQL数据库忘记root用户登录密码的解决办法

【原文链接】

1 停止mysql服务

linux上通过如下命令停止mysql服务

systemctl stop mysqld

Windows通过【计算机】-右键-【管理】-【服务和应用程序】-【服务】,然后找到【MySQL5.7】,在上面右键-停止即可

2 修改配置跳过认证

编辑配置文件,windows系统为my.ini,linux系统为my.cnf,然后在mysqld下面增加如下内容

[mysqld]
skip-grant-tables

3 启动mysql服务

linux系统执行如下命令

systemctl start mysqld

Windows通过【计算机】-右键-【管理】-【服务和应用程序】-【服务】,然后找到【MySQL5.7】,在上面右键-启动即可

4 使用空密码登录root用户

此时使用root用户登录不需要密码了,如下

C:\Users\Administrator>mysql -uroot -P13306
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.37-log MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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>

4 重置root新密码

use mysql;
update user set authentication_string=password('新密码') where user='root';
flush privileges;

如下:

mysql> use mysql;
Database changed
mysql> update user set authentication_string=password("xxxxxxxxx") where user='root';
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.01 sec)

mysql

5 参照步骤1再次停止mysql服务

6 修改pei配置文件,将步骤2中配置的skip-grant-tables去掉

7 参照步骤3再次启动mysql服务

8 使用重置后密码登录

如下,使用新密码已经可以正常登录root用户了

C:\Users\Administrator>mysql -uroot -P13306 -p
Enter password: ***********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.37-log MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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>

猜你喜欢

转载自blog.csdn.net/redrose2100/article/details/125032845