新特性解讀 | MySQL 8.0 新密碼策略(中)

作者:楊濤濤

資深數據庫專家,專研 MySQL 十餘年。擅長 MySQL、PostgreSQL、MongoDB 等開源數據庫相關的備份恢復、SQL 調優、監控運維、高可用架構設計等。目前任職於愛可生,爲各大運營商及銀行金融企業提供 MySQL 相關技術支持、MySQL 相關課程培訓等工作。

本文來源:原創投稿

* 愛可生開源社區出品,原創內容未經授權不得隨意使用,轉載請聯繫小編並註明來源。


本篇繼續介紹 MySQL 8.0 的新密碼驗證策略。
假設有這樣的需求: 管理員創建了兩個用戶,職責分別是開發和運維,並且要求這兩個用戶必須滿足如下需求,
  1. 開發用戶要求定期更改密碼,並且密碼不能與近期更改過的密碼重疊,也即不能複用歷史密碼,這裏限定歷史密碼個數爲3;

  2. 運維用戶同樣要求定期更改密碼,並且密碼不能與某段時間內更改過的密碼重疊,也即同樣不能複用歷史密碼,這裏限定時間段爲一個禮拜。

以上兩種改密碼需求,在數據庫側暫時無法實現,只能拿個“小本子記住歷史密碼保留個數、歷史密碼保留天數,在用戶每次更改密碼前,先檢測小本子上有沒有和新密碼重疊的歷史密碼。
MySQL 8.0 對以上這兩種改密碼需求,直接從數據庫端實現,用戶可以扔掉“小本子”了。
我來分兩部分講解在 MySQL 8.0 版本里對以上改密碼需求的具體實現。
第一,在配置文件裏寫上全局參數
參數 password_history 表示最近使用的密碼保留次數;
參數 password_reuse_interval 表示最近使用的密碼保留天數。
先來實現開發用戶的需求:保留歷史密碼個數爲3。

管理員用戶登錄,設置全局參數:

mysql:(none)>set persist password_history=3;
Query OK, 0 rows affected (0.00 sec)

退出重連,創建用戶 ytt_dev :

root@ytt-ubuntu:/home/ytt# mysql -S /opt/mysql/mysqld.sock
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 33
Server version: 8.0.27 MySQL Community Server - GPL

...

mysql:(none)>create user ytt_dev identified by 'root123';
Query OK, 0 rows affected (0.15 sec)

退出連接,用戶 ytt_dev 重新連接數據庫,並且更改兩次密碼:

root@ytt-ubuntu:/home/ytt# mysql -uytt_dev -hytt-ubuntu -proot123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 34
Server version: 8.0.27 MySQL Community Server - GPL

...

mysql:(none)>
mysql:(none)>alter user ytt_dev identified by 'root456';
Query OK, 0 rows affected (0.03 sec)

mysql:(none)>alter user ytt_dev identified by 'root789';
Query OK, 0 rows affected (0.17 sec)

加上原始密碼,也就是3次密碼,再來更改一次密碼,此時不允許更改密碼,錯誤提示和密碼歷史策略衝突:

mysql:(none)>alter user ytt_dev identified by 'root123';
ERROR 3638 (HY000): Cannot use these credentials for 'ytt_dev@%' because they contradict the password history policy
接下來,選擇一個與歷史密碼不衝突的新密碼進行修改,此時密碼修改成功:
mysql:(none)>alter user ytt_dev identified by 'rootnew';
Query OK, 0 rows affected (0.04 sec)
再來實現運維用戶的需求:保留密碼天數爲7天。
同樣,管理員用戶登錄 MySQL ,並且設置全局參數:
mysql:(none)>set persist password_reuse_interval = 7;
Query OK, 0 rows affected (0.00 sec)

mysql:(none)>set persist password_history=default;
Query OK, 0 rows affected (0.00 sec)

退出重新連接,創建運維用戶 ytt_dba :

mysql:(none)>create user ytt_dba identified by 'root123';
Query OK, 0 rows affected (0.01 sec)

mysql:(none)>\q
Bye

以用戶 ytt_dba 登錄數據庫,並且改了五次密碼:

root@ytt-ubuntu:/home/ytt# mysql -uytt_dba -hytt-ubuntu -proot123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 39
Server version: 8.0.27 MySQL Community Server - GPL

