docker 搭建Mycat环境实现Mysql数据库的读写分离

 

搭建Mycat环境

1.2 搭建Mycat环境

1.2.1 获取资源
#先获取mycat包(其他版本下载地址:http://dl.mycat.org.cn/1.6.7.1/

mkdir -p /data/mycat
cd /data/mycat
wget http://dl.mycat.org.cn/1.6.7.1/Mycat-server-1.6.7.1-release-20190627191042-linux.tar.gz

1.2.2 编写Dockerfile

FROM java:8
USER root
COPY Mycat-server-1.6.7.1-release-20190627191042-linux.tar.gz /
RUN tar -zxf /Mycat-server-1.6.7.1-release-20190627191042-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"]

1.2.3 获取配置文件

tar -zxvf Mycat-server-1.6.5-release-20180122220033-linux.tar.gz 
mv mycat/conf/ conf

 目录结构如下:

1.2.4 修改mycat配置文件
真正需要修改的只有三个文件,具体官方文档写的很详细;
(1):schema.xml ,(2):server.xml,(3):rule.xml;
现在搭建的是Mysql读写分离,修改schema.xml、server.xm即可。rule.xml用于分表分库配置文件。

schema.xml文件代码:

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
   <schema name="mycat" checkSQLschema="false" sqlMaxLimit="100">
        <table name="user" primaryKey="ID" dataNode="dn1,dn2"  />
    </schema>

    <dataNode name="dn1" dataHost="localhost1" database="demo2" />
    <dataNode name="dn2" dataHost="localhost1" database="demo3" />

    <dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"
        writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
        <heartbeat>select user()</heartbeat>
        <writeHost host="hostM1" url="172.18.0.10:3306" user="root" password="root">
            <readHost host="hostM1" url="172.18.0.11:3306" user="root" password="root"> 
        </readHost>
            </writeHost>
    </dataHost>

</mycat:schema>

server.xm文件代码:

<?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/">
   

   <!-- 读写都可用的用户 -->
    <user name="root" defaultAccount="true">
        <property name="password">123456</property>
        <property name="schemas">mycat</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>

    <!-- 只读用户 -->
    <user name="user">
        <property name="password">user</property>
        <property name="schemas">mycat</property>
        <property name="readOnly">true</property>
    </user>

</mycat:server>

1.2.5 执行Dockerfile生成镜像

cd /data/mycat/
docker build -t mycat:1.6 . 

1.2.6 编写mycat启动脚本($PWD表示当前目录)

docker run -d  -p:8066:8066 --name mysql-mycat --net docker-network --ip 172.18.0.20 -p 9066:9066 -v $PWD/logs:/mycat/logs -v $PWD/conf:/mycat/conf mycat:1.6

效果如下:

 有时候 docker run 启动容器失败,需要删除以下自动生成的logs文件夹,再重新docker run

 或使用 docker logs mysql-mycat 查看报错原因

docker logs mysql-mycat

 1.2.7 验证效果

 

posted @ 2020-07-08 21:58  一个人的孤独自白  阅读(1223)  评论(0编辑  收藏  举报