Docker Compose搭建mycat讀寫分離
阿新 • • 發佈:2019-01-07
接上篇docker-compose部署mysql主從複製,本文介紹如何搭建mycat中介軟體
,並用mycat
來做讀寫分離
.
系統環境
- docker 1.12.3
- mysql5.7.17
- deepin 15.3桌面版(這個沒啥影響,因為我們用docker)
- mycat1.6
要點說明
- 看上篇文章的詳細介紹
- 暴露
mysql
mycat
埠號,方便管理 - 本文直接從
docker-compose.yml
開始
Begin
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.xml配置
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<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>
server.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!-- - - Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License. - You
may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0
- - Unless required by applicable law or agreed to in writing, software -
distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the
License for the specific language governing permissions and - limitations
under the License. -->
<!DOCTYPE mycat:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://io.mycat/">
<system>
<property name="useSqlStat">0</property> <!-- 1為開啟實時統計、0為關閉 -->
<property name="useGlobleTableCheck">0</property> <!-- 1為開啟全加班一致性檢測、0為關閉 -->
<property name="sequnceHandlerType">2</property>
<!-- <property name="useCompression">1</property>--> <!--1為開啟mysql壓縮協議-->
<!-- <property name="fakeMySQLVersion">5.6.20</property>--> <!--設定模擬的MySQL版本號-->
<!-- <property name="processorBufferChunk">40960</property> -->
<!--
<property name="processors">1</property>
<property name="processorExecutor">32</property>
-->
<!--預設為type 0: DirectByteBufferPool | type 1 ByteBufferArena-->
<property name="processorBufferPoolType">0</property>
<!--預設是65535 64K 用於sql解析時最大文字長度 -->
<!--<property name="maxStringLiteralLength">65535</property>-->
<!--<property name="sequnceHandlerType">0</property>-->
<!--<property name="backSocketNoDelay">1</property>-->
<!--<property name="frontSocketNoDelay">1</property>-->
<!--<property name="processorExecutor">16</property>-->
<!--
<property name="serverPort">8066</property> <property name="managerPort">9066</property>
<property name="idleTimeout">300000</property> <property name="bindIp">0.0.0.0</property>
<property name="frontWriteQueueSize">4096</property> <property name="processors">32</property> -->
<!--分散式事務開關,0為不過濾分散式事務,1為過濾分散式事務(如果分散式事務內只涉及全域性表,則不過濾),2為不過濾分散式事務,但是記錄分散式事務日誌-->
<property name="handleDistributedTransactions">0</property>
<!--
off heap for merge/order/group/limit 1開啟 0關閉
-->
<property name="useOffHeapForMerge">1</property>
<!--
單位為m
-->
<property name="memoryPageSize">1m</property>
<!--
單位為k
-->
<property name="spillsFileBufferSize">1k</property>
<property name="useStreamOutput">0</property>
<!--
單位為m
-->
<property name="systemReserveMemorySize">384m</property>
<!--是否採用zookeeper協調切換 -->
<property name="useZKSwitch">true</property>
</system>
<!-- 全域性SQL防火牆設定
<firewall>
<whitehost>
<host host="172.18.0.2" user="root"/>
<host host="172.18.0.3" user="root"/>
<host host="172.18.0.4" user="root"/>
</whitehost>
<blacklist check="false">
</blacklist>
</firewall>-->
<user name="root">
<property name="password">jiabin</property>
<property name="schemas">mall</property>
<!-- 表級 DML 許可權設定 -->
<!--
<privileges check="false">
<schema name="TESTDB" dml="0110" >
<table name="tb01" dml="0000"></table>
<table name="tb02" dml="1111"></table>
</schema>
</privileges>
-->
</user>
</mycat:server>
log4j2.xml配置
這個把日誌級別更改為debug,方便我們觀察測試.
mycat的Dockerfile
FROM java:8-jre
MAINTAINER <ssab [email protected]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都已經啟動成功.
我們用docker ps -a
來看一下.
測試
進入mycat客戶端
mysql -u root -p -P 8066 -h 127.0.0.1
執行select語句
因為在上一篇文章中已經做過主從複製的測試,所以這個地方我們就不再重複了,我們直接執行select
語句,看是否已經實現了讀寫分離.
mysql> select * from salesman limit 0,10;
結果集:
然後我們開啟mycat的日誌mycat.log看一下
注意看圖中標記出來的地方.
好吧,從日誌中我們看出我們執行的select
語句是走從庫s1執行的.
執行insert語句
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資料庫中介軟體
,很簡單啊,就是因為它對開發人員基本沒有影響,不會侵入到程式碼中.
共同努力,一起進步!