MyCat基本配置

147 阅读1分钟

1,下载Mycat服务包

wget http://dl.mycat.org.cn/1.6.7.6/20210303094759/Mycat-server-1.6.7.6-release-20210303094759-linux.tar.gz
tar -zxvf Mycat-server-1.6.7.6-release-20210303094759-linux.tar.gz

2,配置Mycat

1server.xml 系统配置信息
2,schema.xml 逻辑库、逻辑表、表的分片规则、数据源
3,rule.xml 分片规则和分片算法

server.xml

//配置用户名和密码以及允许访问的库
<user name="root">
        <property name="password">123456</property>
        <property name="schemas">imall,gupao</property>
</user>

schema.xml

    <schema name="imall" checkSQLschema="false" sqlMaxLimit="100">
            <table name="customer" primaryKey="id" dataNode="122-imall,123-imall,124-imall" rule="auto-sharding-long" />
            <table name="order_info" dataNode="122-imall,123-imall,124-imall" rule="mod-long-order">
                    <childTable name="order_detail" primaryKey="id" joinKey="order_id" parentKey="order_id"> </childTable>
            </table>
    </schema>


    <schema name="gupao" checkSQLschema="false" sqlMaxLimit="100">
            <table name="student"  primaryKey="sid" dataNode="122-gupao,123-gupao,124-gupao" rule="mod-long" />
            <!-- 指定放在某个节点上,不分片 -->
            <table name="noshard" primaryKey="id" autoIncrement="true" dataNode="122-gupao" />
            <!-- 全局表,增删改的语句会发送到三个节点同时执行 -->
            <table name="dict" primaryKey="id" type="global" dataNode="122-gupao,123-gupao,124-gupao" />
            <!-- 单库分表,只在一个节点上根据月份分成三个表  -->
            <table name="fee" primaryKey="id" subTables="fee2025$1-3" dataNode="122-gupao" rule="sharding-by-month" />
    </schema>

    <dataNode name="122-imall" dataHost="host122" database="imall" />
    <dataNode name="123-imall" dataHost="host123" database="imall" />
    <dataNode name="124-imall" dataHost="host124" database="imall" />

    <dataNode name="122-gupao" dataHost="host122" database="gupao" />
    <dataNode name="123-gupao" dataHost="host123" database="gupao" />
    <dataNode name="124-gupao" dataHost="host124" database="gupao" />

    <dataHost name="host122" maxCon="1000" minCon="10" balance="0"
                      writeType="0" dbType="mysql" dbDriver="jdbc" switchType="1"  slaveThreshold="100">
            <heartbeat>select user()</heartbeat>
            <writeHost host="hostM1" url="jdbc:mysql://host1:3306" user="root"
                               password="123456">
            </writeHost>
    </dataHost>

    <dataHost name="host123" maxCon="1000" minCon="10" balance="0"
                      writeType="0" dbType="mysql" dbDriver="jdbc" switchType="1"  slaveThreshold="100">
            <heartbeat>select user()</heartbeat>
            <writeHost host="hostM1" url="jdbc:mysql://host2:3306" user="root"
                               password="123456">
            </writeHost>
    </dataHost>
    
    <dataHost name="host124" maxCon="1000" minCon="10" balance="0"
                          writeType="0" dbType="mysql" dbDriver="jdbc" switchType="1"  slaveThreshold="100">
            <heartbeat>select user()</heartbeat>
            <!-- can have multi write hosts -->
            <writeHost host="hostM1" url="jdbc:mysql://host3:3306" user="root"
                               password="123456">
            </writeHost>
    </dataHost>
    

rule.xml新增

<tableRule name="mod-long-order">
        <rule>
                <columns>order_id</columns>
                <algorithm>mod-long</algorithm>
        </rule>
</tableRule>

Mycat服务启动成功!

image.png mycat的默认访问端口为8066 在122,123,124上分别创建了两个实例imall和gupao,并且添加表之后,在mycat的schema.xml文件中配置了imall和gupao,并且在server.xml配置了root的schema权限之后,就可以通过mycat的8066端口像登录mysql一样登录mycat,就可以看到配置的这两个schema了。 image.png