科技颠覆者 发表于 2024-8-27 05:58:46

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

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

https://i-blog.csdnimg.cn/direct/25eb4946582448a6abd954cf9c7106df.png
2、重新启动MySQL
用下面的命令启动MySQL服务
C:\Users\Administrator>cd /d D:\MySQL8031\bin

D:\MySQL8031\bin>mysqld --console --skip-grant-tables --shared-memory
通过mysqld --console --skip-grant-tables --shared-memory 启动可以跳过密码验证
https://i-blog.csdnimg.cn/direct/9b21bfadb10140859aadc6a761a4812b.png
3、打开另外一个cmd窗口登录MySQL
C:\Users\Administrator>cd /d D:\MySQL8031\bin

D:\MySQL8031\bin>mysql -uroot
Welcome to the MySQL monitor.Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 8.0.31 MySQL Community Server - GPL

Copyright (c) 2000, 2022, 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>
4、设置新的root密码
update mysql.user set authentication_string='' where user='root';
flush privileges;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
https://i-blog.csdnimg.cn/direct/cd7aff30af68436dad3fe59c847189fb.png
5、关闭上述终端,重新启动mysql
net start mysql8031
https://i-blog.csdnimg.cn/direct/e1ab91e805ed46c4a353adc18e741e7d.png
这样就可以用修改后的密码登录进去了
C:\Users\Administrator>cd /d D:\MySQL8031\bin\

D:\MySQL8031\bin>mysql -uroot -proot
mysql: 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.31 MySQL Community Server - GPL

Copyright (c) 2000, 2022, 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>
方法2:
接下来,先容另外一种更简朴的方式。
新建一个newpass.txt文件(可以将它放在my.ini同目次)文件内容为:
ALTER user root@'localhost' identified by 'root';
关闭MySQL服务:
net stop mysql8031
https://i-blog.csdnimg.cn/direct/1e2cd6ecd25b47ba8aedf3a5909cf611.png
用下面的命令启动MySQL服务:
D:\MySQL8031\bin\mysqld.exe --defaults-file=“D:\MySQL8031\my.ini” --init-file=“D:\MySQL8031\newpass.txt”
详细的my.ini文件和newpass.txt文件要改成实际的位置
命令执行后界面没有变化执行下一步。
https://i-blog.csdnimg.cn/direct/523c525dfaa9456abd0a45a83555a041.png
按Ctrl+C终止运行,并重新启动Windows服务
https://i-blog.csdnimg.cn/direct/1be8813aa2434e0587e252d6cc66ec47.png
关注我,学习更多的数据库知识
https://i-blog.csdnimg.cn/direct/a99c60c300df4146b3cc7dd4b843ce88.png

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: Windows下MySQL忘记root密码的两种办理办法