百木园-与人分享,
就是让自己快乐。

译文 | MySQL 8.0 密码管理策略(一)

作者:Sri Sakthivel

原文链接:https://www.percona.com/blog/enhanced-password-management-systems-in-mysql-8-part-1

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/mysql-security-excerpt/8.0/en/password-management.html

  • https://dev.mysql.com/doc/refman/8.0/en/password-management.html#random-password-generation

来源:https://www.cnblogs.com/radondb/p/15351916.html
图文来源于网络,如有侵权请联系删除。

未经允许不得转载:百木园 » 译文 | MySQL 8.0 密码管理策略(一)

相关推荐

  • 暂无文章