本篇继续介绍 MySQL 8.0 的新密码验证策略。
开发用户要求定期更改密码,并且密码不能与近期更改过的密码重叠,也即不能复用历史密码,这里限定历史密码个数为3;
运维用户同样要求定期更改密码,并且密码不能与某段时间内更改过的密码重叠,也即同样不能复用历史密码,这里限定时间段为一个礼拜。
第一,在配置文件里写上全局参数
先来实现开发用户的需求:保留历史密码个数为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.sock
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 33
Server 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 -proot123
mysql: [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 34
Server 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:(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)>\q
Bye复制
以用户 ytt_dba 登录数据库,并且改了五次密码:
root@ytt-ubuntu:/home/ytt# mysql -uytt_dba -hytt-ubuntu -proot123
mysql: [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 39
Server 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 policy
mysql:(none)>alter user ytt_dba identified by 'root456';
ERROR 3638 (HY000): Cannot use these credentials for 'ytt_dba@%' because they contradict the password history policy
mysql:(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 推出的历史密码验证策略是对用户密码安全机制的另外一个全新的改进,可以省去此类需求非数据库侧的繁琐实现。
新特性解读 | 来聊聊 MySQL8.0 的 json 模式校验
文章转载自戏说数据那点事,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
【MySQL 30周年庆】MySQL 8.0 OCP考试限时免费!教你免费领考券
墨天轮小教习
3078次阅读
2025-04-25 18:53:11
MySQL 30 周年庆!MySQL 8.4 认证免费考!这次是认真的。。。
严少安
864次阅读
2025-04-25 15:30:58
【活动】分享你的压箱底干货文档,三篇解锁进阶奖励!
墨天轮编辑部
510次阅读
2025-04-17 17:02:24
MySQL 9.3 正式 GA,我却大失所望,新特性亮点与隐忧并存?
JiekeXu
445次阅读
2025-04-15 23:49:58
3月“墨力原创作者计划”获奖名单公布
墨天轮编辑部
381次阅读
2025-04-15 14:48:05
MySQL 8.0 OCP 1Z0-908 考试解析指南(二)
JiekeXu
319次阅读
2025-04-30 17:37:37
SQL优化 - explain查看SQL执行计划(下)
金同学
288次阅读
2025-05-06 14:40:00
记录MySQL数据库的一些奇怪的迁移需求!
陈举超
286次阅读
2025-04-15 15:27:53
MySQL 8.0 OCP 1Z0-908 考试题解析指南
青年数据库学习互助会
274次阅读
2025-04-30 12:17:54
MySQL 30 周年庆!MySQL 8.4 认证免费考!这次是认真的。。。
数据库运维之道
272次阅读
2025-04-28 11:01:25