一、什么是MYCAT:
一个彻底开源的,面向企业应用开发的大数据库集群
支持事务、ACID、可以替代MySQL的加强版数据库
一个可以视为mysql集群的企业级数据库,用来替代昂贵的Oracle集群
一个融合内存缓存技术、NoSQL技术、HDFS大数据的新型SQL Server
结合传统数据库和新型分布式数据仓库的新一代企业级数据库产品
一个新颖的数据库中间件产品
二、 MyCat是一个基于cobar兴起的开源数据库中间件系统,当前深受广大开源爱好者的追捧以及DBA粉丝们的广泛研究。主要是面对解决高并发,高负载,海量存储等屏颈。
两张表users和test,两个数据库test,dmeo(两个库在一个数据库实例上)
users在test中存储。
test在dmeo中存储。
-- ----------------------------
-- Table structure for `users`
-- ----------------------------
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_name` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`phone` varchar(11) DEFAULT NULL,
`create_time` datetime NOT NULL,
`update_time` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT='用户测试表';
开始使用( mycat官方网站上下载安装包,网址为mycat )
然后在conf目录下编辑service.xml,rule.xml,schema.xml、warpper.conf四个文件。
1、service.xml主要配置mycat服务的参数,比如端口号,myact用户名和密码使用的逻辑数据库等
2、rule.xml主要配置路由策略,主要有分片的片键,拆分的策略(取模还是按区间划分等)
3、schema.xml文件主要配置数据库的信息,例如逻辑数据库名称,物理上真实的数据源以及表和数据源之间的对应关系和路由策略等。
4、warpper 日志:mycat启动,停止,添加为服务等都会记录到此日志文件,如果系统环境配置错误或缺少配置时,导致 Mycat 无法启动,可以通过查看 warrpper.log 定位具体错误原因。warrpper.log可以删除,删除后会自动重新生成该日志
配置如下所示:
需要修改的配置文件
(1)**server.xml**
<mycat:server xmlns:mycat="http://io.mycat/">
<system>
<property name="useSqlStat">0property>
<property name="useGlobleTableCheck">0property>
<property name="sequnceHandlerType">2property>
<property name="processorBufferPoolType">0property>
<property name="defaultSqlParser">druidparserproperty>
<property name="mutiNodeLimitType">1property>
<property name="serverPort">8066property>
<property name="managerPort">9066property>
<property name="handleDistributedTransactions">0property>
<property name="useOffHeapForMerge">1property>
<property name="memoryPageSize">1mproperty>
<property name="spillsFileBufferSize">1kproperty>
<property name="useStreamOutput">0property>
<property name="systemReserveMemorySize">384mproperty>
<property name="useZKSwitch">trueproperty>
system>
<user name="root">
<property name="password">rootproperty>
<property name="schemas">TESTDBproperty>
user>
<user name="user">
<property name="password">userproperty>
<property name="schemas">TESTDBproperty>
<property name="readOnly">trueproperty>
user>
mycat:server>
(2)rule.xml
<mycat:rule xmlns:mycat="http://io.mycat/">
<tableRule name="rule1">
<rule>
<columns>idcolumns>
<algorithm>func1algorithm>
rule>
tableRule>
<tableRule name="rule2">
<rule>
<columns>user_idcolumns>
<algorithm>func1algorithm>
rule>
tableRule>
<tableRule name="sharding-by-intfile">
<rule>
<columns>sharding_idcolumns>
<algorithm>hash-intalgorithm>
rule>
tableRule>
<tableRule name="auto-sharding-long">
<rule>
<columns>idcolumns>
<algorithm>rang-longalgorithm>
rule>
tableRule>
<tableRule name="mod-long">
<rule>
<columns>idcolumns>
<algorithm>mod-longalgorithm>
rule>
tableRule>
<tableRule name="sharding-by-murmur">
<rule>
<columns>idcolumns>
<algorithm>murmuralgorithm>
rule>
tableRule>
<tableRule name="crc32slot">
<rule>
<columns>idcolumns>
<algorithm>crc32slotalgorithm>
rule>
tableRule>
<tableRule name="sharding-by-month">
<rule>
<columns>create_timecolumns>
<algorithm>partbymonthalgorithm>
rule>
tableRule>
<tableRule name="latest-month-calldate">
<rule>
<columns>calldatecolumns>
<algorithm>latestMonthalgorithm>
rule>
tableRule>
<tableRule name="auto-sharding-rang-mod">
<rule>
<columns>idcolumns>
<algorithm>rang-modalgorithm>
rule>
tableRule>
<tableRule name="jch">
<rule>
<columns>idcolumns>
<algorithm>jump-consistent-hashalgorithm>
rule>
tableRule>
<function name="murmur"
class="io.mycat.route.function.PartitionByMurmurHash">
<property name="seed">0property>
<property name="count">2property>
<property name="virtualBucketTimes">160property>
function>
<function name="crc32slot"
class="io.mycat.route.function.PartitionByCRC32PreSlot">
<property name="count">2property>
function>
<function name="hash-int"
class="io.mycat.route.function.PartitionByFileMap">
<property name="mapFile">partition-hash-int.txtproperty>
function>
<function name="rang-long"
class="io.mycat.route.function.AutoPartitionByLong">
<property name="mapFile">autopartition-long.txtproperty>
function>
<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
<property name="count">2property>
function>
<function name="func1" class="io.mycat.route.function.PartitionByLong">
<property name="partitionCount">8property>
<property name="partitionLength">128property>
function>
<function name="latestMonth"
class="io.mycat.route.function.LatestMonthPartion">
<property name="splitOneDay">24property>
function>
<function name="partbymonth"
class="io.mycat.route.function.PartitionByMonth">
<property name="dateFormat">yyyy-MM-ddproperty>
<property name="sBeginDate">2015-01-01property>
function>
<function name="rang-mod" class="io.mycat.route.function.PartitionByRangeMod">
<property name="mapFile">partition-range-mod.txtproperty>
function>
<function name="jump-consistent-hash" class="io.mycat.route.function.PartitionByJumpConsistentHash">
<property name="totalBuckets">3property>
function>
mycat:rule>
(3)schema.xml
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
<table name="test" primaryKey="ID" type="global" dataNode="dn1" />
<table name="users" primaryKey="ID" type="global" dataNode="dn2"/>
schema>
<dataNode name="dn1" dataHost="localhost1" database="dmeo" />
<dataNode name="dn2" dataHost="localhost1" database="test" />
<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="root">
<readHost host="hostS2" url="127.0.0.1:3306" user="root" password="root" />
writeHost>
dataHost>
mycat:schema>