MySQL用户登录失败一定次数自动锁定用户
MySQL 8.0 创建和修改用户的时候可以设置
创建用户
相关语法
CREATE USER [IF NOT EXISTS]
user [auth_option] [, user [auth_option]] ...
DEFAULT ROLE role [, role ] ...
[REQUIRE {NONE | tls_option [[AND] tls_option] ...}]
[WITH resource_option [resource_option] ...]
[password_option | lock_option] ...
[COMMENT 'comment_string' | ATTRIBUTE 'json_object']
password_option: {
PASSWORD EXPIRE [DEFAULT | NEVER | INTERVAL N DAY]
| PASSWORD HISTORY {DEFAULT | N}
| PASSWORD REUSE INTERVAL {DEFAULT | N DAY}
| PASSWORD REQUIRE CURRENT [DEFAULT | OPTIONAL]
| FAILED_LOGIN_ATTEMPTS N
| PASSWORD_LOCK_TIME {N | UNBOUNDED}
}
创建用户测试
-- 本地用户,登录失败3次,锁定2天
CREATE USER 'jeffrey'@'localhost'
IDENTIFIED WITH caching_sha2_password BY 'jeffrey'
PASSWORD EXPIRE INTERVAL 180 DAY
FAILED_LOGIN_ATTEMPTS 3 PASSWORD_LOCK_TIME 2;
查询用户状态
mysql> select user,host,account_locked,User_attributes from mysql.user where user='jeffrey';
+---------+-----------+----------------+----------------------------------------------------------------------------------+
| user | host | account_locked | User_attributes |
+---------+-----------+----------------+----------------------------------------------------------------------------------+
| jeffrey | localhost | N | {"Password_locking": {"failed_login_attempts": 3, "password_lock_time_days": 2}} |
+---------+-----------+----------------+----------------------------------------------------------------------------------+
1 row in set (0.00 sec)
测试连接
# 三次登录失败
[root@testos ~]# mysql -ujeffrey -pxxx -e "select version()"
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'jeffrey'@'localhost' (using password: YES)
[root@testos ~]# mysql -ujeffrey -pxxx -e "select version()"
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'jeffrey'@'localhost' (using password: YES)
[root@testos ~]# mysql -ujeffrey -pxxx -e "select version()"
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 3955 (HY000): Access denied for user 'jeffrey'@'localhost'. Account is blocked for 2 day(s) (2 day(s) remaining) due to 3 consecutive failed logins.
[root@testos ~]#
# 第四次使用正确的密码
[root@testos ~]# mysql -ujeffrey -pjeffrey -e "select version()"
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 3955 (HY000): Access denied for user 'jeffrey'@'localhost'. Account is blocked for 2 day(s) (2 day(s) remaining) due to 3 consecutive failed logins.
[root@testos ~]#
查询用户状态
-- 注意用户的锁定状态是N
mysql> select user,host,account_locked,User_attributes from mysql.user where user='jeffrey';
+---------+-----------+----------------+----------------------------------------------------------------------------------+
| user | host | account_locked | User_attributes |
+---------+-----------+----------------+----------------------------------------------------------------------------------+
| jeffrey | localhost | N | {"Password_locking": {"failed_login_attempts": 3, "password_lock_time_days": 2}} |
+---------+-----------+----------------+----------------------------------------------------------------------------------+
1 row in set (0.00 sec)
解锁
mysql> alter user 'jeffrey'@'localhost' account unlock;
Query OK, 0 rows affected (0.00 sec)
再次测试登录
[root@testos ~]# mysql -ujeffrey -pjeffrey -e "select version()"
mysql: [Warning] Using a password on the command line interface can be insecure.
+-----------+
| version() |
+-----------+
| 8.0.36 |
+-----------+
[root@testos ~]#
修改用户
相关语法
ALTER USER [IF EXISTS]
user [auth_option] [, user [auth_option]] ...
[REQUIRE {NONE | tls_option [[AND] tls_option] ...}]
[WITH resource_option [resource_option] ...]
[password_option | lock_option] ...
[COMMENT 'comment_string' | ATTRIBUTE 'json_object']
password_option: {
PASSWORD EXPIRE [DEFAULT | NEVER | INTERVAL N DAY]
| PASSWORD HISTORY {DEFAULT | N}
| PASSWORD REUSE INTERVAL {DEFAULT | N DAY}
| PASSWORD REQUIRE CURRENT [DEFAULT | OPTIONAL]
| FAILED_LOGIN_ATTEMPTS N
| PASSWORD_LOCK_TIME {N | UNBOUNDED}
}
创建用户测试
mysql> CREATE USER 'tom'@'localhost' IDENTIFIED BY 'tom';
Query OK, 0 rows affected (0.00 sec)
查询用户状态
mysql> select user,host,account_locked,User_attributes from mysql.user where user='tom';
+------+-----------+----------------+-----------------+
| user | host | account_locked | User_attributes |
+------+-----------+----------------+-----------------+
| tom | localhost | N | NULL |
+------+-----------+----------------+-----------------+
1 row in set (0.00 sec)
测试登录
[root@testos ~]# mysql -utom -pxxx -e "select version()"
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'tom'@'localhost' (using password: YES)
[root@testos ~]# mysql -utom -pxxx -e "select version()"
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'tom'@'localhost' (using password: YES)
[root@testos ~]# mysql -utom -pxxx -e "select version()"
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'tom'@'localhost' (using password: YES)
[root@testos ~]# mysql -utom -pxxx -e "select version()"
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'tom'@'localhost' (using password: YES)
[root@testos ~]# mysql -utom -ptom -e "select version()"
mysql: [Warning] Using a password on the command line interface can be insecure.
+-----------+
| version() |
+-----------+
| 8.0.36 |
+-----------+
[root@testos ~]#
修改用户
mysql> alter user 'tom'@'localhost' FAILED_LOGIN_ATTEMPTS 3 PASSWORD_LOCK_TIME 2;
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host,account_locked,User_attributes from mysql.user where user='tom';
+------+-----------+----------------+----------------------------------------------------------------------------------+
| user | host | account_locked | User_attributes |
+------+-----------+----------------+----------------------------------------------------------------------------------+
| tom | localhost | N | {"Password_locking": {"failed_login_attempts": 3, "password_lock_time_days": 2}} |
+------+-----------+----------------+----------------------------------------------------------------------------------+
1 row in set (0.00 sec)
测试登录
#三次登录失败
[root@testos ~]# mysql -utom -pxxx -e "select version()"
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'tom'@'localhost' (using password: YES)
[root@testos ~]# mysql -utom -pxxx -e "select version()"
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'tom'@'localhost' (using password: YES)
[root@testos ~]# mysql -utom -pxxx -e "select version()"
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 3955 (HY000): Access denied for user 'tom'@'localhost'. Account is blocked for 2 day(s) (2 day(s) remaining) due to 3 consecutive failed logins.
[root@testos ~]#
#输入正确的密码也连接失败
[root@testos ~]# mysql -utom -ptom -e "select version()"
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 3955 (HY000): Access denied for user 'tom'@'localhost'. Account is blocked for 2 day(s) (2 day(s) remaining) due to 3 consecutive failed logins.
[root@testos ~]#
解锁
[root@testos ~]# mysql -uroot -prootroot -e "alter user 'tom'@'localhost' account unlock"
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@testos ~]# mysql -utom -ptom -e "select version()"
mysql: [Warning] Using a password on the command line interface can be insecure.
+-----------+
| version() |
+-----------+
| 8.0.36 |
+-----------+
[root@testos ~]#
1236

被折叠的 条评论
为什么被折叠?



