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

Olga ·
更新时间:2024-09-20
· 563 次阅读

一、更改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;

搞定。


作者:developerFBI



root密码 方法 root centos Mysql

需要 登录 后方可回复, 如果你还没有账号请 注册新账号
相关文章
Kamaria 2021-07-24
846