詳解MySQL的用戶密碼過期功能

寫在前面

wamp中的數據庫爲mysql,適用於本文的操作。
xampp中的數據庫爲MariaDB,請參考 http://blog.csdn.net/xianda9133/article/details/72758272


在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 -u furrywall -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的用戶密碼過期功能的相關內容,希望對大家的學習有所幫助。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章