mysql服务nginx和firewalld代理实现


工作中常常遇到只有某个特定服务器才能访问数据库的情况,这个时候为了解决团队同时访问数据库的问题可以采用nginx代理和linux防火墙流量转发的方式解决。实战测试如下:

环境准备

准备一台mysql服务器 和一台虚拟机用来作为访问mysql的代理机/流量转发机/跳板机,
服务器关系:
本地客户端主机------->代理(nginx)或防火墙转发服务器 -------->mysql服务器

nginx代理mysql服务

上传nginx 安装包到 跳板机服务器 并解压如下
在这里插入图片描述
进入nginx源码目录 进行编译 ,编译时一定要带上 stream 模块,该模块实现tcp 四层转发

./configure   --with-http_stub_status_module --with-http_ssl_module --with-stream --with-http_realip_module  --with-stream_realip_module

编译通过后 执行 make && make install 进行安装

安装完成到 安装目录 /usr/local/nginx 下 修改 config/nginx.conf 配置文件

[root@host10 nginx]# pwd
/usr/local/nginx
[root@host10 nginx]# ll
total 4
drwx------ 2 nobody root    6 May 26 05:37 client_body_temp
drwxr-xr-x 2 root   root 4096 May 26 06:19 conf
drwx------ 2 nobody root    6 May 26 05:37 fastcgi_temp
drwxr-xr-x 2 root   root   40 May 26 05:31 html
drwxr-xr-x 2 root   root   58 May 26 05:38 logs
drwx------ 2 nobody root    6 May 26 05:37 proxy_temp
drwxr-xr-x 2 root   root   36 May 26 06:06 sbin
drwx------ 2 nobody root    6 May 26 05:37 scgi_temp
drwx------ 2 nobody root    6 May 26 05:37 uwsgi_temp
[root@host10 nginx]# vi conf/nginx.conf

配置文件增加如下内容 注意 stream 配置 是和 http并列的配置 必须在http 外

在这里插入图片描述

stream {
    server {
        listen 10000;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
        proxy_pass 10.XXX.XXX.XXX:10376;
    }
}

启动nginx 进行访问测试。

[root@host10 nginx]# sbin/nginx

mysql 连接工具中配置 mysql连接信息使用 跳板机上 nginx 的 ip 和端口 进行连接测试,连接正常

在这里插入图片描述

linux防火墙实现mysql流量转发

原理类似,我们需要在跳板机配置一条防火墙策略, 把8000端口的tcp流量 都转发到mysql服务器
我们这里使用 firewalld进行配置
首先确保 firewalld服务正常

[root@host10 zones]# systemctl status  firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: active (running) since Thu 2021-05-27 01:40:33 +08; 3min 34s ago
     Docs: man:firewalld(1)
 Main PID: 64933 (firewalld)
    Tasks: 2
   Memory: 21.9M
   CGroup: /system.slice/firewalld.service
           └─64933 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid

May 27 01:40:33 host10 systemd[1]: Starting firewalld - dynamic firewall daemon...
May 27 01:40:33 host10 systemd[1]: Started firewalld - dynamic firewall daemon.
May 27 01:40:33 host10 firewalld[64933]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration option. It will be removed in a future rel...ing it now.
Hint: Some lines were ellipsized, use -l to show in full.

修改内核配置,允许ipv4转发,并使配置生效

echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p

开启IP伪装(貌似和地址转换有关)

[root@host10 zones]# firewall-cmd --add-masquerade --zone=public 
success

配置转发规则,8000端口的流量转发到10.XXX.XXX.XX:10376

[root@host10 zones]# firewall-cmd --add-forward-port=port=8000:proto=tcp:toport=10376:toaddr=10.***.***.*** 
success
You have new mail in /var/spool/mail/root

对外开放8000端口

[root@host10 zones]# firewall-cmd --zone=public --add-port=8000/tcp 
success

查看确认配置的策略

[root@host10 zones]#  firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources: 
  services: dhcpv6-client ssh
  ports: 8000/tcp
  protocols: 
  masquerade: yes
  forward-ports: port=8000:proto=tcp:toport=10376:toaddr=10.***.***.***
  source-ports: 
  icmp-blocks: 
  rich rules: 

在这里插入图片描述

访问测试
telnet 跳板机的 8000端口可以看到连接到了mysql如下:
在这里插入图片描述

mysql工具连接测试,连接成功

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

catch that elf

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值