亲测有效!!解决MySQL8误删root账号无法登录的问题
- 前言
- 1. 进入cmd ,停止MySQL服务 net stop mysql
- 2. 输入: mysqld --console --skip-grant-tables --shared-memory
- 3.另外开一个cmd,使用mysql直接无密登录 mysql -u root –p(在enter password时直接回车即可)
- 4.修改密码
- 5.试验用密码登录,发现登录成功
前言
本人在做项目的过程中,不小心误删了本地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 - GPLCopyright (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 - GPLCopyright (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>