jdk安装配置
- 首先去oracle官网下载并安装jdk8,添加环境变量,JAVA_HOME设置为D:\Worksoftware\Java\jdk1.8
- CLASSPATH设置为.;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar
- path系统变量追加%JAVA_HOME%\bin;
Mycat安装配置
-
首先添加Windows环境变量,MYCAT_HOME设置为安装目录E:\WorkSoftWare\MycatServer1.5
-
为了降低资源占用,mycat的jvm设置在startup_nowrap.bat,打开后可以清楚看到如下配置:
“%JAVA_CMD%” -server -Xms1G -Xmx2G -XX:MaxPermSize=64M -XX:+AggressiveOpts -XX:MaxDirectMemorySize=1G -DMYCAT_HOME=%MYCAT_HOME% -cp “..\conf;..\lib*” io.mycat.MycatStartup
这里将-Xms1G改成-Xms512M,-Xmx2G改成-Xmx1024M,保存后重新启动即可.
- wrapper.conf文件里的改成wrapper.java.command=E:\WorkSoftWare\jdk1.8\bin\java.exe
Mycat启动的配置
schema.xml配置文件,如果分库在不同的服务器,可以配置两个datahost;如果在一个datahost中配置多个writeHost,则为主从配置。type=”global”时,为全局表
?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://org.opencloudb/" >
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
<table name="travelrecord" dataNode="dn1,dn2,dn3" rule="auto-sharding-long" />
<table name="company" primaryKey="ID" dataNode="dn3,dn2,dn1" rule="mod-long"/>
<table name="goods" primaryKey="ID" type="global" dataNode="dn1,dn2" />
<table name="hotnews" primaryKey="ID" dataNode="dn1,dn2,dn3" rule="mod-long" />
<table name="employee" primaryKey="ID" dataNode="dn1,dn2" rule="sharding-by-intfile" />
<table name="customer" primaryKey="ID" dataNode="dn1,dn2" rule="sharding-by-intfile">
<childTable name="orders" primaryKey="ID" joinKey="customer_id" parentKey="id">
<childTable name="order_items" joinKey="order_id" parentKey="id" />
</childTable>
<childTable name="customer_addr" primaryKey="ID" joinKey="customer_id" parentKey="id" />
</table>
<table name="news_table" primaryKey="ID" type="global" dataNode="dn1,dn2,dn3" />
</schema>
<dataNode name="dn1" dataHost="localhost1" database="mycatDB1" />
<dataNode name="dn2" dataHost="localhost1" database="mycatDB2" />
<dataNode name="dn3" dataHost="localhost1" database="mycatDB3" />
<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>
<span class="hljs-pi" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><?xml version="1.0"?></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"></span><span class="hljs-doctype" style="color: rgb(102, 0, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><!DOCTYPE mycat:schema SYSTEM "schema.dtd"></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"></span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">mycat:schema</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">xmlns:mycat</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"http://org.opencloudb/"</span> ></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"></span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><!--在这一行参数里面,schema name定义了可以在MyCAT前端显示的逻辑数据库的名字,checkSQLschema这个参数为False的时候,表明MyCAT会自动忽略掉表名前的数据库名,比如说mydatabase1.test1,会被当做test1;sqlMaxLimit指定了SQL语句返回的行数限制--></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">schema</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"TESTDB"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">checkSQLschema</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"false"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">sqlMaxLimit</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"100"</span>></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><!-- 主键范围规则 --></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><!-- 这一行代表在MyCAT前端会显示哪些表名,类似几行都代表一样的意思,这里强调的是表,而MyCAT并不会在配置文件里面定义表结构,如果在前端使用show create table ,MyCAT会显示正常的表结构信息,观察Debug日志,可以看到,MyCAT把命令分发给了dn1代表的数据库,然后把dn1的查询结果返回给了前端 可以判断,类似的数据库级别的一些查询指令,有可能是单独分发给某个节点,然后再把某个节点的信息返回给前端。 dataNode的意义很简单,这个逻辑表的数据存储在后端的哪几个数据库里面rule代表的是这个逻辑表students的具体切分策略,目前MyCAT只支持按照某一个特殊列,遵循一些特殊的规则来切分,如取模,枚举等,具体的留给之后细说 --></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">table</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"travelrecord"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">dataNode</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"dn1,dn2,dn3"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">rule</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"auto-sharding-long"</span> /></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">table</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"company"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">primaryKey</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"ID"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">dataNode</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"dn3,dn2,dn1"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">rule</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"mod-long"</span>/></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">table</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"goods"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">primaryKey</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"ID"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">type</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"global"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">dataNode</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"dn1,dn2"</span> /></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><!--求模分片随机规则 --></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">table</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"hotnews"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">primaryKey</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"ID"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">dataNode</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"dn1,dn2,dn3"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">rule</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"mod-long"</span> /></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">table</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"employee"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">primaryKey</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"ID"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">dataNode</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"dn1,dn2"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">rule</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"sharding-by-intfile"</span> /></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">table</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"customer"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">primaryKey</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"ID"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">dataNode</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"dn1,dn2"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">rule</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"sharding-by-intfile"</span>></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><!-- childtable我在测试中并没有实际用起来不过在MyCAT的设计文档里面有提到,childtable是一种依赖于父表的结构, 这意味着,childtable的joinkey会按照父表的parentKey的策略一起切分,当父表与子表进行连接, 且连接条件是childtable.joinKey=parenttable.parentKey时,不会进行跨库的连接. --></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">childTable</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"orders"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">primaryKey</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"ID"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">joinKey</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"customer_id"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">parentKey</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"id"</span>></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">childTable</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"order_items"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">joinKey</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"order_id"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">parentKey</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"id"</span> /></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">childTable</span>></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">childTable</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"customer_addr"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">primaryKey</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"ID"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">joinKey</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"customer_id"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">parentKey</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"id"</span> /></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">table</span>></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><!-- 全局表是自动克隆到所有定义的数据节点,这样可以与拆分节点的任何表连接查询,是在同一个数据节点--></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">table</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"news_table"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">primaryKey</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"ID"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">type</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"global"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">dataNode</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"dn1,dn2,dn3"</span> /></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">schema</span>></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">dataNode</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"dn1"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">dataHost</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"localhost1"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">database</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"mycatDB1"</span> /></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">dataNode</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"dn2"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">dataHost</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"localhost1"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">database</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"mycatDB2"</span> /></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">dataNode</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"dn3"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">dataHost</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"localhost1"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">database</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"mycatDB3"</span> /></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><!-- dataHost配置的是实际的后端数据库集群,大部分参数简单易懂,这里就不一个个介绍了,只介绍比较重要的两个参数,writeType和balance. --></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><!-- writeType和balance是用来控制后端集群的读写分离的关键参数,这里我用了双主双从的集群配置 这里的测试过程比较麻烦,所以直接贴结论: 1.balance=0时,读操作都在localhost上(localhost失败时,后端直接失败) 2.balance=1时,读操作会随机分散在localhost1和两个readhost上面(localhost失败时,写操作会在localhost1,如果localhost1再失败,则无法进行写操作) 3.balance=2时,写操作会在localhost上,读操作会随机分散在localhost1,localhost1和两个readhost上面(同上) 4.writeType=0时,写操作会在localhost上,如果localhost失败,会自动切换到localhost1,localhost恢复以后并不会切换回localhost进行写操作 5.writeType=1时,写操作会随机分布在localhost和localhost1上,单点失败并不会影响集群的写操作,但是后端的从库会无法从挂掉的主库获取更新,会在读数据的时候出现数据不一致 举例:localhost失败了,写操作会在localhost1上面进行,localhost1的主从正常运行,但是localhost的从库无法从localhost获取更新,localhost的从库于其他库出现数据不一致 --></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">dataHost</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"localhost1"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">maxCon</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"1000"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">minCon</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"10"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">balance</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"0"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">writeType</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"0"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">dbType</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"mysql"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">dbDriver</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"native"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">switchType</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"1"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">slaveThreshold</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"100"</span>></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">heartbeat</span>></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">select user()</span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">heartbeat</span>></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><!-- can have multi write hosts --></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">writeHost</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">host</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"hostM1"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">url</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"localhost:3306"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">user</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"root"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">password</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"123456"</span>></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><!-- can have multi read hosts --></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><!--<readHost host="hostS2" url="192.168.1.200:3306" user="root" password="xxx" />--></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">writeHost</span>></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"> </span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">dataHost</span>></span><span style="color: rgb(51, 51, 51); font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"></span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box; font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.2999992370605px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">mycat:schema</span>></span>
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mycat:server SYSTEM "server.dtd"><mycat:serverxmlns:mycat="http://org.opencloudb/"><system><propertyname="defaultSqlParser">druidparser</property></system><username="test"><propertyname="password">test</property><propertyname="schemas">TESTDB</property></user><username="user"><propertyname="password">user</property><propertyname="schemas">TESTDB</property><propertyname="readOnly">true</property></user></mycat:server>
rule.xml配置文
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mycat:rule SYSTEM "rule.dtd">
<mycat:rulexmlns:mycat="http://org.opencloudb/">
<tableRulename="rule1">
<rule>
<columns>
id
</columns>
<algorithm>
func1
</algorithm>
</rule>
</tableRule>
<tableRulename="rule2">
<rule>
<columns>
user_id
</columns>
<algorithm>
func1
</algorithm>
</rule>
</tableRule>
<tableRulename="sharding-by-intfile">
<rule>
<columns>
sharding_id
</columns>
<algorithm>
hash-int
</algorithm>
</rule>
</tableRule>
<tableRulename="auto-sharding-long">
<rule>
<columns>
id
</columns>
<algorithm>
rang-long
</algorithm>
</rule>
</tableRule>
<tableRulename="mod-long">
<rule>
<columns>
id
</columns>
<algorithm>
mod-long
</algorithm>
</rule>
</tableRule>
<tableRulename="sharding-by-murmur">
<rule>
<columns>
id
</columns>
<algorithm>
murmur
</algorithm>
</rule>
</tableRule>
<tableRulename="sharding-by-month">
<rule>
<columns>
create_date
</columns>
<algorithm>
partbymonth
</algorithm>
</rule>
</tableRule>
<tableRulename="latest-month-calldate">
<rule>
<columns>
calldate
</columns>
<algorithm>
latestMonth
</algorithm>
</rule>
</tableRule>
<tableRulename="auto-sharding-rang-mod">
<rule>
<columns>
id
</columns>
<algorithm>
rang-mod
</algorithm>
</rule>
</tableRule>
<tableRulename="jch">
<rule>
<columns>
id
</columns>
<algorithm>
jump-consistent-hash
</algorithm>
</rule>
</tableRule>
<functionname="murmur"class="org.opencloudb.route.function.PartitionByMurmurHash">
<propertyname="seed">
0
</property>
<propertyname="count">
2
</property>
<propertyname="virtualBucketTimes">
160
</property>
</function>
<functionname="hash-int"class="org.opencloudb.route.function.PartitionByFileMap">
<propertyname="mapFile">
partition-hash-int.txt
</property>
</function>
<functionname="rang-long"class="org.opencloudb.route.function.AutoPartitionByLong">
<propertyname="mapFile">
autopartition-long.txt
</property>
</function>
<functionname="mod-long"class="org.opencloudb.route.function.PartitionByMod">
<propertyname="count">
3
</property>
</function>
<functionname="func1"class="org.opencloudb.route.function.PartitionByLong">
<propertyname="partitionCount">
8
</property>
<propertyname="partitionLength">
128
</property>
</function>
<functionname="latestMonth"class="org.opencloudb.route.function.LatestMonthPartion">
<propertyname="splitOneDay">
24
</property>
</function>
<functionname="partbymonth"class="org.opencloudb.route.function.PartitionByMonth">
<propertyname="dateFormat">
yyyy-MM-dd
</property>
<propertyname="sBeginDate">
2015-01-01
</property>
</function>
<functionname="rang-mod"class="org.opencloudb.route.function.PartitionByRangeMod">
<propertyname="mapFile">
partition-range-mod.txt
</property>
</function>
<functionname="jump-consistent-hash"class="org.opencloudb.route.function.PartitionByJumpConsistentHash">
<propertyname="totalBuckets">
3
</property>
</function>
</mycat:rule>
cmd命令运行:
结果:
![这里写图片描述](https://img-blog.csdn.net/20160823000859828)
Mycat连接mysql结果
上图上面的mycatdb1,mycatdb2,mycatdb3数据库是真实物理数据库,TESTDB是中间件逻辑数据库,此时已经所有工作都准备好。