- 先进行 mycat/conf 下的schema.xml和rule.xml配置
- type=“global” 每个节点数据一致,其中表的主键可加可不加
全局表数据自动克隆到所有已定义的数据节点,因此所有节点的数据都一样
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="DBNAME" checkSQLschema="false" sqlMaxLimit="5000">
<!-- sqlMaxLimit限定了每次最多插入5000条数据 -->
<table name="cd_price" type="global" dataNode="dn0,dn1,dn2,dn3,dn4,dn5,dn6,dn7" />
<table name="cd_sale" primaryKey="ID" type="global" dataNode="dn0,dn1,dn2,dn3,dn4,dn5,dn6,dn7" />
</schema>
<schema name="DBNAME2" checkSQLschema="false" sqlMaxLimit="5000000">
<table name="cd_order" type="global" dataNode="dn0,dn1,dn2,dn3,dn4,dn5,dn6,dn7" />
</schema>
<dataNode name="dn0" dataHost="dbhost2" database="db0" />
<dataNode name="dn1" dataHost="dbhost2" database="db1" />
<dataNode name="dn2" dataHost="dbhost2" database="db2" />
<dataNode name="dn3" dataHost="dbhost0" database="db3" />
<dataNode name="dn4" dataHost="dbhost0" database="db4" />
<dataNode name="dn5" dataHost="dbhost0" database="db5" />
<dataNode name="dn6" dataHost="dbhost1" database="db6" />
<dataNode name="dn7" dataHost="dbhost1" database="db7" /> <!-- 定义了8个节点 ,每个节点相当于一个mysql数据库-->
<dataHost name="dbhost0" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat> <!-- can have multi write hosts -->
<writeHost host="hostM1" url="ip1:3306" user="username" password="password">
</writeHost>
</dataHost>
<dataHost name="dbhost1" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="ip2:3306" user="username" password="password">
</writeHost>
</dataHost>
<dataHost name="dbhost2" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="ip3:3306" user="username" password="password">
</writeHost>
</dataHost>
</mycat:schema>
- 根据定义 rule 来分配每个节点流入的数据
<table name="cd_customer" primaryKey="CU_CUSTOMER_ID" dataNode="dn0,dn1,dn2,dn3,dn4,dn5,dn6,dn7" rule="mod-long-customerid" >
rule.xml,确认合适的function ,然后定义 tableRule
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mycat:rule SYSTEM "rule.dtd">
<mycat:rule xmlns:mycat="http://io.mycat/">
<tableRule name="sharding-by-murmur">
<rule>
<columns>id</columns>
<algorithm>murmur</algorithm>
</rule>
</tableRule>
<tableRule name="mod-long-customerid">
<rule>
<columns>cu_customer_id</columns>
<algorithm>mod-long-8</algorithm>
</rule>
</tableRule>
<function name="murmur" class="io.mycat.route.function.PartitionByMurmurHash">
<property name="seed">0</property><!-- 默认是0 -->
<property name="count">8</property><!-- 要分片的数据库节点数量,必须指定,否则没法分片 -->
<property name="virtualBucketTimes">160</property><!-- 一个实际的数据库节点被映射为这么多虚拟节点,默认是160倍,也就是虚拟节点数是物理节点数的160倍 -->
<!-- <property name="weightMapFile">weightMapFile</property> 节点的权重,没有指定权重的节点默认是1。以properties文件的格式填写,以从0开始到count-1的整数值也就是节点索引为key,以节点权重值为值。
所有权重值必须是正整数,否则以1代替。
<property name="bucketMapPath">/etc/mycat/bucketMapPath</property>
用于测试时观察各物理节点与虚拟节点的分布情况,如果指定了这个属性,会把虚拟节点的murmur hash值与物理节点的映射按行输出到这个文件,没有默认值,如果不指定,就不会输出任何东西 -->
</function>
<function name="rang-long" class="io.mycat.route.function.AutoPartitionByLong">
<property name="mapFile">autopartition-long.txt</property>
</function>
<function name="mod-long-8" class="io.mycat.route.function.PartitionByMod">
<!-- how many data nodes -->
<property name="count">8</property>
</function>
</mycat:rule>
- 修改配置schema.xml和rule.xml后 重启MYCAT
cd /usr/local/mycat/bin ##进入mycat的安装路径bin目录下
./mycat status ##查看状态
./mycat stop ##停止
./mycat start ##启动
- 最后执行create 创建表语句
备注:执行建表语句 create table tbname(field1 VARCHAR(64),fieldn VARCHAR(64) );
tbname与 ( 之间
时不要有多余的空格,否则建表时可能报错