Mycat安装与配置详解
需要提前准备两个数据库以便于测试
由于github限制,所以以后新版本从以下地址下载
去这里下载 http://dl.mycat.io (GitHub的mycat: https://github.com/MyCATApache/Mycat-download)
可以看到最新的稳定版是1.6.7.1
之后的都是测试和开发版,所以我们下载稳定版的1.6.7.1
根据不同这里下载不同的版本(这里没有显示全,补过可以通过把鼠标移上去看左下角显示的链接信息)
因为我要下linux版本的。所以我下载第一个Mycat-server-1.6.7.1-release-20190213150257-lin的
下载后上传到Linux上,我的是centos6.5
解压安装包
tar -xvf Mycat-server-1.6.7.1-release-20190213150257-linux.tar.gz
可以看到解压出来的内容都放到mycat文件夹中了。
删除安装包,进入mycat文件夹
bin:命令文件
catlet:空的,扩展
conf:配置文件(server.xml,schema.xml,rule.xml等)
lib:依赖的jar包
进入conf文件夹配置文件
在mycat根目录创建logs文件夹,会把日志自动记录到这里。
如果启动报错可以到此目录查看日志
我在安装的时候发现一个错误
本地主机名未知,我的linux主机名称为test1,所以报错,需要在hosts文件中加上我的主机名字
编辑hosts文件:vim /etc/hosts
把我的主机名test1加上即可.
通过命令:ps -ef | grep mycat 查看服务是否已经确认启动成功
确认成功之后开始配置conf文件夹下的schema.xml、server.xml、rule.xml
配置schema.xml
在后面的测试环节有对此配置的说明内容,很详细。
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/"><!--name:为mycat逻辑库的名字,对应server<property name="schemas">mydatabase</property>,建议设置跟数据库一样的名称checkSQLschema:自动检查逻辑库名称并拼接,true会在sql语句中的表名前拼接逻辑库名,例如select * from mydatabase.t_user;sqlMaxLimit:查询保护、如果没有写limit条件,会自动拼接。只查询100条。--><schema name="mydatabase" checkSQLschema="true" sqlMaxLimit="100"><!--name:为物理数据库的表名,命名与物理数据库的一致 dataNode:为dataNode标签(<dataNode name="dn1" dataHost="dtHost1" database="db1" />)里面的name值dataNode里面填写的节点数量必须和rule里面的规则数量一致例如rule里面只定义了两个0-1M=0 1M-2M=1那么此处只可以指定两个节点,1M=10000,M为单位万primaryKey:为表的ID字段,建议和rule.xml里面指定的ID和物理库的ID一致rule:分片规则,对应rule.xml中<tableRule name="student_id">的nametype:表格类型,默认非global,用于全局表定义--><table name="t_user" dataNode="dn1,dn2,dn3" primaryKey="id" rule="auto-sharding-long"><!--ER分片注意childTable 标签需要放到table标签内,是主外键关联关系,name:为物理数据库的表名,命名与物理数据库的一致 primaryKey:为表t_loginlog的ID字段,建议和rule.xml里面指定的ID和物理库的ID一致.joinKey:从表t_loginlog的外键字段,需要和物理库的字段名称一致parentKey:为主表t_user的字段名,依据此字段做关联,进行ER分片--> <childTable name="t_loginlog" primaryKey="id" joinKey="user_id" parentKey="id"></childTable></table><table name="t_student" dataNode="dn1,dn3" primaryKey="id" rule="student_id" /><table name="t_dictionaries" dataNode="dn1,dn2,dn3" type="global" /><table name="t_teacher" dataNode="dn1" /></schema><!-- name:节点名称,用于在table标签里面调用dataHost:dataHost标签name值(<dataHost name="dtHost1">)database:物理数据库名,需要提前创建好实际存在的--><dataNode name="dn1" dataHost="dtHost1" database="db1" /><dataNode name="dn2" dataHost="dtHost1" database="db2" /><dataNode name="dn3" dataHost="dtHost2" database="db3" /><!--name:节点名称,在上方dataNode标签中调用maxCon:底层数据库的链接最大数minCon:底层数据库的链接最小数balance:值可以为0,1,2,3,分别表示对当前datahost中维护的数据库们的读操作逻辑0:不开启读写分离,所有的读写操作都在最小的索引号的writeHost(第一个writeHost标签)1:全部的readHost和备用writeHost都参与读数据的平衡,如果读的请求过多,负责写的第一个writeHost也分担一部分2 :所有的读操作,都随机的在所有的writeHost和readHost中进行3 :所有的读操作,都到writeHost对应的readHost上进行(备用writeHost不参加了),在集群中没有配置ReadHost的情况下,读都到第一个writeHost完成writeType:控制当前datahost维护的数据库集群的写操作0:所有的写操作都在第一个writeHost标签的数据库进行1:所有的写操作,都随机分配到所有的writeHost(mycat1.5完全不建议配置了)dbtype:数据库类型(不同数据库配置不同名称,mysql)dbDriver:数据库驱动,native,动态获取switchType:切换的逻辑-1:故障不切换1:故障切换,当前写操作的writeHost故障,进行切换,切换到下一个writeHost;slaveThreshold:标签中的<heartbeat>用来检测后端数据库的心跳sql语句;本属性检查从节点与主节点的同步情况(延迟时间数),配合心跳语句show slave status; 读写分离时,所有的readHost的数据都可靠--><dataHost name="dtHost1" maxCon="1000" minCon="10" balance="1"writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"><!--用于验证心跳,这个是mysql主库的配置--><heartbeat>select user()</heartbeat><writeHost host="127.0.0.1" url="192.168.199.11:3306" user="root" password="123456"><readHost host="127.0.0.1" url="192.168.199.12:3306" user="root" password="123456" /></writeHost></dataHost><dataHost name="dtHost2" maxCon="1000" minCon="10" balance="1"writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"><!--用于验证心跳,这个是mysql主库的配置--><heartbeat>select user()</heartbeat><writeHost host="127.0.0.1" url="192.168.199.13:3306" user="root" password="123456"><readHost host="127.0.0.1" url="192.168.199.13:3306" user="root" password="123456" /></writeHost></dataHost>
</mycat:schema>
配置server.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -->
<!DOCTYPE mycat:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://io.mycat/"><system><!--配置的端口号--><property name="serverPort">8066</property><property name="nonePasswordLogin">0</property> <!-- 0为需要密码登陆、1为不需要密码登陆 ,默认为0,设置为1则需要指定默认账户--><property name="useHandshakeV10">1</property><property name="useSqlStat">0</property> <!-- 1为开启实时统计、0为关闭 --><property name="useGlobleTableCheck">0</property> <!-- 1为开启全加班一致性检测、0为关闭 --><property name="sequnceHandlerType">2</property><property name="subqueryRelationshipCheck">false</property> <!-- 子查询中存在关联查询的情况下,检查关联字段中是否有分片字段 .默认 false --><!-- <property name="useCompression">1</property>--> <!--1为开启mysql压缩协议--><!-- <property name="fakeMySQLVersion">5.6.20</property>--> <!--设置模拟的MySQL版本号--><!-- <property name="processorBufferChunk">40960</property> --><!-- <property name="processors">1</property> <property name="processorExecutor">32</property> --><!--默认为type 0: DirectByteBufferPool | type 1 ByteBufferArena | type 2 NettyBufferPool --><property name="processorBufferPoolType">0</property><!--默认是65535 64K 用于sql解析时最大文本长度 --><!--<property name="maxStringLiteralLength">65535</property>--><!--<property name="sequnceHandlerType">0</property>--><!--<property name="backSocketNoDelay">1</property>--><!--<property name="frontSocketNoDelay">1</property>--><!--<property name="processorExecutor">16</property>--><!--<property name="serverPort">8066</property> <property name="managerPort">9066</property> <property name="idleTimeout">300000</property> <property name="bindIp">0.0.0.0</property> <property name="frontWriteQueueSize">4096</property> <property name="processors">32</property> --><!--分布式事务开关,0为不过滤分布式事务,1为过滤分布式事务(如果分布式事务内只涉及全局表,则不过滤),2为不过滤分布式事务,但是记录分布式事务日志--><property name="handleDistributedTransactions">0</property><!--off heap for merge/order/group/limit 1开启 0关闭--><property name="useOffHeapForMerge">1</property><!--单位为m--><property name="memoryPageSize">64k</property><!--单位为k--><property name="spillsFileBufferSize">1k</property><property name="useStreamOutput">0</property><!--单位为m--><property name="systemReserveMemorySize">384m</property><!--是否采用zookeeper协调切换 --><property name="useZKSwitch">false</property><!-- XA Recovery Log日志路径 --><!--<property name="XARecoveryLogBaseDir">./</property>--><!-- XA Recovery Log日志名称 --><!--<property name="XARecoveryLogBaseName">tmlog</property>--><!--如果为 true的话 严格遵守隔离级别,不会在仅仅只有select语句的时候在事务中切换连接--><property name="strictTxIsolation">false</property><property name="useZKSwitch">true</property></system><!-- 全局SQL防火墙设置 --><!--白名单可以使用通配符%或着*--><!--例如<host host="127.0.0.*" user="root"/>--><!--例如<host host="127.0.*" user="root"/>--><!--例如<host host="127.*" user="root"/>--><!--例如<host host="1*7.*" user="root"/>--><!--这些配置情况下对于127.0.0.1都能以root账户登录--><!-- 此处是开放了本机和外网的请求链接,因为我的mycat安装在虚拟机上,所以本机是通过ip链接192.*是为了让navicat可以连接使用(配置完整的虚拟机IP也可以)127.*是为了让虚拟机本身可以通过命令连接--><firewall><whitehost><host host="192.*" user="root"/><host host="127.*" user="root"/></whitehost><blacklist check="false"></blacklist></firewall><!-- 此处定义了一个root用户,可以管理的逻辑库为mydatabase,对应schema.xml中的<schema name="mydatabase" > --><user name="root" defaultAccount="true"><property name="password">123456</property><property name="schemas">mydatabase</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">123456</property><property name="schemas">mycat1,test3</property><property name="readOnly">true</property></user>--></mycat:server>
配置rule.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -->
<!DOCTYPE mycat:rule SYSTEM "rule.dtd">
<mycat:rule xmlns:mycat="http://io.mycat/">
<!--schema.xml中配置的rule="student_id" 所对应的规则,规则对应主键,
列名为id需要与数据库的列名对应algorithm对应下方function--><tableRule name="student_id"><rule><columns>id</columns><algorithm>student_text</algorithm></rule></tableRule><tableRule name="rule1"><rule><columns>id</columns><algorithm>func1</algorithm></rule></tableRule><tableRule name="rule2"><rule><columns>user_id</columns><algorithm>func1</algorithm></rule></tableRule><tableRule name="sharding-by-intfile"><rule><columns>sharding_id</columns><algorithm>hash-int</algorithm></rule></tableRule><tableRule name="auto-sharding-long"><rule><columns>id</columns><algorithm>rang-long</algorithm></rule></tableRule><tableRule name="mod-long"><rule><columns>id</columns><algorithm>mod-long</algorithm></rule></tableRule><tableRule name="sharding-by-murmur"><rule><columns>id</columns><algorithm>murmur</algorithm></rule></tableRule><tableRule name="crc32slot"><rule><columns>id</columns><algorithm>crc32slot</algorithm></rule></tableRule><tableRule name="sharding-by-month"><rule><columns>create_time</columns><algorithm>partbymonth</algorithm></rule></tableRule><tableRule name="latest-month-calldate"><rule><columns>calldate</columns><algorithm>latestMonth</algorithm></rule></tableRule><tableRule name="auto-sharding-rang-mod"><rule><columns>id</columns><algorithm>rang-mod</algorithm></rule></tableRule><tableRule name="jch"><rule><columns>id</columns><algorithm>jump-consistent-hash</algorithm></rule></tableRule><!--在conf中需要添加student_text.txt规则文件--><function name="student_text"class="io.mycat.route.function.AutoPartitionByLong"><property name="mapFile">student_text.txt</property></function><function name="murmur"class="io.mycat.route.function.PartitionByMurmurHash"><property name="seed">0</property><!-- 默认是0 --><property name="count">2</property><!-- 要分片的数据库节点数量,必须指定,否则没法分片 --><property name="virtualBucketTimes">160</property><!-- 一个实际的数据库节点被映射为这么多虚拟节点,默认是160倍,也就是虚拟节点数是物理节点数的160倍 --><!-- <property name="weightMapFile">weightMapFile</property> 节点的权重,没有指定权重的节点默认是1。以properties文件的格式填写,以从0开始到count-1的整数值也就是节点索引为key,以节点权重值为值。所有权重值必须是正整数,否则以1代替 --><!-- <property name="bucketMapPath">/etc/mycat/bucketMapPath</property> 用于测试时观察各物理节点与虚拟节点的分布情况,如果指定了这个属性,会把虚拟节点的murmur hash值与物理节点的映射按行输出到这个文件,没有默认值,如果不指定,就不会输出任何东西 --></function><function name="crc32slot"class="io.mycat.route.function.PartitionByCRC32PreSlot"></function><function name="hash-int"class="io.mycat.route.function.PartitionByFileMap"><property name="mapFile">partition-hash-int.txt</property></function><function name="rang-long"class="io.mycat.route.function.AutoPartitionByLong"><property name="mapFile">autopartition-long.txt</property></function><function name="mod-long" class="io.mycat.route.function.PartitionByMod"><!-- how many data nodes --><property name="count">3</property></function><function name="func1" class="io.mycat.route.function.PartitionByLong"><property name="partitionCount">8</property><property name="partitionLength">128</property></function><function name="latestMonth"class="io.mycat.route.function.LatestMonthPartion"><property name="splitOneDay">24</property></function><function name="partbymonth"class="io.mycat.route.function.PartitionByMonth"><property name="dateFormat">yyyy-MM-dd</property><property name="sBeginDate">2015-01-01</property></function><function name="rang-mod" class="io.mycat.route.function.PartitionByRangeMod"><property name="mapFile">partition-range-mod.txt</property></function><function name="jump-consistent-hash" class="io.mycat.route.function.PartitionByJumpConsistentHash"><property name="totalBuckets">3</property></function>
</mycat:rule>
在conf文件夹创建student_text.txt文件
vim student_text.txt
内容如下,需要注意的是
0-1M ,为0<=id<=10000,
1M-2M,为10000<id<=20000,因为只定义了两个范围。如果超过20000例如20001会报错
说明一下:1k=1000 , 1M=10000
0-1M=0
1M-2M=1
schema.xml中t_student的 table标签值对应了两个dataNode节点,所以此处只有两个规则,不能多,也不能少
<table name="t_student" dataNode="dn1,dn3" primaryKey="id" rule="student_id" />
配置完成后就可以启动mycat进行测试了
在bin文件夹下执行./mycat start
然后通过navicat进行连接,注意端口为之前配置的8066,用户名和密码也是之前配置的root/123456,连接地址为mycat安装主机地址
192.168.199.11和192.168.199.12为双机热备的物理库,
192.168.199.13为单机库,
mycat为逻辑库,通过逻辑库做增删改查、建表、删除表等表操作都会同步在其他物理库上执行。
手动在物理库上创建好已经定义的库
然后在mycat中执行sql创建表,并自动同步到物理库中,注意是mycat执行建表sql
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (`id` int(11) NOT NULL,`name` varchar(255) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;DROP TABLE IF EXISTS `t_student`;CREATE TABLE `t_student` (`id` int(11) NOT NULL,`s_name` varchar(255) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;DROP TABLE IF EXISTS `t_teacher`;
CREATE TABLE `t_teacher` (`id` int(11) NOT NULL,`t_name` varchar(255) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;DROP TABLE IF EXISTS `t_loginlog`;CREATE TABLE `t_loginlog` (`id` int(11) NOT NULL,`user_id` int(11) NOT NULL,`create_time` date DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;DROP TABLE IF EXISTS `t_dictionaries`;CREATE TABLE `t_dictionaries` (`id` int(11) NOT NULL,`name` varchar(255) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
在mycat中执行插入数据sql
INSERT INTO `t_student` VALUES ('0', '00');
INSERT INTO `t_student` VALUES ('1', '111');
INSERT INTO `t_student` VALUES ('10000', '10000');
INSERT INTO `t_student` VALUES ('10001', '10001');
INSERT INTO `t_student` VALUES ('20000', '20000');INSERT INTO `t_user` VALUES ('1', '111');
INSERT INTO `t_user` VALUES ('10000001', '10000001');
INSERT INTO `t_user` VALUES ('5000001', '5000001');INSERT INTO `t_loginlog` VALUES ('1', '1', '2019-03-08');
INSERT INTO `t_loginlog` VALUES ('3', '10000001', '2019-03-06');
INSERT INTO `t_loginlog` VALUES ('2', '5000001', '2019-03-07');INSERT INTO `t_dictionaries` VALUES ('1', '1');
打开mycat查看刚刚插入的数据,
首先确认t_student的数据,分片节点为dn1和dn3对应db1和db3的物理库,并分别存储在dtHost1和dtHost2两台mysql上
分片规则为
0-1M=0
1M-2M=1
确认物理库确实根据ID分开进行存储
确认t_user是否进行了分片,t_loginlog是否根据和t_user的主外键规则进行分片
确认t_user确实按照ID进行了分片,t_loginlog确实根据t_user的外键关联进行了分片,因为我的SQL并没有写主外键的约束,所以我手动往t_loginlog也是可以插入数据的
t_dictionaries表为全局表,所以他的数据操作是同步在所有的数据节点上,
配置了3个数据节点,所以3个数据节点中的数据是一致的
此处就不截图了。
t_teacher 并没有进行分片配置,也只定义了一个节点所以是单表,确认只有一个物理库中存在即可。
至此已经搭建完毕
rule规则详解
常用的分片规则:总共十个(基本够用)一、枚举法
<tableRule name="sharding-by-intfile"><rule><columns>user_id</columns><algorithm>hash-int</algorithm></rule></tableRule>
<function name="hash-int" class="io.mycat.route.function.PartitionByFileMap"><property name="mapFile">partition-hash-int.txt</property><property name="type">0</property><property name="defaultNode">0</property></function>partition-hash-int.txt 配置:
10000=0
10010=1
上面columns 标识将要分片的表字段,algorithm 分片函数,
其中分片函数配置中,mapFile标识配置文件名称,type默认值为0,0表示Integer,非零表示String,
所有的节点配置都是从0开始,及0代表节点1
/**
* defaultNode 默认节点:小于0表示不设置默认节点,大于等于0表示设置默认节点,结点为指定的值
*
默认节点的作用:枚举分片时,如果碰到不识别的枚举值,就让它路由到默认节点
* 如果不配置默认节点(defaultNode值小于0表示不配置默认节点),碰到
* 不识别的枚举值就会报错,
* like this:can't find datanode for sharding column:column_name val:ffffffff
*/二、固定分片hash算法
<tableRule name="rule1"><rule><columns>user_id</columns><algorithm>func1</algorithm></rule>
</tableRule><function name="func1" class="io.mycat.route.function.PartitionByLong"><property name="partitionCount">2,1</property><property name="partitionLength">256,512</property></function>
配置说明:
上面columns 标识将要分片的表字段,algorithm 分片函数,
partitionCount 分片个数列表,partitionLength 分片范围列表
分区长度:默认为最大2^n=1024 ,即最大支持1024分区
约束 :
count,length两个数组的长度必须是一致的。
1024 = sum((count[i]*length[i])). count和length两个向量的点积恒等于1024
用法例子:
@Test
public void testPartition() {
// 本例的分区策略:希望将数据水平分成3份,前两份各占25%,第三份占50%。(故本例非均匀分区)
// |<---------------------1024------------------------>|
// |<----256--->|<----256--->|<----------512---------->|
// | partition0 | partition1 | partition2 |
// | 共2份,故count[0]=2 | 共1份,故count[1]=1 |
int[] count = new int[] { 2, 1 };
int[] length = new int[] { 256, 512 };
PartitionUtil pu = new PartitionUtil(count, length);// 下面代码演示分别以offerId字段或memberId字段根据上述分区策略拆分的分配结果
int DEFAULT_STR_HEAD_LEN = 8; // cobar默认会配置为此值
long offerId = 12345;
String memberId = "qiushuo";// 若根据offerId分配,partNo1将等于0,即按照上述分区策略,offerId为12345时将会被分配到partition0中
int partNo1 = pu.partition(offerId);// 若根据memberId分配,partNo2将等于2,即按照上述分区策略,memberId为qiushuo时将会被分到partition2中
int partNo2 = pu.partition(memberId, 0, DEFAULT_STR_HEAD_LEN);Assert.assertEquals(0, partNo1);
Assert.assertEquals(2, partNo2);
}如果需要平均分配设置:平均分为4分片,partitionCount*partitionLength=1024
<function name="func1" class="org.opencloudb.route.function.PartitionByLong"><property name="partitionCount">4</property><property name="partitionLength">256</property></function>三、范围约定
<tableRule name="auto-sharding-long"><rule><columns>user_id</columns><algorithm>rang-long</algorithm></rule></tableRule>
<function name="rang-long" class="io.mycat.route.function.AutoPartitionByLong"><property name="mapFile">autopartition-long.txt</property></function>
# range start-end ,data node index
# K=1000,M=10000.
0-500M=0
500M-1000M=1
1000M-1500M=2
或
0-10000000=0
10000001-20000000=1配置说明:
上面columns 标识将要分片的表字段,algorithm 分片函数,
rang-long 函数中mapFile代表配置文件路径
所有的节点配置都是从0开始,及0代表节点1,此配置非常简单,即预先制定可能的id范围到某个分片四、求模法
<tableRule name="mod-long"><rule><columns>user_id</columns><algorithm>mod-long</algorithm></rule></tableRule><function name="mod-long" class="io.mycat.route.function.PartitionByMod"><!-- how many data nodes --><property name="count">3</property></function>配置说明:
上面columns 标识将要分片的表字段,algorithm 分片函数,
此种配置非常明确即根据id与count(你的结点数)进行求模预算,相比方式1,此种在批量插入时需要切换数据源,id不连续五、日期列分区法
<tableRule name="sharding-by-date"><rule><columns>create_time</columns><algorithm>sharding-by-date</algorithm></rule></tableRule>
<function name="sharding-by-date" class="io.mycat.route.function..PartitionByDate"><property name="dateFormat">yyyy-MM-dd</property><property name="sBeginDate">2014-01-01</property><property name="sPartionDay">10</property></function>
配置说明:
上面columns 标识将要分片的表字段,algorithm 分片函数,
配置中配置了开始日期,分区天数,即默认从开始日期算起,分隔10天一个分区还有一切特性请看源码Assert.assertEquals(true, 0 == partition.calculate("2014-01-01"));
Assert.assertEquals(true, 0 == partition.calculate("2014-01-10"));
Assert.assertEquals(true, 1 == partition.calculate("2014-01-11"));
Assert.assertEquals(true, 12 == partition.calculate("2014-05-01"));六、通配取模
<tableRule name="sharding-by-pattern"><rule><columns>user_id</columns><algorithm>sharding-by-pattern</algorithm></rule></tableRule>
<function name="sharding-by-pattern" class="io.mycat.route.function.PartitionByPattern"><property name="patternValue">256</property><property name="defaultNode">2</property><property name="mapFile">partition-pattern.txt</property></function>
partition-pattern.txt
# id partition range start-end ,data node index
###### first host configuration
1-32=0
33-64=1
65-96=2
97-128=3
######## second host configuration
129-160=4
161-192=5
193-224=6
225-256=7
0-0=7
配置说明:
上面columns 标识将要分片的表字段,algorithm 分片函数,patternValue 即求模基数,defaoultNode 默认节点,如果不配置了默认,则默认是0即第一个结点
mapFile 配置文件路径
配置文件中,1-32 即代表id%256后分布的范围,如果在1-32则在分区1,其他类推,如果id非数字数据,则会分配在defaoultNode 默认节点String idVal = "0";
Assert.assertEquals(true, 7 == autoPartition.calculate(idVal));
idVal = "45a";
Assert.assertEquals(true, 2 == autoPartition.calculate(idVal));七、ASCII码求模通配
<tableRule name="sharding-by-prefixpattern"><rule><columns>user_id</columns><algorithm>sharding-by-prefixpattern</algorithm></rule></tableRule>
<function name="sharding-by-pattern" class="io.mycat.route.function.PartitionByPrefixPattern"><property name="patternValue">256</property><property name="prefixLength">5</property><property name="mapFile">partition-pattern.txt</property></function>partition-pattern.txt# range start-end ,data node index
# ASCII
# 48-57=0-9
# 64、65-90=@、A-Z
# 97-122=a-z
###### first host configuration
1-4=0
5-8=1
9-12=2
13-16=3
###### second host configuration
17-20=4
21-24=5
25-28=6
29-32=7
0-0=7
配置说明:
上面columns 标识将要分片的表字段,algorithm 分片函数,patternValue 即求模基数,prefixLength ASCII 截取的位数
mapFile 配置文件路径
配置文件中,1-32 即代表id%256后分布的范围,如果在1-32则在分区1,其他类推 此种方式类似方式6只不过采取的是将列种获取前prefixLength位列所有ASCII码的和进行求模sum%patternValue ,获取的值,在通配范围内的
即 分片数,
/**
* ASCII编码:
* 48-57=0-9阿拉伯数字
* 64、65-90=@、A-Z
* 97-122=a-z
*
*/
如 String idVal="gf89f9a";
Assert.assertEquals(true, 0==autoPartition.calculate(idVal));idVal="8df99a";
Assert.assertEquals(true, 4==autoPartition.calculate(idVal));idVal="8dhdf99a";
Assert.assertEquals(true, 3==autoPartition.calculate(idVal));八、编程指定
<tableRule name="sharding-by-substring"><rule><columns>user_id</columns><algorithm>sharding-by-substring</algorithm></rule></tableRule>
<function name="sharding-by-substring" class="io.mycat.route.function.PartitionDirectBySubString"><property name="startIndex">0</property> <!-- zero-based --><property name="size">2</property><property name="partitionCount">8</property><property name="defaultPartition">0</property></function>
配置说明:
上面columns 标识将要分片的表字段,algorithm 分片函数
此方法为直接根据字符子串(必须是数字)计算分区号(由应用传递参数,显式指定分区号)。
例如id=05-100000002
在此配置中代表根据id中从startIndex=0,开始,截取siz=2位数字即05,05就是获取的分区,如果没传默认分配到defaultPartition九、字符串拆分hash解析
<tableRule name="sharding-by-stringhash"><rule><columns>user_id</columns><algorithm>sharding-by-stringhash</algorithm></rule></tableRule>
<function name="sharding-by-substring" class="io.mycat.route.function.PartitionByString"><property name=length>512</property> <!-- zero-based --><property name="count">2</property><property name="hashSlice">0:2</property></function>
配置说明:
上面columns 标识将要分片的表字段,algorithm 分片函数
函数中length代表字符串hash求模基数,count分区数,hashSlice hash预算位
即根据子字符串 hash运算hashSlice : 0 means str.length(), -1 means str.length()-1/*** "2" -> (0,2)<br/>* "1:2" -> (1,2)<br/>* "1:" -> (1,0)<br/>* "-1:" -> (-1,0)<br/>* ":-1" -> (0,-1)<br/>* ":" -> (0,0)<br/>*/
public class PartitionByStringTest {@Test
public void test() {
PartitionByString rule = new PartitionByString();
String idVal=null;
rule.setPartitionLength("512");
rule.setPartitionCount("2");
rule.init();
rule.setHashSlice("0:2");
// idVal = "0";
// Assert.assertEquals(true, 0 == rule.calculate(idVal));
// idVal = "45a";
// Assert.assertEquals(true, 1 == rule.calculate(idVal));//last 4
rule = new PartitionByString();
rule.setPartitionLength("512");
rule.setPartitionCount("2");
rule.init();
//last 4 characters
rule.setHashSlice("-4:0");
idVal = "aaaabbb0000";
Assert.assertEquals(true, 0 == rule.calculate(idVal));
idVal = "aaaabbb2359";
Assert.assertEquals(true, 0 == rule.calculate(idVal));
}十、一致性hash
<tableRule name="sharding-by-murmur"><rule><columns>user_id</columns><algorithm>murmur</algorithm></rule></tableRule>
<function name="murmur" class="io.mycat.route.function.PartitionByMurmurHash"><property name="seed">0</property><!-- 默认是0--><property name="count">2</property><!-- 要分片的数据库节点数量,必须指定,否则没法分片—><property name="virtualBucketTimes">160</property><!-- 一个实际的数据库节点被映射为这么多虚拟节点,默认是160倍,也就是虚拟节点数是物理节点数的160倍--><!--<property name="weightMapFile">weightMapFile</property>节点的权重,没有指定权重的节点默认是1。以properties文件的格式填写,以从0开始到count-1的整数值也就是节点索引为key,以节点权重值为值。所有权重值必须是正整数,否则以1代替 --><!--<property name="bucketMapPath">/etc/mycat/bucketMapPath</property>用于测试时观察各物理节点与虚拟节点的分布情况,如果指定了这个属性,会把虚拟节点的murmur hash值与物理节点的映射按行输出到这个文件,没有默认值,如果不指定,就不会输出任何东西 --></function>
Mycat安装与配置详解相关推荐
- Nagios远程监控软件的安装与配置详解
Nagios远程监控软件的安装与配置详解 作者:redhat_hu Nagios是一款功能强大的网络监视工具,它可以有效的监控windows.linux.unix主机状态以及路由器交换机的网络设置,打 ...
- Julia的安装与配置详解(包含在Ubuntu 18.04和Windows 10系统上Julia的安装)
Julia的安装与配置详解((包含在Ubuntu 18.04和Windows 10系统上Julia的安装) Julia的安装 使用二进制文件安装Julia 在Ubuntu上安装Julia 在Windo ...
- gulp的安装和配置详解
gulp的安装和配置详解 1.安装node.js 先下载一个node.js安装包. 下载完成后双击即可. 打开cmd,命令行输入node -v,回车输出nodejs版本号,表示安装成功. 命令行输入n ...
- node.js卸载、安装、配置详解
node.js卸载.安装.配置详解 一. node.js卸载 二.下载安装 2.1 下载 2.2 安装 2.2.1 选择msi安装 2.2.2 选择zip安装 三.配置 3.1 环境变量配置 3.2 ...
- Gradle安装和配置详解
Gradle安装和配置详解 gradle是基于Apache Ant和Apache Maven概念的项目自动化构建开源工具,也提供了很多第三方插件.在Java Web项目中通常会用到 java.war. ...
- keepalived介绍、安装及配置详解
文章目录 keepalived简介 keeplived和LVS关系 VRRP工作原理 keepalived体系结构及工作原理 keepalived安装 keepalived配置详解 keepalive ...
- Linux中Nginx安装与配置详解及常见问题
3 Nginx安装 3.1 安装前的准备 1)准备 pcre-8.12.tar.gz.该文件为正则表达式库.让nginx支持rewrite需要安装这个库. 2) 准备 nginx-1. ...
- Tomcat安装及配置详解
转载:http://www.ttlsa.com/tomcat/tomcat-install-and-configure/ 一,Tomcat简介 Tomcat 服务器是一个免费的开放源代码的Web 应用 ...
- 01 Redis安装、配置详解、数据备份与恢复
缓存简介 简介 缓存是为了调节速度不一致的两个或者多个不同的物质的速度,在中间对速度访问较快的一方起到一个加速访问速度较慢的一方的作用,比如cpu的一级,二级缓存是为了保存cpu最近经常访问的数据,内 ...
- uWSGI的安装及配置详解
uWSGI是一个Python Web服务器,它实现了WSGI协议.uwsgi.http等协议,常在部署Django或Flask开发的Python Web项目时使用,作为连接Nginx与应用程序之间的桥 ...
最新文章
- wpf ui框架_RapidCAX 开源UI框架
- [深度学习] 自然语言处理---Transformer实现(二)
- 腾讯云短信服务使用记录与.NET Core C#代码分享
- 提高Python运行效率的六个窍门
- java编码规范文档 下载_软件项目实训及课程设计指导——制定待开发项目中各种文档的规范...
- 自己手写一个Spring MVC框架
- 部分更新没有安装 KB4535102
- 教你如何鉴别iPhone翻新机!
- python实现字符串类型 str 转换为 list 类型(unicode 转换为 list)
- 制作电影影评网的html5代码,微信小程序之电影影评小程序制作代码
- uni-app - H5 公众号网页分享到微信朋友圈 / 转发分享给朋友好友 / 分享到手机 QQ / 分享到 QQ 空间,给微信分享卡片设置图标、标题、描述文字等(JS-SDK 通用解决方案放心用)
- API 攻击分析:黑客利用大量IP低频撞库登录API
- 马化腾徐少春等中国富豪获住房补贴 政策真空网民质疑
- 微信小程相对图片路径_微信小程序----相对路径图片不显示
- MaxKey单点登录认证系统v3.5.2GA发布
- 学Python基础面向对象看这一篇文章就够了【全网首推】
- 矿大计算机保研条件,祝贺!矿大1065位保研名单重磅发布!
- Linux libvirt目录,在Linux系统上使用Vagrant和Libvirt的方法
- 十次方前端系统开发(6)
- r730 虚拟磁盘不见了_戴尔服务器R730 无法分区安装解决办法