5. Mycat高级应用-分布式解决方案
5.1 垂直分表
mycat 服务器(192.168.189.128)上面操作:
cd /usr/local/mycat/conf
mv schema.xml schema.xml.ha
vim schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="sh1"> ##默认分片节点
<table name="user" dataNode="sh1"/> ## 访问user表,往 sh1分片
<table name="order_t" dataNode="sh2"/> ## 访问order_t 表 往 sh2 分片
## 没有指明的表,就走默认分片。
</schema>
<dataNode name="sh1" dataHost="oldguo1" database= "taobao" />
<dataNode name="sh2" dataHost="oldguo2" database= "taobao" />
<dataHost name="oldguo1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1">
<heartbeat>select user()</heartbeat>
<writeHost host="db1" url="192.168.189.99:3307" user="root" password="123">
<readHost host="db2" url="192.168.189.99:3309" user="root" password="123" />
</writeHost>
<writeHost host="db3" url="192.168.189.131:3307" user="root" password="123">
<readHost host="db4" url="192.168.189.131:3309" user="root" password="123" />
</writeHost>
</dataHost>
<dataHost name="oldguo2" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1">
<heartbeat>select user()</heartbeat>
<writeHost host="db1" url="192.168.189.99:3308" user="root" password="123">
<readHost host="db2" url="192.168.189.99:3310" user="root" password="123" />
</writeHost>
<writeHost host="db3" url="192.168.189.131:3308" user="root" password="123">
<readHost host="db4" url="192.168.189.131:3310" user="root" password="123" />
</writeHost>
</dataHost>
</mycat:schema>
复制的时候,最好把##注释删掉。
创建测试库和表: db01 192.168.99 上面操作就可以了。
[root@db01 conf]# mysql -S /data/3307/mysql.sock -e "create database taobao charset utf8;"
[root@db01 conf]# mysql -S /data/3308/mysql.sock -e "create database taobao charset utf8;"
[root@db01 conf]# mysql -S /data/3307/mysql.sock -e "use taobao;create table user(id int,name varchar(20))";
[root@db01 conf]# mysql -S /data/3308/mysql.sock -e "use taobao;create table order_t(id int,name varchar(20))"
重启mycat : (192.168.189.128 mycat 服务器)
mycat restart
测试功能: (192.168.189.128 mycat 服务器)
mysql -uroot -p123456 -h 127.0.0.1 -P 8066
mysql> use TESTDB
mysql> insert into user(id ,name ) values(1,'a'),(2,'b');
mysql> commit;
mysql> insert into order_t(id ,name ) values(1,'a'),(2,'b');
mysql> commit;
[root@db01 ~]# mysql -S /data/3307/mysql.sock -e "show tables from taobao;"
[root@db01 ~]# mysql -S /data/3308/mysql.sock -e "show tables from taobao;"
5.2 Mycat分布式-水平拆分(分片)介绍
分片:对一个"bigtable",比如说t3表
(1)行数非常多,800w
(2)访问非常频繁
分片的目的:
(1)将大数据量进行分布存储
(2)提供均衡的访问路由
分片策略:
范围 range 800w 1-400w 400w01-800w
取模 mod 取余数
枚举
哈希 hash
时间 流水
优化关联查询
全局表
ER分片
5.3 Mycat分布式-范围分片
比如说t3表 (192.168.189.128 mycat 服务器)
(1)行数非常多,2000w(1-1000w:sh1 1000w01-2000w:sh2)
(2)访问非常频繁,用户访问较离散
cd /usr/local/mycat/conf
cp schema.xml schema.xml.11
vim schema.xml ##(删除原来的两个垂直分片,添加红色部分,其他不用动。 auto-sharding-long 按范围分片)
<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.txt
删除原有内容,修改我们需要的范围。(本实例t3表只有20行数据,用来模拟。)
保存退出。
我们有两个数据节点,一个是sh1 ,一个是 sh2,0 ----->对应的是sh1 1 ------>对应的是 sh2
1-10=0 -----> >=1 , <=10
10-20=1 -----> >10 ,<=20
创建测试表:(db01 192.168.189.99上操作)
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 (192.168.189.128 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(10,'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');
insert into t3(id,name) values(20,'dd');
db01上进行查看数据:
mysql -S /data/3308/mysql.sock -e "select * from taobao.t3;"
mysql -S /data/3307/mysql.sock -e "select * from taobao.t3;"
insert into t3(id,name) values(21,'scl');
5.4 取模分片(mod-long):
取余分片方式:分片键(一个列)与节点数量进行取余,得到余数,将数据写入对应节点
cd /usr/local/mycat/conf
vim schema.xml
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="sh1">
<table name="t4" dataNode="sh1,sh2" rule="mod-long" />
</schema>
修改rule.xml
cd /usr/local/mycat/conf
vim rule.xml
准备测试环境
创建测试表:(db01 192.168.189.99上操作)
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 (192.168.189.128 mycat 服务器)
mycat restart
测试: (db01 192.168.189.99上操作)
mysql -uroot -p123456 -h192.168.189.99 -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');
分别登录后端节点查询数据:(db01 192.168.189.99上操作)
mysql -S /data/3307/mysql.sock -e "select * from taobao.t4;"
mysql -S /data/3308/mysql.sock -e "select * from taobao.t4;"
5.5、 枚举分片sharding-by-intfile
t5 表
id name telnum
1 bj 1212
2 sh 22222
3 bj 3333
4 sh 44444
5 bj 5555
cd /usr/local/mycat/conf
vim schema.xml
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="sh1">
<table name="t5" dataNode="sh1,sh2" rule="sharding-by-intfile" />
</schema>
修改rule.xml
cd /usr/local/mycat/conf
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>
保存,退出。
修改 hash-int 函数的参数文件 partition-hash-int.txt
cd /usr/local/mycat/conf
partition-hash-int.txt 配置:
bj=0
sh=1
DEFAULT_NODE=1
保存退出。
准备测试环境
db01 192.168.189.99 上操作
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);"
重启mycat (192.168.189.128 mycat服务器)
mycat restart
192.168.189.128 mycat服务器:
mysql -uroot -p123456 -h192.168.189.99 -P8066
use TESTDB
insert into t5(id,name) values(1,'bj');
insert into t5(id,name) values(2,'sh');
insert into t5(id,name) values(3,'bj');
insert into t5(id,name) values(4,'sh');
insert into t5(id,name) values(5,'tj');
分别登录后端节点(db01 192.168.189.99)查询数据
mysql -S /data/3307/mysql.sock -e "select * from taobao.t5;"
mysql -S /data/3308/mysql.sock -e "select * from taobao.t5;"
=====================================