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

作者:楊濤濤

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

本文來源:原創投稿

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


今天我們來繼續介紹 MySQL 8.0 的新密碼策略, 分別爲雙密碼策略和內置隨機密碼生成。
第一,雙密碼策略:

首先來解釋下什麼是雙密碼策略?雙密碼策略就是在日常運維中,需要定期更改指定用戶密碼,同時又需要舊密碼暫時保留一定時長的一種策略。其作用是延遲應用與數據庫之間的用戶新舊密碼對接時間,進而平滑應用的操作感知。可以在如下場景中使用:

在 MySQL 數據庫裏我們部署最多也是最成熟的架構:一主多從。比如說此架構做了讀寫分離,主負責處理前端的寫流量,讀負責處理前端的讀流量,爲了安全起見,需要定期對應用連接數據庫的用戶更改密碼。有了雙密碼機制,對用戶密碼的更改在應用端可以有一定的緩衝延遲,避免業務中斷風險以及開發人員的抱怨。應用端依然可以使用舊密碼來完成對數據庫的檢索,等待合適時機再使用管理員發來的新密碼檢索數據庫。
雙密碼機制包含主密碼與備密碼,當備密碼不再使用時,告知管理員丟棄備密碼,此時用戶的主密碼即是唯一密碼。
具體如何使用呢?用法如下:

管理員先創建一個新用戶 ytt ,密碼是 root_old ,完了更改他的密碼爲 root_new 。此時 root_new 即爲主密碼,而 root_old 即爲備密碼。

mysql:(none)>create user ytt identified by 'root_old';
Query OK, 0 rows affected, 2 warnings (0.24 sec)

mysql:(none)>alter user ytt identified by 'root_new' retain current password;
Query OK, 0 rows affected (0.17 sec)

接下來用戶 ytt 分別使用備密碼與主密碼連接 MySQL 並且執行一條簡單的 SQL 語句:

備密碼連接數據庫:

root@ytt-ubuntu:/home/ytt# mysql -h ytt-ubuntu -P 3306 -uytt -proot_old -e "select 'hello world'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------+
| hello world |
+-------------+
| hello world |
+-------------+

主密碼連接數據庫:

root@ytt-ubuntu:/home/ytt# mysql -h ytt-ubuntu -P 3306 -uytt -proot_new -e "select 'hello world'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------+
| hello world |
+-------------+
| hello world |
+-------------+
可以發現在管理員沒有丟棄舊密碼前,兩個密碼都能正常使用。

相關業務更改完成後,即可告知管理員丟棄備密碼:

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 27
Server version: 8.0.27 MySQL Community Server - GPL

...

mysql:(none)>alter user ytt discard old password;
Query OK, 0 rows affected (0.02 sec)

mysql:(none)>\q
Bye
雙密碼策略有以下需要注意的事項:
  1. 如果用戶本身已經有雙密碼策略,再次更改新密碼時沒有帶 retain current password 子句,那之前的主密碼被替換成新改的密碼,但是備密碼不會被替換。比如更改新密碼爲 root_new_new ,此時備密碼依然是 root_old ,並非之前的主密碼 root_new 。下面例子中輸入密碼 root_old 依然可以連接數據庫,而輸入密碼 root_new 則被數據庫拒絕連接:

 mysql:(none)>alter user ytt identified by 'root_new_new';
Query OK, 0 rows affected (0.16 sec)

root@ytt-ubuntu:/home/ytt# mysql -h ytt-ubuntu -u ytt -proot_old -e "select 'hello world'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------+
| hello world |
+-------------+
| hello world |
+-------------+
root@ytt-ubuntu:/home/ytt# mysql -h ytt-ubuntu -u ytt -proot_new -e "select 'hello world'"
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'ytt'@'ytt-ubuntu' (using password: YES)

還有一點需要注意的細節,如果不帶 retain current password  子句,並且更改新密碼爲空串,那麼主備密碼則會統一更改爲空串。下面例子中數據庫拒絕之前的備密碼連接:

mysql:(none)>alter user ytt identified by '';
Query OK, 0 rows affected (0.80 sec)

root@ytt-ubuntu:/home/ytt# mysql -h ytt-ubuntu -u ytt -proot_old -e "select 'hello world'"
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'ytt'@'ytt-ubuntu' (using password: YES)
root@ytt-ubuntu:/home/ytt# mysql -h ytt-ubuntu -u ytt -e "select 'hello world'"
+-------------+
| hello world |
+-------------+
| hello world |
+-------------+
  1. 新密碼爲空,不允許使用備用密碼。
 mysql:(none)>alter user ytt identified by '' retain current password;
ERROR 3895 (HY000): Current password can not be retained for user 'ytt'@'%' because new password is empty.
  1. 使用雙密碼策略時,不能更改用戶的認證插件。
 mysql:(none)>alter user ytt identified with sha256_password by 'root_new' retain current password;
ERROR 3894 (HY000): Current password can not be retained for user 'ytt'@'%' because authentication plugin is being changed.
第二,隨機密碼生成:

以往舊版本有生成隨機密碼的需求,在 MySQL 端無法直接設定,除非封裝用戶密碼設定邏輯,並且在代碼裏實現隨機密碼生成。比如用存儲過程,腳本等等。

MySQL 8.0 直接可以設置用戶隨機密碼

mysql:(none)>create user ytt_new identified by random password;
+---------+------+----------------------+-------------+
| user | host | generated password | auth_factor |
+---------+------+----------------------+-------------+
| ytt_new | % | >h<m3[bnigz%*f/SnLfp | 1 |
+---------+------+----------------------+-------------+
1 row in set (0.02 sec)

也可以用 set password 子句來設置隨機密碼

mysql:(none)>set password for ytt_new to random;
+---------+------+----------------------+-------------+
| user | host | generated password | auth_factor |
+---------+------+----------------------+-------------+
| ytt_new | % | 5wzZ+0[27cd_CW/]<ua, | 1 |
+---------+------+----------------------+-------------+
1 row in set (0.04 sec)

另外,隨機密碼的長度由參數 generated_random_password_length 調整,默認爲 20 個。

總結

雙密碼策略能讓應用和DBA溝通起來更加協調;隨機密碼設置能讓數據庫系統更加安全。


文章推薦:

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

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

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



社區近期動態




本文關鍵字 #MySQL 8.0 密碼策略 #  #MySQL 8.0 dual password # #MySQL 8.0 隨機密碼#
  點一下“閱讀原文”瞭解更多資訊

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

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