mysql5.7.x安装以后,想修改随机生成的密码为简单容易记忆的密码,如root,123456等,这时候通过修改密码的几种方式都不行,出现密码不符合当前安全策略要求。为了解决这种问题,可以修改几个值,他们是关于密码验证的设置。
我们通过随机生成的密码,登录数据库,查看密码验证相关变量:
mysql> show variables like 'validate_password%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password_check_user_name | OFF |
| validate_password_dictionary_file | |
| validate_password_length | 8 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | MEDIUM |
| validate_password_special_char_count | 1 |
+--------------------------------------+--------+
7 rows in set (0.00 sec)
得到这样的结果,密码长度要求8位,验证策略是MEDIUM,就是长度,数字,大小写,特殊字符都得验证,因此出现如此所示的错误,就很正常了。我们可以修改validate_password_policy=0,这样就是只检查长度。另外我们觉着8位太长了,我们可以改为4。这样可以设置root密码为root。
经过如下修改,再次验证。
mysql> set global validate_password_policy=0; Query OK, 0 rows affected (0.00 sec) mysql> set global validate_password_length=4; Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'validate_password%'; +--------------------------------------+-------+ | Variable_name | Value | +--------------------------------------+-------+ | validate_password_check_user_name | OFF | | validate_password_dictionary_file | | | validate_password_length | 4 | | validate_password_mixed_case_count | 1 | | validate_password_number_count | 1 | | validate_password_policy | LOW | | validate_password_special_char_count | 1 | +--------------------------------------+-------+ 7 rows in set (0.00 sec)
我们再次设置密码,就可以成功的设置root,123456等这样简单的密码了。
mysql> set password=password('root'); Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> exit Bye [root@buejee log]# mysql -uroot -proot mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9 Server version: 5.7.20 MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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>
补充几点:
1、修改密码的四种方式
a)shell命令行下mysqladmin -uroot -poldpassword password newpassword
b)sql命令行下 set password = password(newpassword);
c)sql命令行下 update user set authentication_string=password(newpassword) where user='root';
d)sql命令行下 alter user root@'localhost' identified by 'newpassword';//使用随机密码登录,如果要使用数据,查看数据库等操作时mysql会推荐这种写法改变密码。
2、密码策略的三个值分别代表的含义
a)LOW(0)只检查长度。
b)MEDIUM(1)检查长度,数字,大小写,特殊字符。
c)STRONG(2)检查长度,数字,大小写,特殊字符字典文件。