CentOS7下忘记MySQL数据库root密码与密码过期

本文介绍在Linux环境下如何通过禁用权限表来重置MySQL的root密码,并提供了在遇到连接错误和未知列问题时的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 以root用户登录linux,修改/etc/my.cnf
vim /etc/my.cnf

在[mysqld]的段中加上一句:skip-grant-tables

[mysqld] 

datadir=/var/lib/mysql 
socket=/var/lib/mysql/mysql.sock 
skip-grant-tables 

这里写图片描述


  • 退出保存,重新启动mysqld
service mysqld start
或
systemctl start mysqld

  • 登录并修改MySQL的root密码

命令如下:

//进入 mysq lbin路径
[root@iZ8vb3n2dl2w360w65xtf3Z bin]# pwd
/usr/local/mysql/bin

[root@iZ8vb3n2dl2w360w65xtf3Z bin]# ./mysql -uroot -p
Enter password:

//直接回车
use mysql;

修改密码


update user set password = password('new-password') where user = 'root' ; 

or

update user set authentication_string= password('new-password') where user = 'root' ; 

  • 刷新权限
flush privileges ;

  • 还原/etc/my.cnf

  • 重启mysql服务


其中在重新登录mysql时,你可能遇到如下问题:

问题一:

ERROR 2002 (HY000): Can’t connect to local MySQL server 
through socket ‘/var/lib/mysql/mysql.sock’ (2)

然后去找这个文件,发现/var/lib/mysql/路径下没有mysql.sock文件。而该文件存在于/tmp/mysql.sock !

解决方法:

① 修改my.cnf

[mysqld]

socket = /tmp/mysql.sock

② 软链

ln -s /tmp/mysql.sock  /var/lib/mysql/mysql.sock

问题二:

ERROR 1054 (42S22): Unknown column 'password' in 'field list'

使用如下方式修改密码:

update user set authentication_string= password('new-password') where user = 'root' ; 

问题三:

ERROR 1862 (HY000): Your password has expired. To log in you must change it using a client 
that supports expired passwords.

解决过程如下:

① 修改my.cnf,将skip-grant-tables放开,重启服务,登录mysql;

这里写图片描述


查看用户root信息:

select * from user where user = 'root'\G;

这里写图片描述


② 使用如下命令修改password_expired

use mysql;

update user set password_expired='N' where user='root';

flush privileges;

这里写图片描述


如果是windows下,参考博文:windows环境下mysql忘记密码如何重置

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流烟默

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值