【Mysql】忘记Root密码后如何不影响数据进行重置密码

方法一:通用方法--启动时跳过权限表

1> 停止数据库

以管理员方式打开cmd!!

C:\Users\Administrator>net stop mysql

MySQL 服务正在停止..

MySQL 服务已成功停止。

2> 启动时跳过权限表

mysqld --console --skip-grant-tables --shared-memory

C:\Users\Administrator>mysqld --console --skip-grant-tables --shared-memory

注意:不要关闭终端

3> 重开新的终端(再开一个终端),登录并设置新密码

PS C:\Users\Administrator> mysql -uroot -p

Enter password: 直接敲回车键

mysql> flush privileges;

mysql> alter user root@localhost identified by '123456'; //重置密码

4> 退出MySQL,关闭第一个终端,正常启动服务。

mysql> \q

PS C:\Users\Administrator> net start mysql

MySQL 服务正在启动 ..

MySQL 服务已经启动成功。

PS C:\Users\Administrator> mysql -uroot -p123456

mysql: [Warning] Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 8

Server version: 8.0.36 MySQL Community Server - GPL

Copyright (c) 2000, 2024, 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> \q

Bye

方法二:参考官方文档

1> 停止服务

PS C:\Users\Administrator> net stop mysql

MySQL 服务正在停止.

MySQL 服务已成功停止。

2> 创建一个文本文件到mysql文件夹里,保存为 mysql_init.txt ,内容如下:

ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';

3> 启动同时使用上一步文件修改密码

要求管理员身份打开cmd来运行命令,暂时不要关闭终端:

C:\Users\Administrator>mysqld --init-file= F:\mysql\mysql-8.0.36-winx64\mysql_init.txt

(具体路径复制粘贴)

4> 打开另一个终端,登录测试

PS C:\Users\Administrator> mysql -uroot -p123456

mysql: [Warning] Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 9

Server version: 8.0.36 MySQL Community Server - GPL

Copyright (c) 2000, 2024, 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> \q

Bye

5> 关闭第一个终端,正常启动服务。

C:\Users\Administrator>net start mysql

MySQL 服务正在启动 ..

MySQL 服务已经启动成功。

C:\Users\Administrator>mysql -uroot -p123456

mysql: [Warning] Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 8

Server version: 8.0.36 MySQL Community Server - GPL

Copyright (c) 2000, 2024, 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> \q

Bye

6> 删除文件 mysql_init.txt

相关推荐
掘金-我是哪吒5 分钟前
分布式微服务系统架构第126集:集群,数据库扩展,多节点分布,分库,分表,分片,分表,运维
运维·数据库·分布式·微服务·系统架构
wangbing112518 分钟前
window server 2012安装sql server2008 r2
数据库
码上飞扬20 分钟前
深入解析MySQL联合查询(UNION):案例与实战技巧
数据库·mysql
Leo.yuan26 分钟前
数据分析怎么做?高效的数据分析方法有哪些?
大数据·数据库·信息可视化·数据挖掘·数据分析
zm1 小时前
网络编程epoll和udp
服务器·网络·数据库
野犬寒鸦1 小时前
Linux常用命令详解(下):打包压缩、文本编辑与查找命令
linux·运维·服务器·数据库·后端·github
Ultipa2 小时前
回答 | 图形数据库neo4j社区版可以应用小型企业嘛?
数据库·neo4j·图数据库
charlie1145141912 小时前
逐步理解Qt信号与槽机制
数据库·qt
alpha xu2 小时前
Qwen智能体qwen_agent与Assistant功能初探
数据库·人工智能·python·oracle·智能体·千问agent
大模型最新论文速读3 小时前
在Text-to-SQL任务中应用过程奖励模型
数据库·人工智能·sql·深度学习·语言模型·自然语言处理