接上篇docker-compose部署mysql主从复制,本文介绍如何搭建mycat中间件
,并用mycat
来做读写分离
.
配置文件以及文档地址:mycat-rw
mysql
mycat
端口号,方便管理docker-compose.yml
开始为了看起来方便,咱还是一起都贴出来吧
version: '2'
services:
m1:
build: ./master
container_name: m1
volumes:
- /home/ssab/config/mysql-master/:/etc/mysql/:ro
- /etc/localtime:/etc/localtime:ro
- /home/ssab/config/hosts:/etc/hosts:ro
ports:
- "3309:3306" #暴露mysql的端口
networks:
mysql:
ipv4_address: 172.18.0.2
ulimits:
nproc: 65535
hostname: m1
mem_limit: 1024m
restart: always
environment:
MYSQL_ROOT_PASSWORD: m1test
s1:
build: ./s1
container_name: s1
volumes:
- /home/ssab/config/mysql-s1/:/etc/mysql/:ro
- /etc/localtime:/etc/localtime:ro
- /home/ssab/config/hosts:/etc/hosts:ro
ports:
- "3307:3306"
networks:
mysql:
ipv4_address: 172.18.0.3
links:
- m1
ulimits:
nproc: 65535
hostname: s1
mem_limit: 1024m
restart: always
environment:
MYSQL_ROOT_PASSWORD: s1test
s2:
build: ./s2
container_name: s2
volumes:
- /home/ssab/config/mysql-s2/:/etc/mysql/:ro
- /etc/localtime:/etc/localtime:ro
- /home/ssab/config/hosts:/etc/hosts:ro
ports:
- "3308:3306"
links:
- m1
networks:
mysql:
ipv4_address: 172.18.0.4
ulimits:
nproc: 65535
hostname: s2
mem_limit: 1024m
restart: always
environment:
MYSQL_ROOT_PASSWORD: s2test
mycat: # 设置mycat
build: ./mycat
container_name: mycat
volumes:
- /home/ssab/config/mycat/:/mycat/conf/:ro # mycat配置文件
- /home/ssab/config/mycat-logs/:/mycat/logs/:rw # mycat日志文件
- /etc/localtime:/etc/localtime:ro
- /home/ssab/config/hosts:/etc/hosts:ro
ports:
- "8066:8066" # 暴露mycat服务端口
- "9066:9066" # 暴露mycat管理端口
links: # mycat可以连接m1 s1 s2
- m1
- s1
- s2
networks:
mysql:
ipv4_address: 172.18.0.5
ulimits:
nproc: 65535
hostname: mycat
mem_limit: 1024m
restart: always
networks:
mysql:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.18.0.0/24
gateway: 172.18.0.1
这里只是说一个成功运行的配置,具体详细的配置规则请自己参考mycat权威指南.
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="mall" checkSQLschema="false" sqlMaxLimit="100" dataNode="mallDN">
schema>
<dataNode name="mallDN" dataHost="mallDH" database="mall">
dataNode>
<dataHost name="mallDH" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="native" switchType="-1" slaveThreshold="100">
<heartbeat>select user()heartbeat>
<writeHost host="m1" url="172.18.0.2:3306" user="root" password="m1test">
<readHost host="s1" url="172.18.0.3:3306" user="root" password="s1test" />
<readHost host="s2" url="172.18.0.4:3306" user="root" password="s2test" />
writeHost>
dataHost>
mycat:schema>
<mycat:server xmlns:mycat="http://io.mycat/">
<system>
<property name="useSqlStat">0property>
<property name="useGlobleTableCheck">0property>
<property name="sequnceHandlerType">2property>
<property name="processorBufferPoolType">0property>
<property name="handleDistributedTransactions">0property>
<property name="useOffHeapForMerge">1property>
<property name="memoryPageSize">1mproperty>
<property name="spillsFileBufferSize">1kproperty>
<property name="useStreamOutput">0property>
<property name="systemReserveMemorySize">384mproperty>
<property name="useZKSwitch">trueproperty>
system>
<user name="root">
<property name="password">jiabinproperty>
<property name="schemas">mallproperty>
user>
mycat:server>
这个把日志级别更改为debug,方便我们观察测试.
FROM java:8-jre
MAINTAINER <ssab work_wjj@163.com>
LABEL Description="使用mycat做mysql数据库的读写分离"
ENV mycat-version Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
USER root
COPY ./Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz /
RUN tar -zxf /Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
ENV MYCAT_HOME=/mycat
ENV PATH=$PATH:$MYCAT_HOME/bin
WORKDIR $MYCAT_HOME/bin
RUN chmod u+x ./mycat
EXPOSE 8066 9066
CMD ["./mycat","console"]
在docker-compose.yml
文件目录下运行
docker-compose up -d
如果没有容器对应的镜像文件,则docker-compose
会自动构建镜像.
使用docker-compose
手动构建镜像的命令:docker-compose build mycat
命令成功执行,则容器mycat,m1,s1,s2都已经启动成功.
mysql -u root -p -P 8066 -h 127.0.0.1
因为在上一篇文章中已经做过主从复制的测试,所以这个地方我们就不再重复了,我们直接执行select
语句,看是否已经实现了读写分离.
mysql> select * from salesman limit 0,10;
注意看图中标记出来的地方.
好吧,从日志中我们看出我们执行的select
语句是走从库s1执行的.
mysql> insert into salesman (id,user_num,true_name,address,mobile,disabled) values('30769','33333','ssab','山东省','33333321',0);
打开mycat的日志mycat.log看一下
这次我们发现,执行insert
语句走的是主库m1.
简单来讲,一个使用mycat中间件
搭建mysql 1主2从 主从复制 读写分离的实例就完成了.
要说为什么使用mycat数据库中间件
,很简单啊,就是因为它对开发人员基本没有影响,不会侵入到代码中.
共同努力,一起进步!
原文地址