MySQL 8:密码策略设定

在MySQL 8中,默认的密码策略进行了调节,这篇文章介绍调节和设定的方法。

环境说明

  • 操作系统版本: 10.15.2
liumiaocn:target liumiao$ sw_vers
ProductName:	Mac OS X
ProductVersion:	10.15.2
BuildVersion:	19C57
liumiaocn:target liumiao$ 
  • MySQL版本: 8.0.11
liumiaocn:target liumiao$ mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 46
Server version: 8.0.11 Homebrew

Copyright (c) 2000, 2018, 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> 
  • MySQL运行状态
liumiaocn:target liumiao$ mysql.server status
 SUCCESS! MySQL running (4633)
liumiaocn:target liumiao$ 

问题现象

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'liumiao123';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql>

原因

这是因为在MySQL 8中密码策略缺省被设定为 中等(MEDIUM),liumiao123不能通过这个标准。可以通过如下SQL进行密码策略的查询:

mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name                        | Value  |
+--------------------------------------+--------+
| validate_password_check_user_name    | ON     |
| 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.01 sec)

mysql>

对应方法

要么调高密码的复杂度,要么降低密码策略的标准,这里选择后者进行说明,执行如下SQL即可

mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)

mysql>

执行之后结果确认信息如下所示:

mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+-------+
| Variable_name                        | Value |
+--------------------------------------+-------+
| validate_password_check_user_name    | ON    |
| validate_password_dictionary_file    |       |
| validate_password_length             | 8     |
| 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)

mysql> 

结果确认

再次执行密码设定,可以发现已经能够设定成功了

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'liumiao123';
Query OK, 0 rows affected (0.03 sec)

mysql>

你可能感兴趣的:(#,MySQL)