MYSQL5.7设置账号密码复杂度、密码有效期、账号锁定等策略
一、設(shè)置密碼復(fù)雜度
①[root@localhost tmp]# mysql -uroot -p
②.測(cè)試修改密碼
mysql>alter user 'root'@'localhost' identified by 'cnbi123';
③.查看一下當(dāng)前修改后的密碼策略,如果沒有需要啟動(dòng)安全插件
mysql> SHOW VARIABLES LIKE "%password%";
④.啟動(dòng)安全插件
首先打開/etc/my.cnf,然后在[mysqld]的下方加入如下代碼:
plugin-load-add=validate_password.so
validate-password=FORCE_PLUS_PERMANENT
重啟mysqld服務(wù)
[root@localhost tmp]# systemctl restart mysqld.service
⑤.登錄
[root@localhost tmp]# mysql -uroot -p
⑥.查看一下當(dāng)前修改后的密碼策略
mysql> SHOW VARIABLES LIKE "%password%";
⑦.設(shè)置強(qiáng)密碼策略,啟動(dòng)插件默認(rèn)是強(qiáng)密碼,下面是說明參數(shù)不用設(shè)置
參數(shù)說明:
validate_password_policy值
| Policy | Tests Performed | |
| 0?or?LOW | Length | |
| 1?or?MEDIUM | Length; numeric, lowercase/uppercase, and special characters | |
| 2?or?STRONG | Length; numeric, lowercase/uppercase, and special characters; dictionary file |
默認(rèn)是1,即MEDIUM,所以剛開始設(shè)置的密碼必須符合長(zhǎng)度,且必須含有數(shù)字,小寫或大寫字母,特殊字符。
如果不想設(shè)置那么復(fù)雜,比如指向設(shè)置root密碼為1234,設(shè)置方式:
首先,修改validate_password_policy參數(shù)的值
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
這樣,判斷密碼的標(biāo)準(zhǔn)就基于密碼的長(zhǎng)度了。這個(gè)由validate_password_length參數(shù)來決定。
mysql> select @@validate_password_length;
+----------------------------+
| @@validate_password_length |
+----------------------------+
| ?????????????????????????8 |
+----------------------------+
1 row in set (0.00 sec)
validate_password_length參數(shù)默認(rèn)為8,它有最小值的限制,最小值為:
validate_password_number_count
+ validate_password_special_char_count
+ (2 * validate_password_mixed_case_count)
其中:
validate_password_number_count
#指定了密碼中數(shù)據(jù)的長(zhǎng)度,
validate_password_special_char_count
#指定了密碼中特殊字符的長(zhǎng)度,
validate_password_mixed_case_count
#指定了密碼中大小字母的長(zhǎng)度。
這些參數(shù),默認(rèn)值均為1,所以validate_password_length最小值為4,如果你顯性指定validate_password_length的值小于4,盡管不會(huì)報(bào)錯(cuò),但validate_password_length的值將設(shè)為4。如下所示:
mysql> select @@validate_password_length;
+----------------------------+
| @@validate_password_length |
+----------------------------+
| ?????????????????????????8 |
+----------------------------+
1 row in set (0.00 sec)
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
mysql> select @@validate_password_length;
+----------------------------+
| @@validate_password_length |
+----------------------------+
| ?????????????????????????4 |
+----------------------------+
1 row in set (0.00 sec)
如果修改了validate_password_number_count,validate_password_special_char_count,validate_password_mixed_case_count中任何一個(gè)值,則validate_password_length將進(jìn)行動(dòng)態(tài)修改。
mysql> select @@validate_password_length;
+----------------------------+
| @@validate_password_length |
+----------------------------+
| ?????????????????????????4 |
+----------------------------+
1 row in set (0.00 sec)
mysql> select @@validate_password_mixed_case_count;
+--------------------------------------+
| @@validate_password_mixed_case_count |
+--------------------------------------+
| ???????????????????????????????????1 |
+--------------------------------------+
1 row in set (0.00 sec)
mysql> set global validate_password_mixed_case_count=2;
Query OK, 0 rows affected (0.00 sec)
mysql> select @@validate_password_mixed_case_count;
+--------------------------------------+
| @@validate_password_mixed_case_count |
+--------------------------------------+
| ???????????????????????????????????2 |
+--------------------------------------+
1 row in set (0.00 sec)
mysql> select @@validate_password_length;
+----------------------------+
| @@validate_password_length |
+----------------------------+
| ?????????????????????????6 |
+----------------------------+
1 row in set (0.00 sec)
二、設(shè)置密碼90天過期
#設(shè)置全局密碼過期時(shí)間default_password_lifetime,單位為天
①首先打開/etc/my.cnf,然后在[mysqld]的下方加入如下代碼:?
[mysqld]
default_password_lifetime=90
②重啟mysqld服務(wù)
[root@localhost tmp]# systemctl restart mysqld.service
③登錄mysql查看
mysql> SHOW VARIABLES LIKE "%password%";
#default_password_lifetime=0 時(shí)默認(rèn)密碼用不過期
除全局配置外,也可以創(chuàng)建用戶時(shí)指定密碼過期時(shí)間
①#創(chuàng)建用戶test_passwd并設(shè)置密碼過期時(shí)間為90天
mysql> CREATE USER 'test_passwd'@'localhost' identified by 'Atest_passwd123' PASSWORD EXPIRE INTERVAL 90 DAY;
mysql> select user,host,password_last_changed,password_lifetime from mysql.user where user='test_passwd';
②#創(chuàng)建用戶test_passwd_never并設(shè)置密碼用不過期
mysql> CREATE USER 'test_passwd2'@'localhost' identified by 'Atest_passwd123' PASSWORD EXPIRE NEVER;
mysql> select user,host,password_last_changed,password_lifetime from mysql.user where user='test_passwd2';
#創(chuàng)建用戶test_passwd_default并設(shè)置密碼過期時(shí)間遵循系統(tǒng)默認(rèn)值
mysql> CREATE USER 'test_passwd_default'@'localhost' identified by 'Atest_passwd123' PASSWORD EXPIRE DEFAULT;
mysql> select user,host,password_last_changed,password_lifetime from mysql.user where user='test_passwd_default';
三、設(shè)置登錄失敗處理功能,失敗登錄5次鎖定5分鐘
設(shè)置方法:
①登錄
[root@localhost tmp]# mysql -uroot -p
②輸入一下命令,安裝插件
mysql> install plugin CONNECTION_CONTROL soname 'connection_control.so';
mysql> install plugin CONNECTION_CONTROL_FAILED_LOGIN_ATTEMPTS soname 'connection_control.so';
查看:
mysql> show variables like '%connection_control%';
③修改my.cnf文件
vi /etc/my.cnf
在文件中,我們?cè)黾尤缦聝尚?/p>
connection-control-failed-connections-threshold=5 ??#登陸失敗次數(shù)限制
connection-control-min-connection-delay=300000 ???#限制重試時(shí)間,此處為毫秒,注意按需求換算,此處為5分鐘
④重啟mysqld服務(wù)
[root@localhost tmp]# systemctl restart mysqld.service
⑤重新登錄數(shù)據(jù)庫(kù),查看是否生效
mysql> show variables like '%connection_control%';
?
總結(jié)
以上是生活随笔為你收集整理的MYSQL5.7设置账号密码复杂度、密码有效期、账号锁定等策略的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 键盘录入两个整数,分别赋值给int变量x
- 下一篇: string数据库