mysql的分片怎么写 mysql 分区 分片 分库 分表( 八 )


maxCon="1000":最大的并发连接数
minCon="10" :mycat在启动之后,会在后端节点上自动开启的连接线程,长连接,好处是连接速度快,弊端是占内存
tempReadHostAvailable="1"
这个一主一从时(1个writehost,1个readhost时) , 可以开启这个参数,如果2个writehost,2个readhost时
heartbeatselect user()/heartbeat监测心跳
其他参数sqlMaxLimit自动分页,必须在启用分表的情况下才生效
创建测试库和表:
我们重启mycat后连接到8066
发现跟一个库一样,实际上已经分到不同的物理硬件上了
分片:对一个"bigtable",比如说t3表
热点数据表 核心表
(1)行数非常多,800w下坡
(2)访问非常频繁
分片的目的:
(1)将大数据量进行分布存储
(2)提供均衡的访问路由
分片策略:
范围 range800w1-400w 400w01-800w 不适用于业务访问不均匀的情况
取模 mod(取余数) 和节点的数量进行取模
枚举按枚举的种类分,如移动项目按省份分
哈希 hash
时间 流水
优化关联查询(否则join的表在不同分片上 , 效率会比单库还要低)
全局表
ER分片
案例:移动统一:先拆出边缘业务,再按地域分片,但对应用来说是统一的
vim rule.xml
tableRule name="auto-sharding-long"
rule
columnsid/columns
algorithmrang-long/algorithm
/rule
function name="rang-long"
class="io.mycat.route.function.AutoPartitionByLong"
property name="mapFile"autopartition-long.txt/property
/function
===================================
vim autopartition-long.txt
0-10=0
11-20=1
创建测试表:
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);"
测试:
重启mycat
mycat restart
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');
取余分片方式:分片键(一个列)与节点数量进行取余,得到余数,将数据写入对应节点
vim schema.xml
table name="t4" dataNode="sh1,sh2" rule="mod-long" /
vim rule.xml
property name="count"2/property
准备测试环境
创建测试表:
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);"
重启mycat
mycat restart
测试:
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');
分别登录后端节点查询数据
mysql -S /data/3307/mysql.sock
use taobao
select * from t4;
mysql -S /data/3308/mysql.sock
use taobao
select * from t4;
t5 表
id name telnum
1bj1212
2sh22222
3bj3333
4sh44444
5bj5555
sharding-by-intfile
vim schema.xml
table name="t5" dataNode="sh1,sh2" rule="sharding-by-intfile" /
vim rule.xml
tableRule name="sharding-by-intfile"

推荐阅读