作者:杨涛涛

资深数据库专家,专研 MySQL 十余年。善于 MySQL、PostgreSQL、MongoDB 等开源数据库相干的备份复原、SQL 调优、监控运维、高可用架构设计等。目前任职于爱可生,为各大运营商及银行金融企业提供 MySQL 相干技术支持、MySQL 相干课程培训等工作。

本文起源:原创投稿

*爱可生开源社区出品,原创内容未经受权不得随便应用,转载请分割小编并注明起源。


本篇持续介绍 MySQL 8.0 的新密码验证策略。

假如有这样的需要: 管理员别离创立了一个开发用户与运维用户,并且要求这两个用户必须满足如下需要,

  1. 开发用户要求定期更改明码,并且明码不能与近期更改过的明码重叠,也即不能复用历史明码,这里限定历史明码个数为3;
  2. 运维用户同样要求定期更改明码,并且明码不能与某段时间内更改过的明码重叠,也即同样不能复用历史明码,这里限定时间段为一个礼拜。

以上两种改明码需要,在数据库侧临时无奈实现,只能拿个”小本子记住历史明码保留个数、历史明码保留天数“,在用户每次更改明码前,先检测小本子上有没有和新密码重叠的历史明码。

MySQL 8.0 对以上这两种改明码需要,间接从数据库端实现,用户能够扔掉“小本子”了。

我来分两局部解说在 MySQL 8.0 版本里对以上改明码需要的具体实现。

第一,在配置文件里写上全局参数

参数 password_history 示意最近应用的明码保留次数;

参数 password_reuse_interval 示意最近应用的明码保留天数。

先来实现开发用户的需要:保留历史明码个数为3。

管理员用户登录,设置全局参数:

mysql:(none)>set persist password_history=3;Query OK, 0 rows affected (0.00 sec)

退出重连,创立用户 ytt_dev :

root@ytt-ubuntu:/home/ytt# mysql -S /opt/mysql/mysqld.sockWelcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 33Server version: 8.0.27 MySQL Community Server - GPL...mysql:(none)>create user ytt_dev identified by 'root123';Query OK, 0 rows affected (0.15 sec)

退出连贯,用户 ytt_dev 从新连贯数据库,并且更改两次明码:

root@ytt-ubuntu:/home/ytt# mysql -uytt_dev -hytt-ubuntu -proot123mysql: [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 34Server version: 8.0.27 MySQL Community Server - GPL...mysql:(none)>mysql:(none)>alter user ytt_dev identified by 'root456';Query OK, 0 rows affected (0.03 sec)mysql:(none)>alter user ytt_dev identified by 'root789';Query OK, 0 rows affected (0.17 sec)

加上原始明码,也就是3次明码,再来更改一次明码,此时不容许更改明码,谬误提醒和明码历史策略抵触:

mysql:(none)>alter user ytt_dev identified by 'root123';ERROR 3638 (HY000): Cannot use these credentials for 'ytt_dev@%' because they contradict the password history policy

接下来,抉择一个与历史明码不抵触的新密码进行批改,此时明码批改胜利:

mysql:(none)>alter user ytt_dev identified by 'rootnew';Query OK, 0 rows affected (0.04 sec)
再来实现运维用户的需要:保留明码天数为7天。

同样,管理员用户登录 MySQL ,并且设置全局参数:

mysql:(none)>set persist password_reuse_interval = 7;Query OK, 0 rows affected (0.00 sec)mysql:(none)>set persist password_history=default;Query OK, 0 rows affected (0.00 sec)

退出从新连贯,创立运维用户 ytt_dba :

mysql:(none)>create user ytt_dba identified by 'root123';Query OK, 0 rows affected (0.01 sec)mysql:(none)>\qBye

以用户 ytt_dba 登录数据库,并且改了五次明码:

root@ytt-ubuntu:/home/ytt# mysql -uytt_dba -hytt-ubuntu -proot123mysql: [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 39Server version: 8.0.27 MySQL Community Server - GPL...mysql:(none)>alter user ytt_dba identified by 'root456';Query OK, 0 rows affected (0.15 sec)mysql:(none)>alter user ytt_dba identified by 'root789';Query OK, 0 rows affected (0.08 sec)mysql:(none)>alter user ytt_dba identified by 'root000';Query OK, 0 rows affected (0.02 sec)mysql:(none)>alter user ytt_dba identified by 'root888';Query OK, 0 rows affected (0.02 sec)mysql:(none)>alter user ytt_dba identified by 'root999';Query OK, 0 rows affected (0.12 sec)

接下来验证历史明码验证策略,因为咱们设置了明码历史保留天数,任何在设定工夫内的历史明码,均不能作为新密码应用:MySQL 回绝用户更改明码,谬误提醒与明码历史策略抵触:

mysql:(none)>alter user ytt_dba identified by 'root123';ERROR 3638 (HY000): Cannot use these credentials for 'ytt_dba@%' because they contradict the password history policymysql:(none)>alter user ytt_dba identified by 'root456';ERROR 3638 (HY000): Cannot use these credentials for 'ytt_dba@%' because they contradict the password history policymysql:(none)>

抉择一个非最近更改过的新密码,改密胜利:

mysql:(none)>alter user ytt_dba identified by 'rootnew';Query OK, 0 rows affected (0.10 sec)

如果有一个用户同时须要具备开发用户和运维用户的明码限度条件,能够把两个全局参数一起批改即可:历史明码保留天数为7天、同时历史明码保留个数为3次。

mysql:(none)>set persist password_reuse_interval = 7;Query OK, 0 rows affected (0.00 sec)mysql:(none)>set persist password_history=3;Query OK, 0 rows affected (0.00 sec)
第二, 管理员在创立用户或者更改用户属性时能够对单个用户定义明码验证策略

把全局参数重置为默认,也即敞开明码验证策略:

mysql:(none)>set persist password_reuse_interval = default;Query OK, 0 rows affected (0.00 sec)mysql:(none)>set persist password_history=default;Query OK, 0 rows affected (0.00 sec)

管理员退出连贯从新进入,创立两个用户 ytt_dev1 和 ytt_dba1 :

mysql:(none)>create user ytt_dev1 identified by 'root123';Query OK, 0 rows affected (0.04 sec)mysql:(none)>create user ytt_dba1 identified by 'root123';Query OK, 0 rows affected (0.02 sec)

更改两个用户的明码历史保留策略:

mysql:(none)>alter user ytt_dev1 password history 3;Query OK, 0 rows affected (0.01 sec)mysql:(none)>alter user ytt_dba1 password reuse interval 7 day;Query OK, 0 rows affected (0.02 sec)

检索 mysql.user 表,看看是否更改胜利:

mysql:(none)>select user,password_reuse_history,password_reuse_time from mysql.user where password_reuse_history is not null or password_reuse_time is not null;+----------+------------------------+---------------------+| user     | password_reuse_history | password_reuse_time |+----------+------------------------+---------------------+| ytt_dba1 |                   NULL |                   7 || ytt_dev1 |                      3 |                NULL |+----------+------------------------+---------------------+2 rows in set (0.00 sec)

具体验证办法相似全局参数设置局部,此处省略。

总结:

MySQL 8.0 推出的历史明码验证策略是对用户明码平安机制的另外一个全新的改良,能够省去此类需要非数据侧的繁琐实现。