前言
一般我们开发环境的MySQL是没有配置登录保护的,但仅限于开发环境,正式环境是不允许无限制登录,存在很大的风险。
MySQL 5.7 以后提供了Connection-Control插件用来控制客户端在登录操作连续失败一定次数后的响应的延迟。该插件可有效的防止客户端暴力登录的风险(攻击)。该插件包含以下两个组件:
- connection_control:控制失败次数以及延迟时间
- connection_control_failed_login_attempts:将登录失败的操作记录至information_schema表
插件安装
增加配置文件
~$ vi /etc/my.cnf
// 添加以下配置
[mysqld]
plugin-load-add = connection_control.so
connection-control = FORCE
connection-control-failed-login-attempts = FORCE
connection_control_min_connection_delay = 1000
connection_control_max_connection_delay = 86400
connection_control_failed_connections_threshold = 3
执行安装命令
//使用root账户登录
~$ mysql -u root -p
mysql> install plugin connection_control soname "connection_control.so";
mysql> install plugin connection_control_failed_login_attempts soname "connection_control.so";
验证插件安装状态
mysql> select plugin_name, plugin_status from information_schema.plugins where plugin_name like 'connection%';
+------------------------------------------+---------------+
| plugin_name | plugin_status |
+------------------------------------------+---------------+
| CONNECTION_CONTROL | ACTIVE |
| CONNECTION_CONTROL_FAILED_LOGIN_ATTEMPTS | ACTIVE |
+------------------------------------------+---------------+
2 rows in set (0.00 sec)
修改插件配置
~$ vi /etc/my.cnf
// 修改以下配置,并重启MySQL
// 阻塞一分钟
connection_control_min_connection_delay = 60000
connection_control_max_connection_delay = 86400
// 可错误五次
connection_control_failed_connections_threshold = 5
查看修改后配置
~$ mysql -u root -p;
mysql> show variables like 'connection_control%';
+-------------------------------------------------+------------+
| Variable_name | Value |
+-------------------------------------------------+------------+
| connection_control_failed_connections_threshold | 5 |
| connection_control_max_connection_delay | 86400 |
| connection_control_min_connection_delay | 60000 |
+-------------------------------------------------+------------+
3 rows in set (0.00 sec)
测试
查看登录记录
mysql> use information_schema;
// 查看用户登录失败次数,当用户登录成功则删除记录
mysql> select * from connection_control_failed_login_attempts;
// 连接控制的使用次数
mysql> show global status like 'connection_control_delay_generated';