1. 程式人生 > >MySQL的使用者密碼過期功能

MySQL的使用者密碼過期功能

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使用者賬號的到期日期一個簡單例子:

?
1 mysql> ALTER USER 'testuser' @ 'localhost' PASSWORD EXPIRE;

一旦某個使用者的這個選項設定為”Y”,那麼這個使用者還是可以登陸到MySQL伺服器,但是在使用者未設定新密碼之前不能執行任何查詢語句,而且會得到如下錯誤訊息提示:

?
1 2 3 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 .

當用戶設定了新密碼後,此使用者的所有操作(根據使用者自身的許可權)會被允許執行:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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配置如下:

?
1 2 [mysqld] default_password_lifetime=90

如果要設定密碼永不過期的全域性策略,可以這樣:(注意這是預設值,配置檔案中可以不宣告)

?
1 2 [mysqld] default_password_lifetime=0

在MySQL執行時可以使用超級許可權修改此配置:

?
1 2 mysql> SET GLOBAL default_password_lifetime = 90; Query OK, 0 rows affected (0.00 sec)

還可以使用ALTER USER命令為每個具體的使用者賬戶單獨設定特定的值,它會自動覆蓋密碼過期的全域性策略。要注意ALTER USER語句的INTERVAL的單位是“天”。

?
1 ALTER USER ‘testuser '@‘localhost' PASSWORD EXPIRE INTERVAL 30 DAY ;

禁用密碼過期:

?
1 ALTER USER 'testuser' @ 'localhost' PASSWORD EXPIRE NEVER;

讓使用者使用預設的密碼過期全域性策略:

?
1 ALTER USER 'testuser' @ 'localhost' PASSWORD EXPIRE DEFAULT ;

從MySQL 5.7.6版開始,還可以使用ALTER USER語句修改使用者的密碼:

?
1 2 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.

下面建立一個帶賬戶鎖的使用者:

?
1 2 mysql> CREATE USER 'furrywall' @ 'localhost' IDENTIFIED BY '71m32ch4n6317' ACCOUNT LOCK; Query OK, 0 rows affected ( 0.00 sec)

如下所示,新建立的使用者在嘗試登陸時會得到一個ERROR 3118錯誤訊息提示:

?
1 2 3 $ mysql -ufurrywall -p Enter password: ERROR 3118 (HY000): Access denied for user 'furrywall' @ 'localhost' . Account is locked.

此時就需要使用ALTER USER … ACCOUNT UNLOCK語句進行解鎖了:

?
1 2 mysql>ALTER USER 'furrywall' @ 'localhost' ACCOUNT UNLOCK; Query OK, 0 rows affected ( 0.00 sec)

現在,這個使用者已經解鎖,可以登陸了:

?
1 2 3 4 5 6 7 8 9 10 11 $ 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>

還可以這樣鎖定使用者賬戶:

?
1 2 mysql> ALTER USER 'furrywall' @ 'localhost' ACCOUNT LOCK; Query OK, 0 rows affected ( 0.00 sec)

以上就是為大家介紹的MySQL的使用者密碼過期功能的相關內容,希望對大家的學習有所幫助。