当前位置: 移动技术网 > IT编程>数据库>Mysql > 详解MySQL的用户密码过期功能

详解MySQL的用户密码过期功能

2017年12月12日  | 移动技术网IT编程  | 我要评论

payment card industry,即支付卡行业,pci行业表示借记卡、信用卡、预付卡、电子钱包、atm和pos卡及相关的业务。
pci dss,即pci数据安全标准(payment card industry data security standard)是由pci安全标准委员会制定,旨在使国际上采用一致的数据安全措施。

pci dss标准要求用户每隔90天必须更改他们的密码。那么mysql数据库该怎样适应这个情况?幸运的是,在mysql版本5.6.6版本起,添加了password_expired功能,它允许设置用户的过期时间。

这个特性已经添加到mysql.user数据表,但是它的默认值是”n”。可以使用alter user语句来修改这个值。

下面是关于如何设置mysql用户账号的到期日期一个简单例子:

mysql> alter user 'testuser'@'localhost' password expire;

一旦某个用户的这个选项设置为”y”,那么这个用户还是可以登陆到mysql服务器,但是在用户未设置新密码之前不能运行任何查询语句,而且会得到如下错误消息提示:

mysql> show databases;
error 1820 (hy000): you must set password before executing this statement
keep in mind that this does not affect any current connections the account has open.

当用户设置了新密码后,此用户的所有操作(根据用户自身的权限)会被允许执行:

mysql> set password=password('mechipoderranen');
query ok, 0 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| database      |
+--------------------+
| information_schema |
| data        |
| logs        |
| mysql       |
| performance_schema |
| test        |
+--------------------+
6 rows in set (0.00 sec)
mysql>

dba可以通过cron定时器任务来设置mysql用户的密码过期时间。

从mysql 5.7.4版开始,用户的密码过期时间这个特性得以改进,可以通过一个全局变量default_password_lifetime来设置密码过期的策略,此全局变量可以设置一个全局的自动密码过期策略。

用法示例:
可以在mysql的配置文件中设置一个默认值,这会使得所有mysql用户的密码过期时间都为90天,mysql会从启动时开始计算时间。my.cnf配置如下:

[mysqld]
default_password_lifetime=90

如果要设置密码永不过期的全局策略,可以这样:(注意这是默认值,配置文件中可以不声明)

[mysqld]
default_password_lifetime=0

在mysql运行时可以使用超级权限修改此配置:

mysql> set global default_password_lifetime = 90;
query ok, 0 rows affected (0.00 sec)

还可以使用alter user命令为每个具体的用户账户单独设置特定的值,它会自动覆盖密码过期的全局策略。要注意alter user语句的interval的单位是“天”。

alter user ‘testuser'@‘localhost' password expire interval 30 day;

禁用密码过期:

alter user 'testuser'@'localhost' password expire never;

让用户使用默认的密码过期全局策略:

alter user 'testuser'@'localhost' password expire default;

从mysql 5.7.6版开始,还可以使用alter user语句修改用户的密码:

mysql> alter user user() identified by '637h1m27h36r33k';
query ok, 0 rows affected (0.00 sec)

后记

在mysql 5.7.8版开始用户管理方面添加了锁定/解锁用户账户的新特性, related to user management is locking/unlocking user accounts when create user, or at a later time running the alter user statement.

下面创建一个带账户锁的用户:

mysql> create user 'furrywall'@'localhost' identified by '71m32ch4n6317' account lock;
query ok, 0 rows affected (0.00 sec)

如下所示,新创建的用户在尝试登陆时会得到一个error 3118错误消息提示:

$ mysql -ufurrywall -p
enter password:
error 3118 (hy000): access denied for user 'furrywall'@'localhost'. account is locked.

此时就需要使用alter user … account unlock语句进行解锁了:

mysql>alter user 'furrywall'@'localhost' account unlock;
query ok, 0 rows affected (0.00 sec)

现在,这个用户已经解锁,可以登陆了:

$ mysql -ufurrywall -p
enter password:
welcome to the mysql monitor. commands end with ; or g.
your mysql connection id is 17
server version: 5.7.8-rc mysql community server (gpl)
copyright (c) 2000, 2015, 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> alter user 'furrywall'@'localhost' account lock;
query ok, 0 rows affected (0.00 sec)

以上就是为大家介绍的mysql的用户密码过期功能的相关内容,希望对大家的学习有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网