详解MySQL的用户密码过期功能
作者:资深架构师 时间:2024-01-21 01:29:40
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)
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
CGArt®2008 贺岁刊电子杂志玉鼠闹春
![](https://img.aspxhome.com/file/UploadPic/20082/15/200821511345421s.jpg)
Python使用OpenPyXL处理Excel表格
![](https://img.aspxhome.com/file/2023/1/87791_0s.png)
Python Collatz序列实现过程解析
Python有序查找算法之二分法实例分析
![](https://img.aspxhome.com/file/2023/8/85378_0s.jpg)
Mysql慢查询优化方法及优化原则
此数据库没有有效所有者,因此无法安装数据库关系图支持对象
Dreamweaver技巧十二招
Javascript学习第一季 三
Python自动扫雷实现方法
caffe binaryproto 与 npy相互转换的实例讲解
mysql插入数据INSERT INTO SET的优势
mysql中数据库覆盖导入的几种方式总结
![](https://img.aspxhome.com/file/2023/1/103131_0s.png)
在ubuntu中重置mysql服务器root密码的方法
numpy ndarray 按条件筛选数组,关联筛选的例子
iPhone应用设计趋势[译]
python 的numpy库中的mean()函数用法介绍
基于pytorch的lstm参数使用详解
![](https://img.aspxhome.com/file/2023/9/87709_0s.jpg)
MySql中特殊运算符的使用方法总结
![](https://img.aspxhome.com/file/2023/0/114980_0s.jpg)
Python split() 函数拆分字符串将字符串转化为列的方法
python学习实操案例(二)
![](https://img.aspxhome.com/file/2023/8/112738_0s.png)