詳解MySQL的使用者密碼過期功能

2021-08-01 15:49:59 字數 3631 閱讀 3275

wamp中的資料庫為mysql,適用於本文的操作。

xampp中的資料庫為mariadb,請參考

在mysql版本5.6.6版本起,新增了password_expired功能,它允許設定使用者的過期時間。

這個特性已經新增到mysql.user資料表,但是它的預設值是」n」。可以使用alter user語句來修改這個值。

下面是關於如何設定mysql使用者賬號的到期日期乙個簡單例子:

mysql> alter user 'testuser'@'localhost' password expire;
error 1820 (hy000): you must set password before executing this statement

keep in mind that this does

not affect any current connections the account has open.

當使用者設定了新密碼後,此使用者的所有操作(根據使用者自身的許可權)會被允許執行:

mysql> set password=password('mechipoderranen');

query ok, 0 rows affected (0.00 sec)

mysql> show databases;

+--------------------+

| database |

+--------------------+

| information_schema |

| data |

| logs |

| mysql |

| performance_schema |

| test |

+--------------------+

6 rows in set (0.00 sec)

mysql>

dba可以通過cron定時器任務來設定mysql使用者的密碼過期時間。

從mysql 5.7.4版開始,使用者的密碼過期時間這個特性得以改進,可以通過乙個全域性變數default_password_lifetime來設定密碼過期的策略,此全域性變數可以設定乙個全域性的自動密碼過期策略。

用法示例:

可以在mysql的配置檔案中設定乙個預設值,這會使得所有mysql使用者的密碼過期時間都為90天,mysql會從啟動時開始計算時間。my.cnf配置如下:

[mysqld]

default_password_lifetime=90

如果要設定密碼永不過期的全域性策略,可以這樣:(注意這是預設值,配置檔案中可以不宣告)

[mysqld]

default_password_lifetime=0

在mysql執行時可以使用超級許可權修改此配置:

mysql>

setglobal default_password_lifetime =

90;query ok, 0

rows affected (0.00 sec)

還可以使用alter user命令為每個具體的使用者賬戶單獨設定特定的值,它會自動覆蓋密碼過期的全域性策略。要注意alter user語句的interval的單位是「天」。

alter user 『testuser'@『localhost' password expire interval 30 day;
禁用密碼過期:

alter

user

'testuser'@'localhost' password expire never;

讓使用者使用預設的密碼過期全域性策略:

alter

user

'testuser'@'localhost' password expire default;

從mysql 5.7.6版開始,還可以使用alter user語句修改使用者的密碼:

mysql> alter user user() identified by

'637h1m27h36r33k';

query ok, 0

rows affected (0.00 sec)

後記

在mysql 5.7.8版開始使用者管理方面新增了鎖定/解鎖使用者賬戶的新特性, related to user management is locking/unlocking user accounts when create user, or at a later time running the alter user statement.

下面建立乙個帶賬戶鎖的使用者:

mysql> create user 'furrywall'@'localhost' identified by

'71m32ch4n6317' account lock;

query ok, 0 rows affected (0.00 sec)

$ mysql -ufurrywall -p

enter password:

error 3118 (hy000): access denied for user 'furrywall'@'localhost'. account is locked.

此時就需要使用alter user … account unlock語句進行解鎖了:

mysql>alter user 'furrywall'@'localhost' account unlock;

query ok, 0

rows affected (0.00 sec)

現在,這個使用者已經解鎖,可以登陸了:

$ mysql -u furrywall -p

enter password:

welcome to the mysql monitor. commands end

with ; or g.

your mysql connection id is

17server version: 5.7

.8-rc mysql community server (gpl)

oracle is a registered trademark of oracle corporation and/or its

affiliates. other names may be trademarks of their respective

owners.

type

'help;' or

'h'for help. type

'c' to clear the current input statement.

mysql>

還可以這樣鎖定使用者賬戶:

mysql> alter user 'furrywall'@'localhost' account lock;

query ok, 0

rows affected (0.00 sec)

mysql設定使用者密碼過期時間

mysql5.7.4之後 修改my.cnf配置檔案 mysqld default password lifetime 90 密碼90天過期 或是 mysqld default password lifetime 0 密碼永不過期或執行時修改 set global default password l...

MySQL 設定使用者密碼過期策略

1.在mysql執行時可以使用超級許可權修改此配置 mysql set global default password lifetime 90 query ok,0 rows affected 0.00 sec 還可以使用alter user命令為每個具體的使用者賬戶單獨設定特定的值,它會自動覆蓋密...

MySQL密碼過期

1 用mysql命令列登入mysql的root使用者 2 重新修改root密碼 set password for root localhost password newpass mysql 5.7增加了兩個欄位password last changed password lifetime來完善安全策...