Linux运维笔记
我们一直在努力

MySQL 5.7 忘记root密码和重置密码的方法

1、登录MySQL服务器,修改配置文件,添加skip-grant-tables跳过密码验证

vim /etc/my.cnf

[mysqld]
character_set_server = utf8
skip-grant-tables

2、重启MySQL之后就没有不用密码直接登录了

service mysqld restart

输入mysql直接可以登录

[root@JD ~]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@JD ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.17 Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql>

3、设定重新修改密码

mysql> update mysql.user set authentication_string=password(‘66666′) where user=’root’;
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1

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

4、退出MySQL并把my.cnf中的skip-grant-tables去掉,然后重启MySQL

 

报错:修改后即可正常登录MySQL了,但是因为版本和设定不同可能会出现以下报错:

ERROR 1820 (HY000): You must reset your password using ALTER USER statement;
有可能提示需要再次修改密码

SET PASSWORD = PASSWORD(‘123456’);

ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
这个是告诉你,根据MySQL密码安全策略,你的密码不安全,说白了,就是太简单了,可以通过以下参数来把密码规则改掉

set global validate_password_policy=0; 校验级别最低,只校验密码长度,只要长度跟validate_password_length一样即可,默认长度是8位。可以通过:
set global validate_password_mixed_case_count=2;

再修改密码即可

SET PASSWORD = PASSWORD(‘123456’);

赞(0) 打赏

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址