当前位置: 首页 > 工具软件 > MySQL-Proxy > 使用案例 >

mysql-proxy代理内网数据库

钮博裕
2023-12-01

一、使用场景介绍

mysql-proxy的机器属于代理机器也就是相当于白名单的审核机器,通过防火墙规则,限制允许那些IP访问本机的代理内网数据库的端口, 有点:保证内网的数据库不暴露在公网上,保证了数据库的安全。 缺点:一旦mysql-proxy代理服务器挂掉了,就相当于应用都链接不上了数据库。同时如果太多的应用程序通过mysql-proxy链接内网数据库的话,mysql-proxy代理服务器必须也得保证足够的带宽,负责导致链接数据库特别的慢,或者链接不上数据库。

二、演示环境:

2台物理机器,系统都是CentOS release 6.8 (Final) 一台机器配置内外网卡:104.137.27.4 192.168.1.100 部署mysql-proxy 服务 一台机器配置外网卡:104.137.27.3 部署mysql服务,并登陆此机器数据库,授权一个账户允许104.137.27.4机器来链接此数据库 ``` grant all on mtoyydb.* to zytestuser01@'104.137.27.4' identified by 'dr3dfKj=DHee';

mysql> flush privileges ```

三、二进制安装mysql-proxy:

下载安装包:wget https://downloads.mysql.com/archives/get/file/mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit.tar.gz tar zxf mysql-proxy-0.8.5-linux-el6-x86-64bit -C /usr/local/ cd /usr/local/ mv mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit mysql-proxy mkdir /usr/local/mysql-proxy/{conf,log} -p

定义下环境变量:

[root@book sysconfig]# tail -3 /etc/profile LUA_PATH="/usr/local/mysql-proxy/share/doc/mysql-proxy/?.lua" export LUA_PATH export PATH=$PATH:/usr/local/mysql-proxy/bin

四、mysql-proxy 参数介绍:

Application Options:

4.1应用参数介绍:

mysql-proxy --help-all

4.2mysql-proxy代理模块参数介绍

4.3线上配置文件介绍:

``` [root@book mysql-proxy]# cat /usr/local/mysql-proxy/conf/mysql-proxy.conf [mysql-proxy] user=www daemon=true keepalive=true plugins=proxy,admin

日志级别

log-level=info log-file=/usr/local/mysql-proxy/log/mysql-proxy.log

本机ip地址

proxy-address=104.137.27.4:9196

backend主 注意addresses

proxy-backend-addresses=104.137.27.3:3306

proxy的管理用户admin的IP和端口

admin-address=104.137.27.4:9197

下面的三个参数必须设定,否则mysql-proxy服务启动不了的

admin-username=zykjadmin admin-password=Zyjkwestos

admin的lua脚本地址;

admin-lua-script=/usr/local/mysql-proxy/lib/mysql-proxy/lua/admin.lua ```

五、启动mysql-proxy

/usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/mysql-proxy.conf

登陆mysql-proxy管理账户,查看反向代理管理列表 [root@book ~]# mysql -uzykjadmin -pZyjkwestos -h104.137.27.4 --port=9197

``` Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.0.99-agent-admin

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> SELECT * FROM backends; +-------------+------------------+-------+------+------+-------------------+ | backendndx | address | state | type | uuid | connectedclients | +-------------+------------------+-------+------+------+-------------------+ | 1 | 104.137.27.3:3306 | up | rw | NULL | 2 | +-------------+------------------+-------+------+------+-------------------+ 1 row in set (0.00 sec)

MySQL [(none)]> SELECT * FROM help; +------------------------+------------------------------------+ | command | description | +------------------------+------------------------------------+ | SELECT * FROM help | shows this help | | SELECT * FROM backends | lists the backends and their state | +------------------------+------------------------------------+ 2 rows in set (0.00 sec)

MySQL [(none)]> ```

六、通过sqlyog来登陆反向代理数据库

七、MySQL-porxy代理服务器防火墙的配置策略:

``` [root@book sysconfig]# cat /etc/sysconfig/iptables

Generated by iptables-save v1.4.7 on Thu Mar 2 14:32:03 2017

*filter :INPUT ACCEPT [358:20023] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [2491:287941] -A INPUT -i lo -j ACCEPT -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -p tcp -m tcp --dport 4567 -j ACCEPT -A INPUT -p tcp -m multiport --dports 80,81,443 -m state --state NEW -j ACCEPT -A INPUT -s 104.137.27.6/32 -p tcp -m tcp --dport 873 -m state --state NEW -j ACCEPT -A INPUT -s 304.37.57.45/32 -p tcp -m multiport --dports 9196 -j ACCEPT -A INPUT -s 204.17.47.245/32 -p tcp -m multiport --dports 21,10050,3306 -j ACCEPT

-A INPUT -p tcp -m tcp --dport 9196 -j DROP

-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT

-A INPUT -p icmp -m icmp --icmp-type 8 -m limit --limit 5/sec --limit-burst 10 -j ACCEPT -A INPUT -j DROP COMMIT

Completed on Thu Mar 2 14:32:03 2017

```

7.1配置策略解释:

``` A INPUT -p tcp -m tcp --dport 4567 -j ACCEPT 此处6029相当于sshd的登陆端口号

-A INPUT -p tcp -m multiport --dports 80,81,443 -m state --state NEW -j ACCEPT 放行80,81和443业务端口 -A INPUT -s 104.137.27.6/32 -p tcp -m tcp --dport 873 -m state --state NEW -j ACCEPT 允许特定的IP104.137.27.6到本地服务器上通过rsync拉取数据

-A INPUT -s 304.37.57.45/32 -p tcp -m multiport --dports 9196 -j ACCEPT 允许指定的IP304.37.57.45 来链接MySQL-proxy服务的9196端口,从而链接到内网的数据库

-A INPUT -s 204.17.47.245/32 -p tcp -m multiport --dports 21,10050,3306 -j ACCEPT 解释:204.17.47.245 机器上部署-server,通过拨号登陆-server,然后在可以登陆ftp,登陆3306数据库服务

-A INPUT -p icmp -m icmp --icmp-type 8 -m limit --limit 5/sec --limit-burst 10 -j ACCEPT 解释:对ping做限制策略

-A INPUT -j DROP 解释:其他的客户端IP地址一律禁止访问本服务器上的服务的端口 ```

 类似资料: