windows
修改配置文件
1、打开mysql配置文件my.ini, 找到 [mysqld],在下面添加:
skip-grant-tables
2、重启mysql服务
修改mysql密码
3、在终端使用root账号登陆,不需要输入密码直接回车
4、使用命令
mysql>update mysql.user set authentication_string=password(‘new_password’) where user=‘root’ and Host =‘localhost’
mysql>FLUSH PRIVILEGES;
5、注释第一步添加的内容
6、重启mysql服务,登陆
centos
修改配置文件
1、修改配置文件
vim /etc/my.cnf
在 [mysqld] 的段中加上一句:skip-grant-tables
修改mysql密码
2、重启 mysql 服务,systemctl restart mysqld
3、在输入 mysql -u root -p 输入回车即可进入 mysql 数据库


低版本修改:update user set Password = password(‘LDtianzhe.’) where user = ‘root’;
高版本修改:update user set authentication_string=password(‘new password’) where user = ‘root’;
注释第一步中添加的内容vim /etc/my.cnf,重启mysql服务
本文详细介绍了在Windows和CentOS操作系统中,当忘记MySQL密码时如何通过修改配置文件来重置root用户的密码。步骤包括:在my.ini或my.cnf配置文件中添加'skip-grant-tables',重启MySQL服务,然后使用SQL命令更新用户密码。对于不同MySQL版本,更新密码的SQL语句略有不同。最后,移除添加的配置并再次重启服务以完成密码更改。
401

被折叠的 条评论
为什么被折叠?



