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

news/2025/2/12 8:06:56/

亲测有效!!解决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>

http://www.ppmy.cn/news/880844.html

相关文章

ElasticSearch简单介绍以及基本概念阐述

文章目录 一、ES是什么二、ES主要功能1、实时数据搜索和分析:2、分布式架构:3、全文搜索:4、实时数据分析:5、多种数据类型支持:6、实时监控和可视化:7、安全性和访问控制:8、多种集成和扩展&am…

第三方api对接怎么做?淘宝1688api接口怎么对接?

在今天的互联网上,第三方API对接是必不可少的。这种技术将不同的应用程序/服务连接在一起,创造了无限的可能性。 第三方api对接怎么做? 1、与支付公司签约 首先,通过正规的渠道,如支付公司官网或正规服务商&#xf…

HT合泰单片机入门教程(第五章 PWM)

文章目录 系列文章目录前言一、新建工程二、PWM相关了解1.PWM了解2.硬件电路了解 三、PWM实现1.代码实现 总结 系列文章目录 第一章 HT单片机环境搭建 第二章 点亮第一个LED灯 第三章 按键 第四章 定时器 第五章 PWM 第六章 时基中断 第七章 LCD 前言 PWM 信号把模拟信号…

S5PV210开发 -- 驱动开发相关硬件简介

如需转载请注明出处:https://blog.csdn.net/qq_29350001/article/details/78485355 到此,开发板已经是算跑起来了。系统移植部分稍后再讲。我们先举两个例子,让LED亮起来、蜂鸣器响起来。 一、LED亮起来 (1)LED 正负…

【STC单片机学习】第十一课:蜂鸣器

【朱老师课程总结 侵删】 第一部分、章节目录 1.11.1.蜂鸣器的原理​​ 1.11.2.让蜂鸣器响起来 1.11.3.用定时器控制蜂鸣器音调 1.11.4.蜂鸣器发出滴滴声 1.11.5.让蜂鸣器唱歌1 1.11.6.让蜂鸣器唱歌2 第二部分、章节介绍 1.11.1.…

stc89c51单片机音乐盒系统设计_基于单片机的音乐盒设计资料 含PCB与原理图,源程序和报告...

1 选题的背景 本次课设是应用单片机原理和控制理论设计音乐演奏控制器的硬件电路,并利用C语言进行程序设计。通过控制单片机内部的定时器来产生不同频率的方波,驱动喇叭发出不同的音调的音乐,在利用延迟来控制发音时间的长短。把乐谱转化成相应的定时常数就可以从发音设备中…

总务部竞赛部联合培训(硬件)

文章目录 一.按键(1)按键介绍(2)上拉输入和下拉输入(2)上拉输入和下拉输入(3)按键抖动和消抖硬件消抖:软件消抖: 二.蜂鸣器&#xff0…

10. 从0学ARM-基于Exynos4412的pwm详解

十一、PWM详解 一、什么是PWM PWM,英文名Pulse Width Modulation,是脉冲宽度调制缩写,它是通过对一系列脉冲的宽度进行调制,等效出所需要的波形(包含形状以及幅值),对模拟信号电平进行数字编码…