当前位置:   article > 正文

数据库译文 | MySQL 8.0 密码管理策略(一)_mysql的密码$a$005是什么意思

mysql的密码$a$005是什么意思

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

MySQL 8.0 在密码管理方面有很多改善,本文将介绍以下两个特性。

  • 密码重用策略
  • 生成随机密码

| 1 密码重用策略

该策略简单说,就是设置新密码时,可以限制禁止使用曾经用过的密码。有以下两种策略:

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,历史密码中最近的两个不能被重新使用。

  1. mysql> create user 'herc'@'localhost' identified by 'Percona@321' password history 2;
  2. Query OK, 0 rows affected (0.02 sec)
  3. mysql> select user, host, password_reuse_history from mysql.user where user='herc'\G
  4. *************************** 1. row ***************************
  5. user: herc
  6. host: localhost
  7. password_reuse_history: 2
  8. 1 row in set (0.00 sec)

MySQL 将在 mysql.password_history 表上记录密码更改的信息。

  1. mysql> select * from mysql.password_history;
  2. +-----------+------+----------------------------+------------------------------------------------------------------------+
  3. | Host | User | Password_timestamp | Password |
  4. +-----------+------+----------------------------+------------------------------------------------------------------------+
  5. | localhost | herc | 2021-09-20 15:44:42.295778 | $A$005$=R:q'M(Kh#D];c~SdCLyluq2UVHFobjWOFTwn2JYVFDyI042sl56B7DCPSK5 |
  6. +-----------+------+----------------------------+------------------------------------------------------------------------+
  7. 1 row in set (0.00 sec)

接下来我们尝试更改用户 herc@localhost 的密码。

  1. mysql> alter user 'herc'@'localhost' identified by 'MySQL@321';
  2. Query OK, 0 rows affected (0.02 sec)
  3. mysql> select * from mysql.password_history\G
  4. *************************** 1. row ***************************
  5. Host: localhost
  6. User: herc
  7. Password_timestamp: 2021-09-20 15:49:15.459018
  8. CGeRQT31UUwtw194KOKGdNbgj3558VUB.dxcoS8r4IKpG8
  9. *************************** 2. row ***************************
  10. Host: localhost
  11. User: herc
  12. Password_timestamp: 2021-09-20 15:44:42.295778
  13. Password: $A$005$=R:q'M(Kh#D];c~SdCLyluq2UVHFobjWOFTwn2JYVFDyI042sl56B7DCPSK5
  14. 2 rows in set (0.00 sec)

更改成功后,查看 mysql.password_history 表,可见该表存有最近两个密码的信息。

再次更改该用户密码,且密码为创建用户时设置的密码值(Percona@321)。

  1. mysql> alter user 'herc'@'localhost' identified by 'Percona@321';
  2. 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)进行修改,修改成功!

  1. mysql> alter user 'herc'@'localhost' identified by 'Herc@321';
  2. Query OK, 0 rows affected (0.01 sec)
  3. mysql> alter user 'herc'@'localhost' identified by 'Percona@321';
  4. Query OK, 0 rows affected (0.02 sec)

也可以在启动时在全局配置 password_history

  1. #vi my.cnf
  2. [mysqld]
  3. password_history=6
  4. #set global
  5. mysql> set global password_history=5;
  6. 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,并设置用户密码可重用的时间间隔为五天。

  1. mysql> create user 'sri'@'localhost' identified by 'Percona@321' password reuse interval 5 day;
  2. Query OK, 0 rows affected (0.01 sec)
  3. mysql> select user, host, password_reuse_time from mysql.user where user='sri'\G
  4. *************************** 1. row ***************************
  5. user: sri
  6. host: localhost
  7. password_reuse_time: 5
  8. 1 row in set (0.00 sec)

