MyCat是国内一个比较优秀的开源项目,一个实现数据库分布式服务的中间件方案,提供多种分区分表算法及其实现。
安装与运行:
- 直接上官网 http://www.mycat.org.cn 下载 1.6 release,提供 for win/linux版本。
- 解压至 E:\Software\Mycat-server-1.6
- console下,goto E:\Software\Mycat-server-1.6\bin,安装服务: mycat install, 运行: mycat restart, 看看有没错误抛出,没有ok。
各种配置实践:
我认为分布式数据库的控制难点有两个,一个是全局序列号的生成,一个是分片算法,下面是研究实践:
全局配置:
- server.xml, 配置用户:
<user name="developer">
<property name="password">developer</property>
<property name="schemas">testdb1,testdb2</property>
<!--<property name="readOnly">true</property> -->
</user>
- schema.xml, 配置dn1到3, 三个数据节点,(测试环境中,dbx与localhost的dbx库相对应)
<dataNode name="dn1" dataHost="localhost1" database="db1" />
<dataNode name="dn2" dataHost="localhost1" database="db2" />
<dataNode name="dn3" dataHost="localhost1" database="db3" />
<!--<dataNode name="dn4" dataHost="sequoiadb1" database="SAMPLE" />
<dataNode name="jdbc_dn1" dataHost="jdbchost" database="db1" />
<dataNode name="jdbc_dn2" dataHost="jdbchost" database="db2" />
<dataNode name="jdbc_dn3" dataHost="jdbchost" database="db3" /> -->
<dataHost name="localhost1" 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="developer" password="developer">
<!-- can have multi read hosts -->
<!-- <readHost host="hostS2" url="192.168.1.200:3306" user="developer" password="developer" /> -->
</writeHost>
<!-- <writeHost host="hostM2" url="localhost:3316" user="developer" password="developer"/> -->
</dataHost>
Plan1:
- 全局序列号生成方式: 本地文件方式, 分片算法: 范围约定 (auto-sharding-long)
配置:
- conf\server.xml , 0
- conf\sequence_conf.properties
#default global sequence
GLOBAL.CURID=10002
GLOBAL.MINID=10001
GLOBAL.HISIDS=
GLOBAL.MAXID=20000
# self define sequence
EMPLOYEE.HISIDS=
EMPLOYEE.MINID=1001
EMPLOYEE.MAXID=2000
EMPLOYEE.CURID=1000
- conf\schema.xml,
测试:
- 用数据库客户端连接上 localhost:8066, testdb1;
- 运行
insert into employee(id,name,sharding_id) values(next value for MYCATSEQ_GLOBAL@@hostname,10000);
"insert into employee(id,name,sharding_id) values(next value for MYCATSEQ_EMPLOYEE,@@hostname,10000);"
观察结果:
1001 BEN-NOTEBOOK 10000
1002 BEN-NOTEBOOK 10000
10001 BEN-NOTEBOOK 10000
10002 BEN-NOTEBOOK 10000
10003 BEN-NOTEBOOK 10000
10003 BEN-NOTEBOOK 10000
待续...