MySQL如何实现用户密码过期功能

47次阅读
没有评论

共计 1978 个字符,预计需要花费 5 分钟才能阅读完成。

这篇文章主要介绍了 MySQL 如何实现用户密码过期功能,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让丸趣 TV 小编带着大家一起了解一下。

从 MySQL 版本 5.6.6 版本起,添加了 password_expired 功能,它允许设置用户的过期时间。

这个特性已经添加到 mysql.user 数据表,但是它的默认值是”N”。可以使用 ALTER USER 语句来修改这个值。
例如:
mysql ALTER USER mdba@ localhost PASSWORD EXPIRE;
Query OK, 0 rows affected (0.04 sec)

在用户未设置新密码之前不能运行任何查询语句,而且会得到如下错误消息提示:
mysql show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

按照以下操作执行完后此用户的所有操作就又会被允许执行:

mysql  alter user mdba@localhost identified by Aisino123!
Query OK, 0 rows affected (0.03 sec)

mysql flush privileges;
Query OK, 0 rows affected (0.04 sec)

mysql show databases;
+——————–+
| Database           |
+——————–+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+——————–+
4 rows in set (0.00 sec)

在 MySQL 5.7.8 版开始用户管理方面添加了锁定 / 解锁用户账户的新特性
例如:

mysql alter user mdba@localhost account lock;
Query OK, 0 rows affected (0.04 sec)

重新登录发现被拒绝:
[root@localhost ~]# mysql -u mdba -p
Enter password:
ERROR 3118 (HY000): Access denied for user mdba @ localhost . Account is locked.

解锁后恢复正常:

mysql alter user mdba@localhost account unlock;
Query OK, 0 rows affected (0.03 sec)

[root@localhost ~]# mysql -u mdba -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 942539
Server version: 5.7.17-debug-log Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type help; or \h for help. Type \c to clear the current input statement.

mysql

从 MySQL 5.7.4 版开始,用户的密码过期时间这个特性得以改进,可以通过一个全局变量 default_password_lifetime 来设置密码过期的策略,
此全局变量可以设置一个全局的自动密码过期策略。
在 MySQL5.7 的配置文件中设置一个默认值,这会使得所有 MySQL 用户的密码过期时间都为 90 天,MySQL 会从启动时开始计算时间。
例如在 my.cnf 里添加:

[mysqld]
default_password_lifetime=90

这会使得所有 MySQL 用户的密码过期时间都为 90 天,MySQL 会从启动时开始计算时间。
如果要设置密码永不过期的全局策略,可以设置 default_password_lifetime=0,或者在命令行设置:
mysql SET GLOBAL default_password_lifetime = 0;
Query OK, 0 rows affected (0.00 sec)

感谢你能够认真阅读完这篇文章,希望丸趣 TV 小编分享的“MySQL 如何实现用户密码过期功能”这篇文章对大家有帮助,同时也希望大家多多支持丸趣 TV,关注丸趣 TV 行业资讯频道,更多相关知识等着你来学习!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-26发表,共计1978字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)