Docker Compose搭建mycat读写分离

接上篇docker-compose部署mysql主从复制,本文介绍如何搭建mycat中间件,并用mycat来做读写分离.

配置文件以及文档地址:mycat-rw

系统环境

  • 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配置



<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配置




<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>

log4j2.xml配置

这个把日志级别更改为debug,方便我们观察测试.

mycat的Dockerfile

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都已经启动成功.

我们用docker ps -a来看一下.mycat

测试

进入mycat客户端

mysql -u root -p -P 8066 -h 127.0.0.1

执行select语句

因为在上一篇文章中已经做过主从复制的测试,所以这个地方我们就不再重复了,我们直接执行select语句,看是否已经实现了读写分离.

mysql> select * from salesman limit 0,10;

结果集:
Docker Compose搭建mycat读写分离_第1张图片

然后我们打开mycat的日志mycat.log看一下
Docker Compose搭建mycat读写分离_第2张图片

注意看图中标记出来的地方.

好吧,从日志中我们看出我们执行的select语句是走从库s1执行的.

执行insert语句

mysql> insert into salesman (id,user_num,true_name,address,mobile,disabled) values('30769','33333','ssab','山东省','33333321',0);

打开mycat的日志mycat.log看一下

Docker Compose搭建mycat读写分离_第3张图片

这次我们发现,执行insert语句走的是主库m1.

总结

简单来讲,一个使用mycat中间件搭建mysql 1主2从 主从复制 读写分离的实例就完成了.

要说为什么使用mycat数据库中间件,很简单啊,就是因为它对开发人员基本没有影响,不会侵入到代码中.

共同努力,一起进步!

原文地址

你可能感兴趣的:(linux)