MyCat_下载配置

下载安装

请参照官网的文档,官网文档非常详细。建议测试阶段使用源码运行,有问题可以debug。

基本配置

schema.xml

配置了三个节点dn0~1
z_user使用test-rule规则进行分片
z_order是z_user的子表,使用er分片
z_dual使用z_dual-rule规则进行分片
z_dict配置的为全局表

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://org.opencloudb/">
    <schema name="MYSQLDB" checkSQLschema="false">
        <table name="z_user" primaryKey="ID" rule="test-rule" dataNode="dn0,dn1,dn2">
            <childTable parentKey="ID" name="z_order" joinKey="p_id"/>
        </table>
        <table name="z_dual" primaryKey="ID" rule="z_dual-rule" dataNode="dn0,dn1,dn2"/>
        <table name="z_dict" primaryKey="ID" type="global" dataNode="dn0,dn1,dn2"/>
    </schema>
    <dataNode name="dn0" dataHost="mysql_host0" database="mycat_db0" />
    <dataNode name="dn1" dataHost="mysql_host1" database="mycat_db1" />
    <dataNode name="dn2" dataHost="mysql_host2" database="mycat_db2" />
    <dataHost name="mysql_host0" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
        <heartbeat>select user()</heartbeat>
        <writeHost host="hostM1" url="ip:3306" user="root" password="XXXX">
        </writeHost>
    </dataHost>
    <dataHost name="mysql_host1" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
        <heartbeat>select user()</heartbeat>
        <writeHost host="hostM1" url="ip:3306" user="root" password="XXXX">
        </writeHost>
    </dataHost>
    <dataHost name="mysql_host2" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
        <heartbeat>select user()</heartbeat>
        <writeHost host="hostM1" url="ip:3306" user="root" password="XXXX">
        </writeHost>
    </dataHost>
</mycat:schema>

rule.xml

添加了schema中用到的两个分片规则

    <tableRule name="test-rule">
        <rule>
            <columns>user_code</columns>
            <algorithm>test-rule-func</algorithm>
        </rule>
    </tableRule>
    <tableRule name="z_dual-rule">
        <rule>
            <columns>id</columns>
            <algorithm>z_dual-rule-func</algorithm>
        </rule>
    </tableRule>
    <!--...-->
    <function name="test-rule-func" class="org.opencloudb.route.function.PartitionByHashMod">
        <property name="count">3</property>
    </function>
    <function name="z_dual-rule-func" class="org.opencloudb.route.function.PartitionByMod">
        <property name="count">3</property>
    </function>

server.xml

配置访问用的用户名密码等

<mycat:server xmlns:mycat="http://org.opencloudb/">
    <user name="yourUsername">
        <property name="password">yourPassword</property>
        <property name="schemas">MYSQLDB</property>
    </user>
</mycat:server>

启动

  • 如果是源码的方式,运行这个类 org.opencloudb.MycatStartup
  • 如果是下载的编译好的启动 bin\startup_nowrap.bat

如果启动信息不报错,则表明启动成功

测试

测试时jdbc的连接方式与mysql一样,只要修改相应的数据库名称和用户名密码等。

package com.am.mycatclient;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.am.mycatclient.datasource.DataSourceFactory;
import com.am.mycatclient.datasource.TableInfo;
public class App {
    public static void main(String[] args) throws Exception {
        DataSourceFactory ds = DataSourceFactory.instance();
        ds.initDataSource("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:8066/MYSQLDB?autoReconnect=true&useUnicode=true&characterEncoding=utf8", "yourUsername", "yourPassword");
        testQuery(ds);
    }
    public static void testQuery(DataSourceFactory ds) {
        PreparedStatement pst = null;
        Connection con = null;
        try {
            con = ds.getConnection();
            String querySql = "select * from z_dict";
            pst = con.prepareStatement(querySql);
            ResultSet result = pst.executeQuery();
            while (result.next()) {
                String id = result.getString("id");
                System.out.println(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        } finally {
            try {
                pst.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}

转载于:https://my.oschina.net/sandant/blog/698882

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值