CentOS7下忘记MySQL数据库root密码与密码过期
程序员文章站
2022-03-09 11:07:24
...
- 以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密码
命令如下:
/usr/bin/mysql(或直接mysql)
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;
上一篇: 数据库的单表查询,熟悉SQL语句