亲测有效!!解决MySQL8误删root账号无法登录的问题


前言

本人在做项目的过程中,不小心误删了本地MySQL的root账号,心急如焚,但是好在数据库的data都有备份,那想办法能找回账号成功登录就好了。下面是我亲测有效的一种办法,希望可以帮助到大家!!

本解决方法参考自:简书一文
如果遇到其他问题也可以在评论区互相交流,感谢小伙伴的大力支持!如果切实解决了你的问题的话,点个赞支持一下吧,希望可以高效解决大家问题。


1. 进入cmd ,停止MySQL服务 net stop mysql

方法1:可以打开服务(win+R),输入services.msc,进去之后找到MySQL服务,停止。
方法2:找到本地MySQL的bin目录下,以管理员身份运行cmd,输入以下命令:

Microsoft Windows [版本 10.0.14393]
(c) 2016 Microsoft Corporation。保留所有权利。

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

C:\Users\Administrator>

2. 输入: mysqld --console --skip-grant-tables --shared-memory

C:\Users\Administrator>mysqld --console --skip-grant-tables --shared-memory
2022-09-08T05:24:08.165351Z 0 [System] [MY-010116] [Server] D:\mysql\bin\mysqld.exe (mysqld 8.0.22) starting as process 6852
2022-09-08T05:24:08.167088Z 0 [Warning] [MY-013242] [Server] --character-set-server: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
2022-09-08T05:24:08.188808Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-09-08T05:24:09.406594Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-09-08T05:24:09.657486Z 0 [Warning] [MY-011311] [Server] Plugin mysqlx reported: 'All I/O interfaces are disabled, X Protocol won't be accessible'
2022-09-08T05:24:10.006858Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2022-09-08T05:24:10.008668Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2022-09-08T05:24:10.072437Z 0 [System] [MY-010931] [Server] D:\mysql\bin\mysqld.exe: ready for connections. Version: '8.0.22'  socket: ''  port: 0  MySQL Community Server - GPL.

3.另外开一个cmd,使用mysql直接无密登录 mysql -u root –p(在enter password时直接回车即可)

此处如果使用root失败便使用其他账号试试,例如:mysql -u mysql.sys -p,目的是先能够进入mysql的指令下,方便进行下一步操作。

C:\Users\Administrator>mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.22 MySQL Community Server - GPL

Copyright (c) 2000, 2020, 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。
找到安装目录下的my.ini配置文件,找到[mysqld],在这行下面加入如下语句:

skip-grant-tables

4.修改密码

此处应注意,要原样执行ALTER这句话,误删root的会新建root用户。
flush privileges的作用是刷新权限

mysql> flush privileges;

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
Query OK, 0 rows affected (0.07 sec)

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

5.试验用密码登录,发现登录成功


C:\Users\Administrator>mysql -u root -p
Enter password: ****
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.22 MySQL Community Server - GPL

Copyright (c) 2000, 2020, 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>
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

君知燕云归

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值