这意味着每个密码生效后,在五天内都不能再被重复设置。

  1. mysql> select * from mysql.password_history where user='sri'\G
  2. *************************** 1. row ***************************
  3. Host: localhost
  4. User: sri
  5. Password_timestamp: 2021-09-20 16:09:27.918585
  6. Password: $A$005$+B e3!C9&8m
  7. eFRG~IqRWX4b6PtzLA8I4VsdYvWU3qRs/nip/QRhXXR5phT6
  8. 1 row in set (0.00 sec)

执行 ALTER 来更改密码。

  1. mysql> alter user 'sri'@'localhost' identified by 'Herc@321';
  2. Query OK, 0 rows affected (0.02 sec)
  3. mysql> select * from mysql.password_history where user='sri'\G
  4. *************************** 1. row ***************************
  5. Host: localhost
  6. User: sri
  7. Password_timestamp: 2021-09-20 16:17:51.840483
  8. Password: $A$005$~k7qp8.OP=^#e79qwtiYd7/cmCFLvHM7MHFbvfX2WlhXqzjmrN03gGZ4
  9. *************************** 2. row ***************************
  10. Host: localhost
  11. User: sri
  12. Password_timestamp: 2021-09-20 16:09:27.918585
  13. Password: $A$005$+B e3!C9&8m
  14. eFRG~IqRWX4b6PtzLA8I4VsdYvWU3qRs/nip/QRhXXR5phT6
  15. 2 rows in set (0.00 sec)

现在尝试再次设置为第一个密码。

  1. mysql> alter user 'sri'@'localhost' identified by 'Percona@321';
  2. ERROR 3638 (HY000): Cannot use these credentials for 'sri@localhost' because they contradict the password history policy

设置失败!

也可以在启动时在全局配置 password_reuse_interval

  1. #vi my.cnf
  2. [mysqld]
  3. password_reuse_interval=365
  4. #set global
  5. mysql> set global password_reuse_interval=365;
  6. Query OK, 0 rows affected (0.00 sec)

| 2 随机密码

从 MySQL 8.0.18 开始,MySQL 能够为用户帐户创建随机密码。这意味着可以不必分配指定密码。它支持以下语句:

  • 创建用户
  • 更改用户
  • 设置密码

我们需要使用 RANDOM PASSWORD 以避免修改密码时屏幕上有明文显示。例如:

  1. mysql> create user 'sakthi'@'localhost' identified by random password;
  2. +--------+-----------+----------------------+
  3. | user | host | generated password |
  4. +--------+-----------+----------------------+
  5. | sakthi | localhost | .vZYy+< alter user 'sri'@'localhost' identified by random password;
  6. +------+-----------+----------------------+
  7. | user | host | generated password |
  8. +------+-----------+----------------------+
  9. | sri | localhost | 5wb>2[]q*jbDsFvlN-i_ |
  10. +------+-----------+----------------------+
  11. 1 row in set (0.02 sec)

密码哈希值将存储在 mysql.user 表中。

  1. mysql> select user, authentication_string from mysql.user where user in ('sakthi','sri')\G
  2. *************************** 1. row ***************************
  3. user: sakthi
  4. authentication_string: $A$005$L`PYcedj%3tz*J>ioBP1.Rsrj7H8wtelqijvV0CFnXVnWLNIc/RZL0C06l4oA
  5. *************************** 2. row ***************************
  6. user: sri
  7. authentication_string: $A$005$/k?aO&ap.#b=
  8. ^zt[E|x9q3w9uHn1oEumXUgnqNMH8xWo4xd/s26hTPKs1AbC2
  9. 2 rows in set (0.00 sec)

默认情况下,密码长度为 20 个字符。我们可以使用变量 generated_random_password_length 定义密码长度,允许的长度范围是 5 到 255。

  1. mysql> select @@generated_random_password_length;
  2. +------------------------------------+
  3. | @@generated_random_password_length |
  4. +------------------------------------+
  5. | 20 |
  6. +------------------------------------+
  7. 1 row in set (0.00 sec)

如果 validate_password 组件已经安装,对随机密码策略不会有影响。

还有一些特性,将在下一篇文章中介绍。

 

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号