Windows下MySQL忘记root密码的两种办理办法

科技颠覆者  金牌会员 | 2024-8-27 05:58:46 | 来自手机 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 552|帖子 552|积分 1656

  1. 作者介绍:老苏,10余年DBA工作运维经验,擅长Oracle、MySQL、PG、Mongodb数据库运维(如安装迁移,性能优化、故障应急处理等)
  2. 公众号:老苏畅谈运维
  3. 欢迎关注本人公众号,更多精彩与您分享。
复制代码
方法1:
对于忘记MySQL root用户密码,常规的办理方法是启动的时候加 skip-grant-tables 选项,在绕过密码认证之后,进入MySQL数据库体系,以便进行修复或重置密码等操作。
在window下,怎么操作呢,接下来为您先容。
1、window下关闭MySQL相关服务
打开cmd窗口,我这里MySQL服务名称设置为了MySQL8031,所以这里是 net stop mysql8031


2、重新启动MySQL
用下面的命令启动MySQL服务
  1. C:\Users\Administrator>cd /d D:\MySQL8031\bin
  2. D:\MySQL8031\bin>mysqld --console --skip-grant-tables --shared-memory
复制代码
通过mysqld --console --skip-grant-tables --shared-memory 启动可以跳过密码验证

3、打开另外一个cmd窗口登录MySQL
  1. C:\Users\Administrator>cd /d D:\MySQL8031\bin
  2. D:\MySQL8031\bin>mysql -uroot
  3. Welcome to the MySQL monitor.  Commands end with ; or \g.
  4. Your MySQL connection id is 7
  5. Server version: 8.0.31 MySQL Community Server - GPL
  6. Copyright (c) 2000, 2022, Oracle and/or its affiliates.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. mysql>
复制代码
4、设置新的root密码
  1. update mysql.user set authentication_string='' where user='root';
  2. flush privileges;
  3. ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
复制代码

5、关闭上述终端,重新启动mysql
  1. net start mysql8031
复制代码

这样就可以用修改后的密码登录进去了
  1. C:\Users\Administrator>cd /d D:\MySQL8031\bin\
  2. D:\MySQL8031\bin>mysql -uroot -proot
  3. mysql: [Warning] Using a password on the command line interface can be insecure.
  4. Welcome to the MySQL monitor.  Commands end with ; or \g.
  5. Your MySQL connection id is 8
  6. Server version: 8.0.31 MySQL Community Server - GPL
  7. Copyright (c) 2000, 2022, Oracle and/or its affiliates.
  8. Oracle is a registered trademark of Oracle Corporation and/or its
  9. affiliates. Other names may be trademarks of their respective
  10. owners.
  11. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  12. mysql>
复制代码
方法2:
接下来,先容另外一种更简朴的方式。
新建一个newpass.txt文件(可以将它放在my.ini同目次)文件内容为:
  1. ALTER user root@'localhost' identified by 'root';
复制代码
关闭MySQL服务:
  1. net stop mysql8031
复制代码

用下面的命令启动MySQL服务:
D:\MySQL8031\bin\mysqld.exe --defaults-file=“D:\MySQL8031\my.ini” --init-file=“D:\MySQL8031\newpass.txt”
详细的my.ini文件和newpass.txt文件要改成实际的位置
命令执行后界面没有变化执行下一步。

按Ctrl+C终止运行,并重新启动Windows服务

关注我,学习更多的数据库知识


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

科技颠覆者

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表