MyCat的核心概念
1.逻辑数据库 schema.xml
2.分片规则 rule.xml
单库大表拆分
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<!-- 逻辑库 -->
<!--
checkSQLschema=false 处理表名的序号
sqlMaxLimit = 100 只返回100条
-->
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
<!-- 逻辑表 -->
<!--
name:逻辑表名
dataNode:节点
rule:分片规则
mod-long:取模
-->
<table name="company" subTables="company$1-3" dataNode="dn1" rule="mod-long"/>
</schema>
<!-- 数据节点 -->
<dataNode name="dn1" dataHost="localhost1" database="db" />
<!-- host配置 -->
<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="localhost:3306" user="root"
password="123456">
</writeHost>
</dataHost>
</mycat:schema>
跨库大表拆分
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<!-- 逻辑库 -->
<!--
checkSQLschema=false 处理表名的序号
sqlMaxLimit = 100 只返回100条
-->
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" randomDataNode="dn1">
<!-- 逻辑表 -->
<!--
name:逻辑表名
dataNode:节点
rule:分片规则
mod-long:取模
-->
<table name="company" subTables="company$1-3" dataNode="dn1" rule="mod-long"/>
<table name="record" dataNode="dn1,dn2,dn1" rule="mod-long"/>
</schema>
<!-- 数据节点 -->
<dataNode name="dn1" dataHost="localhost1" database="db1" />
<dataNode name="dn2" dataHost="localhost1" database="db2" />
<dataNode name="dn3" dataHost="localhost1" database="db3" />
<!-- host配置 -->
<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="localhost:3306" user="root"
password="123456">
</writeHost>
</dataHost>
</mycat:schema>
读写分离
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1">
</schema>
<!-- 数据节点 -->
<dataNode name="dn1" dataHost="localhost1" database="db" />
<!-- host配置 -->
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="0"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<!-- 心跳检测 -->
<heartbeat>show slave status</heartbeat>
<writeHost host="hostM1" url="localhost:3306" user="root"
password="123456">
<readHost host="localhost1" url="localhost:3306" password="root" user="123456" />
</writeHost>
</dataHost>
</mycat:schema>