Scenes
For example, a large company, there is a table holds the record transaction information. The table data is large, but the query, update, basically by subsidiaries to operate.
We can divide the table by a subsidiary of numbers. For example, a subsidiary of No. 1 records stored in the transaction table record_1, empathy for the preservation of transaction records subsidiary number 2 to the record_2 in ...
achieve
New database instance in mysql dbcompany, and a new record0-record3, a total of four tables, as follows
CREATE TABLE `record_0` (
`id` int(11) NOT NULL,
`companyid` int(11) DEFAULT NULL COMMENT '子公司编号',
`info` varchar(255) DEFAULT NULL COMMENT '业务信息',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `record_1` (
`id` int(11) NOT NULL,
`companyid` int(11) DEFAULT NULL COMMENT '子公司编号',
`info` varchar(255) DEFAULT NULL COMMENT '业务信息',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `record_2` (
`id` int(11) NOT NULL,
`companyid` int(11) DEFAULT NULL COMMENT '子公司编号',
`info` varchar(255) DEFAULT NULL COMMENT '业务信息',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `record_3` (
`id` int(11) NOT NULL,
`companyid` int(11) DEFAULT NULL COMMENT '子公司编号',
`info` varchar(255) DEFAULT NULL COMMENT '业务信息',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Real server configuration information
By modifying the conf / schema.xml, to the real server configuration information, and to divide the database node, node specifies the data table is located.
Note that key information subTables="record_$0-3"
, and rule="rule-record"
, by the regular slices, the data will fall into record_0 to record_3.
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<!-- name为逻辑数据库名 -->
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
<!-- 配置对应表名、子表名、对应数据库、规则名 -->
<table name="record" subTables="record_$0-3" dataNode="dn1" rule="rule-record" />
</schema>
<!-- 真实数据库名 -->
<dataNode name="dn1" dataHost="localhost1" database="dbcompany" />
<!-- 配置物理数据库连接信息 -->
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="127.0.0.1:3306" user="root" password="Easy@0122"/>
</dataHost>
</mycat:schema>
Configure routing rules
In the schema.xml we have developed a storage node record table, and set up routing rules Name rule-record, and then we set the rules specific strategies.
Modify conf / role.xml, configuration rules below, we note that according companyid列
to which the rule applies.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mycat:rule SYSTEM "rule.dtd">
<mycat:rule xmlns:mycat="http://io.mycat/">
<!-- 规则 -->
<tableRule name="rule-record">
<rule>
<!-- 规则应用的列 -->
<columns>companyid</columns>
<algorithm>mod-long</algorithm>
</rule>
</tableRule>
<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
<!-- 此处设置有多少个子表即可 -->
<property name="count">4</property>
</function>
</mycat:rule>
MyCat service configuration information
Through the above two configuration files, we have specified a library table, sub-table routing rules, following which we will pass MyCat exposed, allowing the client to access it.
By modifying the conf / server.xml configuration MyCat foreign service information, mainly user name, password, and database name specified above abstract TESTDB.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mycat:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://io.mycat/">
<!-- system部分采用默认即可 -->
<system>
<property name="nonePasswordLogin">0</property> <!-- 0为需要密码登陆、1为不需要密码登陆 ,默认为0,设置为1则需要指定默认账户-->
<property name="useHandshakeV10">1</property>
<property name="useSqlStat">0</property> <!-- 1为开启实时统计、0为关闭 -->
<property name="useGlobleTableCheck">0</property> <!-- 1为开启全加班一致性检测、0为关闭 -->
<property name="sequnceHandlerType">2</property>
<property name="subqueryRelationshipCheck">false</property> <!-- 子查询中存在关联查询的情况下,检查关联字段中是否有分片字段 .默认 false -->
<!--默认为type 0: DirectByteBufferPool | type 1 ByteBufferArena | type 2 NettyBufferPool -->
<property name="processorBufferPoolType">0</property>
<!--分布式事务开关,0为不过滤分布式事务,1为过滤分布式事务(如果分布式事务内只涉及全局表,则不过滤),2为不过滤分布式事务,但是记录分布式事务日志-->
<property name="handleDistributedTransactions">0</property>
<!--off heap for merge/order/group/limit 1开启 0关闭-->
<property name="useOffHeapForMerge">1</property>
<!--单位为m-->
<property name="memoryPageSize">64k</property>
<!--单位为k-->
<property name="spillsFileBufferSize">1k</property>
<property name="useStreamOutput">0</property>
<!--单位为m-->
<property name="systemReserveMemorySize">384m</property>
<!--是否采用zookeeper协调切换 -->
<property name="useZKSwitch">false</property>
</system>
<!-- 设置访问的用户名密码 -->
<user name="root" defaultAccount="true">
<property name="password">123456</property>
<!-- 注意此处是之前设定的抽象数据库名称 -->
<property name="schemas">TESTDB</property>
</user>
</mycat:server>
Start the test
Double-click bin / startup_nowrap.bat start MyCat, then use Navicat or other tools can be connected MyCat virtual database.
We perform the following eight sql statement
insert into record (id,companyid,info)values(1,1,'test');
insert into record (id,companyid,info)values(4,2,'test');
insert into record (id,companyid,info)values(2,3,'test');
insert into record (id,companyid,info)values(3,4,'test');
insert into record (id,companyid,info)values(5,5,'test');
insert into record (id,companyid,info)values(7,6,'test');
insert into record (id,companyid,info)values(6,7,'test');
insert into record (id,companyid,info)values(8,8,'test');
After execution, we look at real physical database:
record_0 data are as follows, indicating that according to companyid modulo matching our strategy into effect!
The other three libraries also operate this rule, that data falling into the record_[companyid%4]
table.