MySQL-分布式架构-MyCAT( 五 )


分片:对一个"bigtable",比如说t3表 (1)行数非常多,800w (2)访问非常频繁 分片的目的: (1)将大数据量进行分布存储 (2)提供均衡的访问路由 分片策略: 范围 range 800w 1-400w 400w01-800w 取模 mod 取余数 枚举 哈希 hash 时间 流水 优化关联查询 全局表 ER分片 1.9.1 范围分片
(1)范围分片参数配置
比如说t3表(1)行数非常多,2000w(1-1000w:sh1 1000w01-2000w:sh2)(2)访问非常频繁,用户访问较离散cp schema.xml schema.xml.1 ==========vim schema.xml<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="sh1"><table name="t3" dataNode="sh1,sh2" rule="auto-sharding-long" /></schema><dataNode name="sh1" dataHost="oldguo1" database= "taobao" /><dataNode name="sh2" dataHost="oldguo2" database= "taobao" />===========vim rule.xml<tableRule name="auto-sharding-long"> <rule> <columns>id</columns> <algorithm>rang-long</algorithm> </rule> <function name="rang-long" class="io.mycat.route.function.AutoPartitionByLong"> <property name="mapFile">autopartition-long.txt</property></function> ===========vim autopartition-long.txt0-10=011-20=1(2)创建测试表:
mysql -S /data/3307/mysql.sock -e "use taobao;create table t3 (id int not null primary key auto_increment,name varchar(20) not null);" mysql -S /data/3308/mysql.sock -e "use taobao;create table t3 (id int not null primary key auto_increment,name varchar(20) not null);" (3)重启mycat------>mycat restart
(4)登录mycat插入数据
mysql -uroot -p123456 -h 127.0.0.1 -P 8066 insert into t3(id,name) values(1,'a'); insert into t3(id,name) values(2,'b'); insert into t3(id,name) values(3,'c'); insert into t3(id,name) values(4,'d'); insert into t3(id,name) values(11,'aa'); insert into t3(id,name) values(12,'bb'); insert into t3(id,name) values(13,'cc'); insert into t3(id,name) values(14,'dd'); (5)分别登入后端节点测试
mysql -S /data/3307/mysql.sock -e "use taobao;select * from t5;"mysql -S /data/3308/mysql.sock -e "use taobao;select * from t5;"1.9.2 取模分片(mod-long)
取余分片方式:分片键(一个列)与节点数量进行取余,得到余数,将数据写入对应节点
(1)取模分片参数配置
=======vim schema.xml <table name="t4" dataNode="sh1,sh2" rule="mod-long" /> ========vim rule.xml <tableRule name="mod-long"> <rule> <columns>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">2</property> </function> (2)创建测试表:
mysql -S /data/3307/mysql.sock -e "use taobao;create table t4 (id int not null primary key auto_increment,name varchar(20) not null);" mysql -S /data/3308/mysql.sock -e "use taobao;create table t4 (id int not null primary key auto_increment,name varchar(20) not null);"(3)重启mycat
mycat restart (4)模拟数据测试:
mysql -uroot -p123456 -h10.0.0.52 -P8066 use TESTDB insert into t4(id,name) values(1,'a'); insert into t4(id,name) values(2,'b'); insert into t4(id,name) values(3,'c'); insert into t4(id,name) values(4,'d'); (5)分别登录后端节点查询数据
mysql -S /data/3307/mysql.sock -e "use taobao;select * from t4;"mysql -S /data/3308/mysql.sock -e "use taobao;select * from t4;"1.9.3 枚举分片
t5 表 id name telnum 1 bj 1212 2 sh 22222 3 bj 3333 4 sh 44444 5 bj 5555 (1)枚举分片参数配置
=====vim schema.xml<table name="t5" dataNode="sh1,sh2" rule="sharding-by-intfile" />=====vim rule.xml<tableRule name="sharding-by-intfile"><rule> <columns>name</columns><algorithm>hash-int</algorithm></rule> </tableRule> <function name="hash-int" class="org.opencloudb.route.function.PartitionByFileMap"> <property name="mapFile">partition-hash-int.txt</property><property name="type">1</property></function> ======partition-hash-int.txt 配置: bj=0 sh=1DEFAULT_NODE=1 columns 标识将要分片的表字段,algorithm 分片函数,其中分片函数配置中,mapFile标识配置文件名称(2)准备测试环境
mysql -S /data/3307/mysql.sock -e "use taobao;create table t5 (id int not null primary key auto_increment,name varchar(20) not null);"mysql -S /data/3308/mysql.sock -e "use taobao;create table t5 (id int not null primary key auto_increment,name varchar(20) not null);"


推荐阅读