1、安裝nginx,安裝過程中注意添加--with-stream模塊
2、修改nginx配置文件(編輯配置文件時注意所有的空格避免tab鍵出現)
stream { #類似於7層的http段
upstream mysql {
#hash $remote_addr consistent;
server 172.16.1.202:3306 weight=1 max_fails=3 fail_timeout=10s;
}
server {
listen 新端口;
proxy_pass mysql;
proxy_timeout 600s;
proxy_connect_timeout 30s;
}
}
3、重啓nginx,在客戶端登錄數據庫
出現如下報錯
[root@ip-10-0-1-85 ~]# mysql -uuser -hIP地址 -P 新端口 -p
Enter password:
ERROR 1129 (HY000): Host 'IP地址' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'
解決辦法
mysql> show variables like 'max_connect_errors';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| max_connect_errors | 100 |
+--------------------+-------+
1 row in set
mysql> set global max_connect_errors=1000
-> ;
mysql> show variables like 'max_connect_errors';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| max_connect_errors | 1000 |
+--------------------+-------+
1 row in set
4、重新登錄
[root@ip-10-0-1-85 ~]# mysql -ucpct_db_test -hIP地址 -P 新端口 -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 108554
Server version: 5.6.41-log Source distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> Ctrl-C -- exit!
Aborted
當我們後期還出現上一步的問題是我們可以通過flush hosts;命令來解決問題
至此通過nginx實現四層端口轉發已部署完畢