用到的类方法:io.mycat.route.function.PartitionByFileMap
分片类型:
按数字分片:sharding-by-intfile
按照字符串分片,就是改下rule里的function的<property name="type">1</property>
1、schema.xml信息
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
<table name="sharding_by_intfile" primaryKey="id" dataNode="dn0,dn1,dn2"
rule="sharding-by-intfile" />
</schema>
<dataNode name="dn0" dataHost="dh-1" database="db0"/>
<dataNode name="dn1" dataHost="dh-1" database="db1"/>
<dataNode name="dn2" dataHost="dh-1" database="db2"/>
<dataHost name="dh-1" maxCon="1000" minCon="10" balance="0"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="localhost:3306" user="root"
password="123456">
</writeHost>
</dataHost>
</mycat:schema>2、rule.xml信息
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mycat:rule SYSTEM "rule.dtd">
<mycat:rule xmlns:mycat="http://io.mycat/">
<tableRule name="sharding-by-intfile">
<rule>
<columns>age</columns>
<algorithm>hash-int</algorithm>
</rule>
</tableRule>
<function name="hash-int"
class="io.mycat.route.function.PartitionByFileMap">
<property name="mapFile">fun/partition-hash-int.txt</property>
<property name="type">0</property>
<property name="defaultNode">0</property>
</function>
</mycat:rule>
3、规则文件信息partition-hash-int.txt
11=1
12=2
4、测试用到的sql
=======================枚举================
CREATE TABLE sharding_by_intfile (`age` int NOT NULL ,`db_nm` varchar(20) NULL);
INSERT INTO `sharding_by_intfile` (age,db_nm) VALUES (10, database());
INSERT INTO `sharding_by_intfile` (age,db_nm) VALUES (11, database());
INSERT INTO `sharding_by_intfile` (age,db_nm) VALUES (12, database());
select * from `sharding_by_intfile`;
5、注意事项:
schema里的table的dataNode节点个数必须:大于等于 partition-hash-int.txt里的配置个数
本文详细介绍使用MyCat中间件进行数据库分片的配置过程,包括schema.xml与rule.xml的具体设置,以及规则文件partition-hash-int.txt的内容。通过具体示例展示如何实现按数字分片,并给出测试SQL语句。
595

被折叠的 条评论
为什么被折叠?



