Centos7.5 MySQL5.7忘记root密码的解决方法

一、更改Mysql的主配置文件,my.cnf配置文件,在[mysqld]下添加skip-grant-tables

[root@zhong ~]# vim /etc/my.cnf
[mysqld]
skip-grant-tables

二、重启mysql服务,这边我用的源码安装的,yum源安装的可以用:systemctl restart mysql

[root@zhong ~]# vim /etc/my.cnf
[mysqld]
skip-grant-tables

保存.按Esc键,再按键盘的两个键Shift+!,然后删除冒号(:)后面的字符,输入wq就可以保存成功了。

三、接下来用root登录,修改密码。

[root@iZm5efrcnefgfjyg34v91qZ shangcheng]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.29

mysql> update mysql.user set authentication_string=password('1234567') where user='root';
Query OK, 1 row affected, 1 warning (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 1

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> quit

注意:最后记得将Mysql主配置文件中skip-grant-tables去掉

然后重启一下mysql。输入命令:

systemctl restart mysqld.service

四、使用root登录,输入刚才的密码。执行如下操作报错:

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

有这个提示是因为root的密码初化后在重新修改下密码才能进行其他操作。我在网上搜索到的用这alter修改语句,结果不行。我换了另一种方式进行如下操作:

修改validate_password_policy参数的值为0表示

set global validate_password_policy=0;

密码默认长度为8,由于我自定义的密码长度为11位,所以要设置这个长度为11。

set global validate_password_length=11;

更新密码命令:

SET PASSWORD = PASSWORD('tts198200..');

最后执行:

flush privileges;

 

搞定。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值