Ubuntu中Mysql结合防火墙的使用实现Bind多IP以及MySQL数据库读写分离中间件及ProxySQL的安装

发布于:2024-09-17 ⋅ 阅读:(148) ⋅ 点赞:(0)

一、Ubuntu中Mysql结合防火墙的使用实现Bind多IP

    Mysql中要在配置中bind指定多个IP是实现不了的,之前也有一篇相关的文章,可在博客中搜索,其配置项要么是指定一个IP地址,要么是0.0.0.0。默认是配置的127.0.0.1,但这会导致外面的服务器以及win上的client无法连接mysql查看数据。而如果改成0.0.0.0,又对外暴露了端口,比较危险。我以前的做法是绑定局域网IP地址,然后使用haproxy作负载提供对外的连接服务,可在haproxy中的七层四层上做对应的IP限制。实际上另外一个结合防火墙的方法也比较好用。

    在Ubuntu中先要将mysql的bind-address配置注释或者设置成0.0.0.0,重启mysql;这时mysql支持了外部IP的访问,然后通过防火墙阻止需要的IP外对3306端口的访问,从而间接实现绑定了多个IP,包括127.0.0.1,内网IP和外网IP地址。如下:

#防火墙的配置
root@us12:~# /sbin/iptables -A INPUT -p tcp -s 112.95.214.8 --dport 3306 -j ACCEPT 
root@us12:~# /sbin/iptables -A INPUT -p tcp -s 192.168.162.8  --dport 3306 -j ACCEPT 
root@us12:~# /sbin/iptables -A INPUT -p tcp --dport 3306 -j DROP
#查看防火墙的设置
root@us12:~# /sbin/iptables --list
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  112.95.214.8         anywhere             tcp dpt:mysql
ACCEPT     tcp  --  192.168.162.8        anywhere             tcp dpt:mysql
DROP       tcp  --  anywhere             anywhere             tcp dpt:mysql

Chain FORWARD (policy DROP)
target     prot opt source               destination         
DOCKER-USER  all  --  anywhere             anywhere            
DOCKER-ISOLATION-STAGE-1  all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
#可以使用-D选项删除指定条,如果执行下面这条命令就是删除Chain INPUT中112.95.214.8的许可这条    
root@us12:~# iptables -D INPUT 1
#也可以通过!排除法进行添加多个IP
root@us12:/etc/# /sbin/iptables -A INPUT -p tcp --dport 3306 ! -s 112.95.214.8 -j DROP
root@us12:/etc/# /sbin/iptables -A INPUT -p tcp --dport 3306 ! -s 192.168.162.8 -j DROP

    在centos中防火墙我是通过/etc/init.d/iptables命令进行重启,但是在Ubuntu16中未找到对应的命令文件,不过可以通过修改/etc/sysconfig/iptables配置让它自动加载生效并保存。

#使用iptables-save保存iptables规则
root@us12:/etc/# iptables-save > /etc/iptables.rules
#iptables规则自动保存与自动加载,添加以下两行
root@us12:/etc/# vim /etc/network/interfaces
pre-up iptables-restore < /etc/iptables.rules
post-down iptables-save > /etc/iptables.rules

二、MySQL数据库读写分离中间件及ProxySQL的安装

    现在的框架或者很多开源CMS基本都已经在代码层面支持了读写分离,但现在手上有一套代码层面已经不好改动的系统,要想实现读写分离,那可得伤筋动骨,毕竟系统比较庞大,且其中有大量的事务处理,而事务处理中不能简单将一个事务中的SQL机械地根据是insert还是update来执行分离,所以只有考虑使用读写分离的中间件了,

    ProxySQL是一个高性能、高可用的MYSQL读写分离开源中间件,当然市面上也有不少的相关的产品,有mysql官方的MySQL-Proxy、有阿里巴巴的Cobar、Amoeba、TDDL,还有其它比如Atlas,mycat,one proxy,proxySQL等等中间件。MySQL-Proxy更新缓慢,长期停留在Alpha版阶段, Amoeba不支持事务,TDDL未完整开源。360开发的Atlas开始感觉不错,但是版本好久没有更新了,上次更新还是2016-07-28,

地址:https://sourceforge.net/projects/math-atlas/files/Stable/ 

        不过从网上来看这个产品认可度也还好,这里找到一个安装的shell脚本 https://gist.github.com/kparrish/6395318 最后还是选择一个稳定、使用比较普遍的ProxySQL。因为它对事务有很明确的支持。

    官网地址:https://proxysql.com/ 下载地址:Index of /ProxySQL/
    目前最新版本是2019年刚发布的2.0版,地址:Index of /ProxySQL/proxysql-2.0.x/stretch/

#安装ProxySQL
yes@u07:/# apt-get install -y lsb-release
yes@u07:/# wget -O - 'http://repo.proxysql.com/ProxySQL/repo_pub_key' | apt-key add -
yes@u07:/# echo deb http://repo.proxysql.com/ProxySQL/proxysql-1.4.x/$(lsb_release -sc)/ ./ \
| tee /etc/apt/sources.list.d/proxysql.list
yes@u07:~# lsb_release --help
Usage: lsb_release [options]
Options:
  -h, --help         show this help message and exit
  -v, --version      show LSB modules this system supports
  -i, --id           show distributor ID
  -d, --description  show description of this distribution
  -r, --release      show release number of this distribution
  -c, --codename     show code name of this distribution
  -a, --all          show all of the above information
  -s, --short        show requested information in short format
yes@u07:~# lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 9.2(stretch)
Release:        9.2
Codename:       stretch
yes@u07:/# echo "deb http://repo.proxysql.com/ProxySQL/proxysql-1.4.x/stretch/ ./" >> /etc/apt/sources.list.d/proxysql.list
yes@u07:/# apt-get update
yes@u07:/# apt-get install proxysql

    lsb_release命令,其中LSB是Linux Standard Base的缩写,显示发行版本信息。如上示例中使用-ac选项得取short版的codename值。proxysql开启/关闭/重启命令

开启:service proxysql start
关闭:service proxysql stop
重启:service proxysql restart


网站公告

今日签到

点亮在社区的每一天
去签到