共计 5850 个字符,预计需要花费 15 分钟才能阅读完成。
作者:Sri Sakthivel
原文链接:https://www.percona.com/blog/…
MySQL 8.0 在明码治理方面有很多改善,本文将介绍以下两个个性。
- 明码重用策略
- 生成随机明码
| 1 明码重用策略
该策略简略说,就是设置新密码时,能够限度禁止应用已经用过的明码。有以下两种策略:
- 历史明码 password_history
- 间隔时间 password_reuse_interval
1.1 历史明码
MySQL 官网手册形容:
If an account is restricted on the basis of number of password changes, a new password cannot be chosen from a specified number of the most recent passwords.
在试验环境中,创立用户并增加条件 password history 2
,历史明码中最近的两个不能被从新应用。
mysql> create user 'herc'@'localhost' identified by 'Percona@321' password history 2;
Query OK, 0 rows affected (0.02 sec)
mysql> select user, host, password_reuse_history from mysql.user where user='herc'\G
*************************** 1. row ***************************
user: herc
host: localhost
password_reuse_history: 2
1 row in set (0.00 sec)
MySQL 将在 mysql.password_history
表上记录明码更改的信息。
mysql> select * from mysql.password_history;
+-----------+------+----------------------------+------------------------------------------------------------------------+
| Host | User | Password_timestamp | Password |
+-----------+------+----------------------------+------------------------------------------------------------------------+
| localhost | herc | 2021-09-20 15:44:42.295778 | $A$005$=R:q'M(Kh#D];c~SdCLyluq2UVHFobjWOFTwn2JYVFDyI042sl56B7DCPSK5 |
+-----------+------+----------------------------+------------------------------------------------------------------------+
1 row in set (0.00 sec)
接下来咱们尝试更改用户 herc@localhost
的明码。
mysql> alter user 'herc'@'localhost' identified by 'MySQL@321';
Query OK, 0 rows affected (0.02 sec)
mysql> select * from mysql.password_history\G
*************************** 1. row ***************************
Host: localhost
User: herc
Password_timestamp: 2021-09-20 15:49:15.459018
CGeRQT31UUwtw194KOKGdNbgj3558VUB.dxcoS8r4IKpG8
*************************** 2. row ***************************
Host: localhost
User: herc
Password_timestamp: 2021-09-20 15:44:42.295778
Password: $A$005$=R:q'M(Kh#D];c~SdCLyluq2UVHFobjWOFTwn2JYVFDyI042sl56B7DCPSK5
2 rows in set (0.00 sec)
更改胜利后,查看 mysql.password_history
表,可见该表存有最近两个明码的信息。
再次更改该用户明码,且明码为创立用户时设置的明码值(Percona@321)。
mysql> alter user 'herc'@'localhost' identified by 'Percona@321';
ERROR 3638 (HY000): Cannot use these credentials for 'herc@localhost' because they contradict the password history policy
批改失败! 因为依据咱们设置的限度策略,不能重用在 mysql.password_policy
表中被记录的最近的两个明码。因而,如果想再次重复使用第一个明码,就不能让该明码呈现在 mysql.password_policy
表中。
再设置一个新密码后(第一个明码曾经不是最近的两个明码了),再尝试用第一个明码值(Percona@321)进行批改, 批改胜利!
mysql> alter user 'herc'@'localhost' identified by 'Herc@321';
Query OK, 0 rows affected (0.01 sec)
mysql> alter user 'herc'@'localhost' identified by 'Percona@321';
Query OK, 0 rows affected (0.02 sec)
也能够在启动时在全局配置 password_history
。
#vi my.cnf
[mysqld]
password_history=6
#set global
mysql> set global password_history=5;
Query OK, 0 rows affected (0.00 sec)
1.2 间隔时间
MySQL 官网手册形容:
If an account is restricted based on time elapsed, a new password cannot be chosen from passwords in the history that are newer than a specified number of days.
测试前,创立了用户 sri@localhost
,并设置用户明码可重用的工夫距离为五天。
mysql> create user 'sri'@'localhost' identified by 'Percona@321' password reuse interval 5 day;
Query OK, 0 rows affected (0.01 sec)
mysql> select user, host, password_reuse_time from mysql.user where user='sri'\G
*************************** 1. row ***************************
user: sri
host: localhost
password_reuse_time: 5
1 row in set (0.00 sec)
这意味着每个明码失效后,在五天内都不能再被反复设置。
mysql> select * from mysql.password_history where user='sri'\G
*************************** 1. row ***************************
Host: localhost
User: sri
Password_timestamp: 2021-09-20 16:09:27.918585
Password: $A$005$+B e3!C9&8m
eFRG~IqRWX4b6PtzLA8I4VsdYvWU3qRs/nip/QRhXXR5phT6
1 row in set (0.00 sec)
执行 ALTER 来更改明码。
mysql> alter user 'sri'@'localhost' identified by 'Herc@321';
Query OK, 0 rows affected (0.02 sec)
mysql> select * from mysql.password_history where user='sri'\G
*************************** 1. row ***************************
Host: localhost
User: sri
Password_timestamp: 2021-09-20 16:17:51.840483
Password: $A$005$~k7qp8.OP=^#e79qwtiYd7/cmCFLvHM7MHFbvfX2WlhXqzjmrN03gGZ4
*************************** 2. row ***************************
Host: localhost
User: sri
Password_timestamp: 2021-09-20 16:09:27.918585
Password: $A$005$+B e3!C9&8m
eFRG~IqRWX4b6PtzLA8I4VsdYvWU3qRs/nip/QRhXXR5phT6
2 rows in set (0.00 sec)
当初尝试再次设置为第一个明码。
mysql> alter user 'sri'@'localhost' identified by 'Percona@321';
ERROR 3638 (HY000): Cannot use these credentials for 'sri@localhost' because they contradict the password history policy
设置失败!
也能够在启动时在全局配置 password_reuse_interval
。
#vi my.cnf
[mysqld]
password_reuse_interval=365
#set global
mysql> set global password_reuse_interval=365;
Query OK, 0 rows affected (0.00 sec)
| 2 随机明码
从 MySQL 8.0.18 开始,MySQL 可能为用户帐户创立随机明码。这意味着能够不用调配指定明码。它反对以下语句:
- 创立用户
- 更改用户
- 设置明码
咱们须要应用 RANDOM PASSWORD
以防止批改明码时屏幕上有明文显示。例如:
mysql> create user 'sakthi'@'localhost' identified by random password;
+--------+-----------+----------------------+
| user | host | generated password |
+--------+-----------+----------------------+
| sakthi | localhost | .vZYy+<<BO7l1;vtIufH |
+--------+-----------+----------------------+
1 row in set (0.01 sec)
mysql> alter user 'sri'@'localhost' identified by random password;
+------+-----------+----------------------+
| user | host | generated password |
+------+-----------+----------------------+
| sri | localhost | 5wb>2[]q*jbDsFvlN-i_ |
+------+-----------+----------------------+
1 row in set (0.02 sec)
明码哈希值将存储在 mysql.user
表中。
mysql> select user, authentication_string from mysql.user where user in ('sakthi','sri')\G
*************************** 1. row ***************************
user: sakthi
authentication_string: $A$005$L`PYcedj%3tz*J>ioBP1.Rsrj7H8wtelqijvV0CFnXVnWLNIc/RZL0C06l4oA
*************************** 2. row ***************************
user: sri
authentication_string: $A$005$/k?aO&ap.#b=
^zt[E|x9q3w9uHn1oEumXUgnqNMH8xWo4xd/s26hTPKs1AbC2
2 rows in set (0.00 sec)
默认状况下,明码长度为 20 个字符。咱们能够应用变量 generated_random_password_length
定义明码长度,容许的长度范畴是 5 到 255。
mysql> select @@generated_random_password_length;
+------------------------------------+
| @@generated_random_password_length |
+------------------------------------+
| 20 |
+------------------------------------+
1 row in set (0.00 sec)
如果 validate_password
组件曾经装置,对随机明码策略不会有影响。
还有一些个性,将在下一篇文章中介绍。
参考
MySQL 手册:
- https://dev.mysql.com/doc/mys…
- https://dev.mysql.com/doc/ref…