1. mycat下载
下载地址 http://dl.mycat.io/1.6-RELEASE/
2.解压,配置环境变量,path=…\Mycat-server-1.6-RELEASE-20161028204710-win\mycat\bin[mycat安装地址]
3.修改配置文件【目录:…\Mycat-server-1.6-RELEASE-20161028204710-win\mycat\conf】
-
修改wrapper.conf
-
修改server.xml,配置用户名和密码[如果server.xml配置了多个schema,那么在schema.xml要有对应的schema匹配。
<user name="root">
<property name="password">123456</property>
<property name="schemas">TESTDB</property>
<!-- 表级 DML 权限设置 -->
<!--
<privileges check="false">
<schema name="TESTDB" dml="0110" >
<table name="tb01" dml="0000"></table>
<table name="tb02" dml="1111"></table>
</schema>
</privileges>
-->
</user>
<user name="user">
<property name="password">user</property>
<property name="schemas">TESTDB</property>
<property name="readOnly">true</property>
</user>
- 修改schema.xml (定义逻辑库,表、分片节点等内容),如果是应用于多个项目,可以设置多个schema
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
<table name="user" dataNode="dn1,dn2,dn3" autoIncrement = "true" rule="auto-sharding-long" />
<table name="goods" dataNode="dn1,dn2,dn3" autoIncrement = "true" rule="auto-sharding-long" />
<table name="tests" type="global" dataNode="dn1,dn2" />
</schema>
<dataNode name="dn1" dataHost="localhost" database="db1" />
<dataNode name="dn2" dataHost="localhost" database="db2" />
<dataNode name="dn3" dataHost="localhost" database="db3" />
<dataHost name="localhost" 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="localhost:3306" user="root"
password="123456">
<!-- can have multi read hosts -->
<!-- <readHost host="hostS2" url="192.168.1.200:3306" user="root" password="xxx" /> -->
</writeHost>
<!-- <writeHost host="hostS1" url="localhost:3316" user="root"
password="123456" /> -->
</dataHost>
注:简单介绍一下Mycat分库规则
MYCAT常用的分片规则如下,另外还有一些其他分片方式这里不全部列举:
(1)分片枚举: sharding-by-intfile
(2)主键范围约定: auto-sharding-long 此分片适用于,提前规划好分片字段某个范围属于哪个分片
(3)一致性hash: sharding-by-murmur
(4)字符串hash解析: sharding-by-stringhash
(5)按日期(天)分片:sharding-by-date
(6)按单月小时拆分: sharding-by-hour
(7)自然月分片: sharding-by-month
(8)取模: mod-long 此规则为对分片字段求摸运算
(9)取模范围约束: sharding-by-pattern 此种规则是取模运算与范围约束的结合,主要为了后续数据迁移做准备,即可以自主决定取模后数据的节点分布
参考链接:https://blog.csdn.net/dingchang3060/article/details/80408765