温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2021-11-06 10:45:18 来源:亿速云 阅读:315 作者:小新 栏目:MySQL数据库

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

从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)

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI