Windows中忘记MySQL ROOT密码的解决方法

在需要ROOT身份登录MySQL但又忘记密码时,可以先已管理员身份运行cmd命令窗口,输入以下命令停止MySQL服务

net stop mysql

随后cd到MySQL安装目录下的bin目录下

D: //我的安装在D盘
cd D:\MySQL\bin

 使用跳过权限验证的方式起启动MySQL

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

 

可以使用 win+r 打开 services.msc (“服务”应用程序)查看MySQL是否在运行状态。 

保持这个cmd命令窗口,再次使用管理员身份运行一个cmd命令窗口,在新的cmd窗口中cd到MySQL安装目录

在新的命令窗口中输入以下命令来连接到MySQL服务器

mysql -u root

如果出现以下错误提示

ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost:3306' (10061)

可以使用以下命令查看MySQL默认的3306端口有没有被占用

netstat -ano | findstr :3306

如果没有任何显示,就需要修改下MySQL/bin文件夹中的my.ini文件,该文件大体内容如下:

[client]    #客户端设置,即客户端默认的连接参数
# 设置mysql客户端连接服务端时默认使用的端口
port=3306
 
#默认编码
default-character-set = utf8mb4
 
[mysql]    #客户端设置
#MySQL 提示符配置
#用户名@主机名+mysql版本号+数据库名
prompt=\\U@9527 8.0.36 [\\d]>\\__
 
# 设置mysql客户端默认字符集
default-character-set = utf8mb4
 
[mysqld]    #服务端基本设置
# 默认连接端口
port=3306
 
bind-address=127.0.0.1

# MySQL安装根目录的路径
basedir=D:\MySQL\
 
# MySQL服务器数据目录的路径
datadir=D:\MySQL\data
 
# 允许最大连接数
max_connections=200
 
# 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统
max_connect_errors=10
 
#服务端默认编码
character_set_server = utf8mb4
 
#在创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
 
# 配置时区
default-time_zone='+8:00'

成功登录后会显示

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
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.

root@@GGboy [(none)]> _

 随后依次输入以下SQL命令来重置root用户的密码

FLUSH PRIVILEGES; 
ALTER USER 'root'@'localhost' IDENTIFIED BY '新的密码';
FLUSH PRIVILEGES;

之后便可以在最初的cmd命令窗口输入 ctrl+c 退出 MySQL

再重启MySQL

net start mysql

随后输入以下命令,即可使用新的ROOT密码登录MySQL

mysql -u root -p

登录成功,密码修改完成,在 root@localhost@9527 8.0.36 [(none)]> _ 里输入 \q 即可退出 

Enter password: *****
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.

root@localhost@9527 8.0.36 [(none)]> _
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值