詳解MySQL8.0 密碼過期策略
MySQL8.0.16開始,可以設(shè)置密碼的過期策略,今天針對這個小的知識點進行展開。
1、手工設(shè)置單個密碼過期
MySQL8.0中,我們可以使用alter user這個命令來讓密碼過期。
首先我們創(chuàng)建賬號yeyz,密碼是yeyz
[root@VM-0-14-centos ~]# /usr/local/mysql-8.0.19-el7-x86_64/bin/mysql -uyeyz -pyeyz -h127.0.0.1 -P4306 -e "select 1" mysql: [Warning] Using a password on the command line interface can be insecure. +---+ | 1 | +---+ | 1 | +---+
這里我們讓它過期:
mysql> alter user yeyz@'127.0.0.1' password expire; Query OK, 0 rows affected (0.01 sec)
再來看連接:
[root@VM-0-14-centos ~]# /usr/local/mysql-8.0.19-el7-x86_64/bin/mysql -uyeyz -pyeyz -h127.0.0.1 -P4306 -e "select 1" mysql: [Warning] Using a password on the command line interface can be insecure. Please use --connect-expired-password option or invoke mysql in interactive mode. -- 提示我們通過--connect-expire-password命令來進行連接,我們加上看看 [root@VM-0-14-centos ~]# /usr/local/mysql-8.0.19-el7-x86_64/bin/mysql -uyeyz -pyeyz -h127.0.0.1 -P4306 --connect-expired-password -e "select 1" mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 1820 (HY000) at line 1: You must reset your password using ALTER USER statement before executing this statement. -- 這里提示我們先執(zhí)行alter user的語法來修改密碼,然后再使用密碼。
當然,除了手工設(shè)置密碼過期外,我們還可以設(shè)置密碼永不過期和指定過期時間:
-- 設(shè)置密碼永不過期 mysql> create user yeyz1@'127.0.0.1' identified with 'mysql_native_password' by 'yeyz1' password expire never; Query OK, 0 rows affected (0.01 sec) -- 設(shè)置密碼過期天數(shù)為指定天數(shù) mysql> create user yeyz2@'127.0.0.1' identified with 'mysql_native_password' by 'yeyz2' password expire interval 90 day; Query OK, 0 rows affected (0.01 sec)
如果我們想遵循全局密碼到期策略,則可以使用defalut關(guān)鍵字:
mysql> create user yeyz3@'127.0.0.1' identified with 'mysql_native_password' by 'yeyz3' password expire default; Query OK, 0 rows affected (0.01 sec)
這種情況下,將遵守參數(shù)default_password_lifetime設(shè)置的時間。
2、設(shè)置全局密碼過期時間。
如果我們想讓所有的密碼都有過期時間,可以通過配置參數(shù)default_password_lifetime。它的默認值為0,表示禁用自動密碼過期。如果default_password_lifetime的值為正整數(shù)N,則表示允許的密碼生存期,單位為天,因此必須每N天更改一次密碼。
mysql> show variables like '%lifetime%'; +---------------------------+-------+ | Variable_name | Value | +---------------------------+-------+ | default_password_lifetime | 0 | +---------------------------+-------+ 1 row in set (0.00 sec)
3、設(shè)置全局密碼可重復(fù)使用時間和可重復(fù)使用的間隔次數(shù)
注意,這里的可重復(fù)使用時間和可重復(fù)使用的間隔次數(shù)和過期時間的概念不一樣,過期時間指的是密碼到這個時間就過期了,就變成不可用了。而可重復(fù)使用指的是到指定時間才可以重復(fù)使用歷史密碼,或者密碼修改了指定的次數(shù)之后,才可以使用歷史密碼。
我們可以通過下面的方法來設(shè)置單個密碼可重復(fù)使用時間,或者可重復(fù)間隔次數(shù),其中:
過期時間表示多久之后,需要修改密碼;
過期次數(shù)表示每間隔多少次才可以設(shè)置重復(fù)密碼。
這兩個功能分別需要使用參數(shù) password_history 和 password_reuse_interval
我們來測試下password_history這個參數(shù):
mysql> alter user yeyz@'127.0.0.1' identified with 'mysql_native_password' by 'yeyz'; Query OK, 0 rows affected (0.01 sec) mysql> mysql> show variables like '%password_history%'; +------------------+-------+ | Variable_name | Value | +------------------+-------+ | password_history | 0 | +------------------+-------+ 1 row in set (0.00 sec) mysql> set global password_history=2; Query OK, 0 rows affected (0.00 sec) -- 第一次修改,成功 mysql> alter user yeyz@'127.0.0.1' identified with 'mysql_native_password' by 'yeyz'; Query OK, 0 rows affected (0.01 sec) -- 第二次修改,報錯 mysql> alter user yeyz@'127.0.0.1' identified with 'mysql_native_password' by 'yeyz'; ERROR 3638 (HY000): Cannot use these credentials for 'yeyz@127.0.0.1' because they contradict the password history policy mysql>
可以看到,一開始的時候,這個password_history參數(shù)設(shè)置為0,我們將它改為2,代表每執(zhí)行2次密碼設(shè)置動作,才可以重復(fù)之前的密碼,也就是不允許本次修改的密碼和上次密碼一致。然后開始修改密碼為之前同樣的密碼'yeyz',第一次修改的時候成功了,第二次設(shè)置密碼的時候,直接報錯了。
這種方式是通過系統(tǒng)變量的方式來設(shè)置密碼的有效次數(shù)的。
4、設(shè)置單個密碼可重復(fù)使用時間和可重復(fù)使用的間隔次數(shù)
-- 設(shè)置密碼為每間隔5次才可以重復(fù)使用 mysql> create user yeyz3@'127.0.0.1' identified with 'mysql_native_password' by 'yeyz3' password history 5; Query OK, 0 rows affected (0.01 sec) -- 設(shè)置密碼為每隔5天才可以重復(fù)使用 mysql> create user yeyz4@'127.0.0.1' identified with 'mysql_native_password' by 'yeyz4' password reuse interval 5 day; Query OK, 0 rows affected (0.01 sec) -- 設(shè)置密碼為每隔5天可以重復(fù)使用或者每個5次才可以重復(fù)使用,取最嚴格的那個條件 mysql> create user yeyz5@'127.0.0.1' identified with 'mysql_native_password' by 'yeyz5' password reuse interval 5 day password history 5; Query OK, 0 rows affected (0.01 sec) -- 使用默認的全局密碼可重復(fù)使用策略,也就是password history參數(shù)和password reuse interval參數(shù) mysql> create user yeyz6@'127.0.0.1' identified with 'mysql_native_password' by 'yeyz6' password reuse interval default password history default; Query OK, 0 rows affected (0.01 sec)
以上就是詳解MySQL8.0 密碼過期策略的詳細內(nèi)容,更多關(guān)于MySQL8.0密碼過期策略的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
mysql 8.0.12 安裝配置方法圖文教程(windows10)
這篇文章主要為大家詳細介紹了windows10下mysql 8.0.12 安裝配置方法圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08
MySql 快速插入千萬級大數(shù)據(jù)的方法示例
這篇文章主要介紹了MySql 快速插入千萬級大數(shù)據(jù)的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2019-08-08
Mysql主從復(fù)制(master-slave)實際操作案例
這篇文章主要介紹了Mysql主從復(fù)制(master-slave)實際操作案例,同時介紹了Mysql grant 用戶授權(quán)的相關(guān)內(nèi)容,需要的朋友可以參考下2014-06-06
MySQL中三種關(guān)聯(lián)查詢方式的簡單比較
這篇文章主要介紹了MySQL中三種關(guān)聯(lián)查詢方式的簡單比較,即ON和USING還有傳統(tǒng)的FROM...WHERE...,需要的朋友可以參考下2015-06-06
MySQL深分頁,limit 100000,10優(yōu)化方式
MySQL中深分頁查詢因需掃描大量數(shù)據(jù)行導(dǎo)致效率低下,優(yōu)化方法包括子查詢優(yōu)化、延遲關(guān)聯(lián)、標簽記錄法和使用between...and...等,通過減少回表次數(shù)和范圍掃描提升查詢性能,覆蓋索引幫助減少搜索次數(shù),提升性能2024-10-10