...

mysql:(none)>alter user ytt_dba identified by 'root456';
Query OK, 0 rows affected (0.15 sec)

mysql:(none)>alter user ytt_dba identified by 'root789';
Query OK, 0 rows affected (0.08 sec)

mysql:(none)>alter user ytt_dba identified by 'root000';
Query OK, 0 rows affected (0.02 sec)

mysql:(none)>alter user ytt_dba identified by 'root888';
Query OK, 0 rows affected (0.02 sec)

mysql:(none)>alter user ytt_dba identified by 'root999';
Query OK, 0 rows affected (0.12 sec)

接下來驗證歷史密碼驗證策略,由於我們設置了密碼歷史保留天數,任何在設定時間內的歷史密碼,均不能作爲新密碼使用:MySQL 拒絕用戶更改密碼,錯誤提示與密碼歷史策略衝突:

mysql:(none)>alter user ytt_dba identified by 'root123';
ERROR 3638 (HY000): Cannot use these credentials for 'ytt_dba@%' because they contradict the password history policy
mysql:(none)>alter user ytt_dba identified by 'root456';
ERROR 3638 (HY000): Cannot use these credentials for 'ytt_dba@%' because they contradict the password history policy
mysql:(none)>

選擇一個非最近更改過的新密碼,改密成功:

mysql:(none)>alter user ytt_dba identified by 'rootnew';
Query OK, 0 rows affected (0.10 sec)

如果有一個用戶同時需要具備開發用戶和運維用戶的密碼限制條件,可以把兩個全局參數一起修改即可:歷史密碼保留天數爲7天、同時歷史密碼保留個數爲3次。

mysql:(none)>set persist password_reuse_interval = 7;
Query OK, 0 rows affected (0.00 sec)

mysql:(none)>set persist password_history=3;
Query OK, 0 rows affected (0.00 sec)
第二, 管理員在創建用戶或者更改用戶屬性時可以對單個用戶定義密碼驗證策略

把全局參數重置爲默認,也即關閉密碼驗證策略:

mysql:(none)>set persist password_reuse_interval = default;
Query OK, 0 rows affected (0.00 sec)

mysql:(none)>set persist password_history=default;
Query OK, 0 rows affected (0.00 sec)

管理員退出連接重新進入,創建兩個用戶 ytt_dev1 和 ytt_dba1 :

mysql:(none)>create user ytt_dev1 identified by 'root123';
Query OK, 0 rows affected (0.04 sec)

mysql:(none)>create user ytt_dba1 identified by 'root123';
Query OK, 0 rows affected (0.02 sec)

更改兩個用戶的密碼歷史保留策略:

mysql:(none)>alter user ytt_dev1 password history 3;
Query OK, 0 rows affected (0.01 sec)

mysql:(none)>alter user ytt_dba1 password reuse interval 7 day;
Query OK, 0 rows affected (0.02 sec)

檢索 mysql.user 表,看看是否更改成功:

mysql:(none)>select user,password_reuse_history,password_reuse_time from mysql.user where password_reuse_history is not null or password_reuse_time is not null;
+----------+------------------------+---------------------+
| user | password_reuse_history | password_reuse_time |
+----------+------------------------+---------------------+
| ytt_dba1 | NULL | 7 |
| ytt_dev1 | 3 | NULL |
+----------+------------------------+---------------------+
2 rows in set (0.00 sec)
具體驗證方法類似全局參數設置部分,此處省略。

總結:

MySQL 8.0 推出的歷史密碼驗證策略是對用戶密碼安全機制的另外一個全新的改進,可以省去此類需求非數據庫側的繁瑣實現。


文章推薦:

新特性解讀 | MySQL 8.0 新密碼策略(上)

新特性解讀 | 來聊聊 MySQL8.0 的 json 模式校驗

技術分享 | TiDB 對大事務的簡單拆分



社區近期動態




本文關鍵字 #mysql 8.0密碼驗證策略 #  #mysql 密碼安全 # #MySQL 密碼保留衝突驗證#
  點一下“閱讀原文”瞭解更多資訊

本文分享自微信公衆號 - 愛可生開源社區(ActiontechOSS)。
如有侵權,請聯繫 [email protected] 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。

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