前言
一般我們開發環境的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';