mysql-密碼過期
阿新 • • 發佈:2020-07-16
mysql-密碼過期
問題現象
- error-log提示:ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
- 使用出錯的資料庫業務賬號登入資料庫執行任一命令時提示上面語句
解決辦法
臨時解決
-
修改密碼
mysql> set password = password('newpasswd'); #使用過期使用者登入直接修改 mysql> set password for 'test'@'%' = password('newpasswd'); #使用具有修改使用者許可權的使用者登入來修改
永久解決
-
將使用者的密碼有效期修改為永不過期
mysql> alter user 'test'@'%' password expire never;
-
修改密碼有效期預設策略
mysql> set global default_password_lifetime=0;
邏輯原理
使用者指定的密碼策略 > 預設策略
-
建立使用者時指定密碼有效期
mysql> alter user 'test'@'%' identified by '321321' password expire interval 1 day;
-
建立使用者時為預設密碼策略
mysql> create user 'test'@'%' identified by '321321'; mysql> show create user 'test'@'%'\G *************************** 1. row *************************** CREATE USER for test@%: CREATE USER 'test'@'%' IDENTIFIED WITH 'mysql_native_password' AS '*4160291B4C8CC2573CC94951203FFBC858754907' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK 1 row in set (0.00 sec)
其中'PASSWORD EXPIRE'是預設的'DEFAULT'
-
檢視預設策略
mysql> show variables like 'default_password_lifetime'; +---------------------------+-------+ | Variable_name | Value | +---------------------------+-------+ | default_password_lifetime | 0 | +---------------------------+-------+ 1 row in set (0.01 sec)
'0'代表永不過期
mysql_5.7.4 - 5.7.10版本預設是360天,5.7.11以後預設永不過期
填坑指南
假如新進公司,資料庫屬於運維範圍,這時候可以想想是否有資料庫使用者密碼過期的坑
-
檢視使用者密碼狀態
mysql> SELECT user,host,password_expired,password_last_changed,password_lifetime from mysql.user; +---------------+-----------+------------------+-----------------------+-------------------+ | user | host | password_expired | password_last_changed | password_lifetime | +---------------+-----------+------------------+-----------------------+-------------------+ | root | localhost | N | 2020-07-16 12:19:39 | NULL | | mysql.session | localhost | N | 2020-07-15 11:11:16 | NULL | | mysql.sys | localhost | N | 2020-07-15 11:11:16 | NULL | | test | % | N | 2020-07-16 12:49:23 | NULL | +---------------+-----------+------------------+-----------------------+-------------------+ 4 rows in set (0.00 sec)
-
檢視使用者的密碼策略是否為預設策略
mysql> SHOW CREATER USER 'test'@'%'; +-------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | CREATE USER for test@% | +-------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | CREATE USER 'test'@'%' IDENTIFIED WITH 'mysql_native_password' AS '*4160291B4C8CC2573CC94951203FFBC858754907' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK | +-------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
-
檢視資料庫密碼預設策略
mysql> SHOW VARIABLES LIKE 'default_password_lifetime'; +---------------------------+-------+ | Variable_name | Value | +---------------------------+-------+ | default_password_lifetime | 0 | +---------------------------+-------+ 1 row in set (0.00 sec)
-
檢視當前時間
mysql> SELECT now(); +---------------------+ | now() | +---------------------+ | 2020-07-16 13:17:01 | +---------------------+ 1 row in set (0.00 sec)
-
根據第3步的全域性密碼策略對第1步中'password_last_changed'和第4步的當前時間做比對,來判斷是否過期;原因是第1步中的'password_expired'並不準確