詳解MySQL8.0 密碼過(guò)期策略
MySQL8.0.16開始,可以設(shè)置密碼的過(guò)期策略,今天針對(duì)這個(gè)小的知識(shí)點(diǎn)進(jìn)行展開。
1、手工設(shè)置單個(gè)密碼過(guò)期
MySQL8.0中,我們可以使用alter user這個(gè)命令來(lái)讓密碼過(guò)期。
首先我們創(chuàng)建賬號(hào)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 | +---+
這里我們讓它過(guò)期:
mysql> alter user yeyz@'127.0.0.1' password expire; Query OK, 0 rows affected (0.01 sec)
再來(lái)看連接:
[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. -- 提示我們通過(guò)--connect-expire-password命令來(lái)進(jìn)行連接,我們加上看看 [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的語(yǔ)法來(lái)修改密碼,然后再使用密碼。
當(dāng)然,除了手工設(shè)置密碼過(guò)期外,我們還可以設(shè)置密碼永不過(guò)期和指定過(guò)期時(shí)間:
-- 設(shè)置密碼永不過(guò)期 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è)置密碼過(guò)期天數(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è)置的時(shí)間。
2、設(shè)置全局密碼過(guò)期時(shí)間。
如果我們想讓所有的密碼都有過(guò)期時(shí)間,可以通過(guò)配置參數(shù)default_password_lifetime。它的默認(rèn)值為0,表示禁用自動(dòng)密碼過(guò)期。如果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ù)使用時(shí)間和可重復(fù)使用的間隔次數(shù)
注意,這里的可重復(fù)使用時(shí)間和可重復(fù)使用的間隔次數(shù)和過(guò)期時(shí)間的概念不一樣,過(guò)期時(shí)間指的是密碼到這個(gè)時(shí)間就過(guò)期了,就變成不可用了。而可重復(fù)使用指的是到指定時(shí)間才可以重復(fù)使用歷史密碼,或者密碼修改了指定的次數(shù)之后,才可以使用歷史密碼。
我們可以通過(guò)下面的方法來(lái)設(shè)置單個(gè)密碼可重復(fù)使用時(shí)間,或者可重復(fù)間隔次數(shù),其中:
過(guò)期時(shí)間表示多久之后,需要修改密碼;
過(guò)期次數(shù)表示每間隔多少次才可以設(shè)置重復(fù)密碼。
這兩個(gè)功能分別需要使用參數(shù) password_history 和 password_reuse_interval
我們來(lái)測(cè)試下password_history這個(gè)參數(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) -- 第二次修改,報(bào)錯(cuò) 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>
可以看到,一開始的時(shí)候,這個(gè)password_history參數(shù)設(shè)置為0,我們將它改為2,代表每執(zhí)行2次密碼設(shè)置動(dòng)作,才可以重復(fù)之前的密碼,也就是不允許本次修改的密碼和上次密碼一致。然后開始修改密碼為之前同樣的密碼'yeyz',第一次修改的時(shí)候成功了,第二次設(shè)置密碼的時(shí)候,直接報(bào)錯(cuò)了。
這種方式是通過(guò)系統(tǒng)變量的方式來(lái)設(shè)置密碼的有效次數(shù)的。
4、設(shè)置單個(gè)密碼可重復(fù)使用時(shí)間和可重復(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ù)使用或者每個(gè)5次才可以重復(fù)使用,取最嚴(yán)格的那個(gè)條件 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) -- 使用默認(rèn)的全局密碼可重復(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 密碼過(guò)期策略的詳細(xì)內(nèi)容,更多關(guān)于MySQL8.0密碼過(guò)期策略的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
手把手教你使用Navicat生成MySQL測(cè)試數(shù)據(jù)
Navicat是一套快速、可靠并價(jià)格相當(dāng)便宜的數(shù)據(jù)庫(kù)管理工具,專為簡(jiǎn)化數(shù)據(jù)庫(kù)的管理及降低系統(tǒng)管理成本而設(shè),下面這篇文章主要給大家介紹了關(guān)于使用Navicat生成MySQL測(cè)試數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2023-05-05Mysql修改datadir導(dǎo)致無(wú)法啟動(dòng)問(wèn)題解決方法
這篇文章主要介紹了Mysql修改datadir導(dǎo)致無(wú)法啟動(dòng)問(wèn)題解決方法,本文原因是SELINUX導(dǎo)致,用關(guān)閉SELINUX的方法解決了這個(gè)問(wèn)題,需要的朋友可以參考下2015-02-02查看修改mysql編碼方式讓它支持中文(gbk或者utf8)
MySQL的默認(rèn)編碼是Latin1,不支持中文,要支持中文需要把數(shù)據(jù)庫(kù)的默認(rèn)編碼修改為gbk或者utf8,真的是很麻煩啊,不過(guò)本文提供了詳細(xì)的修改教程,感興趣的你可不要走開啊,希望本文對(duì)你有所幫助2013-01-01