MySQL用户登录失败一定次数自动锁定用户

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 ~]#

参考资料

https://dev.mysql.com/doc/refman/8.0/en/alter-user.html

https://dev.mysql.com/doc/refman/8.0/en/create-user.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值