shard1
10.0.0.51:3307<-----> 10.0.0.52:33071、db02
mysql -S /data/3307/mysql.sock -e "grant replication slave on *.* to repl@'10.0.0.%' identified by '123';"
mysql -S /data/3307/mysql.sock -e "grant all on *.* to root@'10.0.0.%' identified by '123' with grant option;"2、db01
mysql -S /data/3307/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='10.0.0.52', MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
mysql -S /data/3307/mysql.sock -e "start slave;"
mysql -S /data/3307/mysql.sock -e "show slave status\G"3、db02
mysql -S /data/3307/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='10.0.0.51', MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
mysql -S /data/3307/mysql.sock -e "start slave;"
mysql -S /data/3307/mysql.sock -e "show slave status\G"10.0.0.51:3309------> 10.0.0.51:33074、db01
mysql -S /data/3309/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='10.0.0.51', MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
mysql -S /data/3309/mysql.sock -e "start slave;"
mysql -S /data/3309/mysql.sock -e "show slave status\G"10.0.0.52:3309------> 10.0.0.52:33075、db02
mysql -S /data/3309/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='10.0.0.52', MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
mysql -S /data/3309/mysql.sock -e "start slave;"
mysql -S /data/3309/mysql.sock -e "show slave status\G"
shard2
10.0.0.52:3308<-----> 10.0.0.51:33086、db01
mysql -S /data/3308/mysql.sock -e "grant replication slave on *.* to repl@'10.0.0.%' identified by '123';"
mysql -S /data/3308/mysql.sock -e "grant all on *.* to root@'10.0.0.%' identified by '123' with grant option;"7、db02
mysql -S /data/3308/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='10.0.0.51', MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
mysql -S /data/3308/mysql.sock -e "start slave;"
mysql -S /data/3308/mysql.sock -e "show slave status\G"8、db01
mysql -S /data/3308/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='10.0.0.52', MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
mysql -S /data/3308/mysql.sock -e "start slave;"
mysql -S /data/3308/mysql.sock -e "show slave status\G"10.0.0.52:3310-----> 10.0.0.52:33089、db02
mysql -S /data/3310/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='10.0.0.52', MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
mysql -S /data/3310/mysql.sock -e "start slave;"
mysql -S /data/3310/mysql.sock -e "show slave status\G"10.0.0.51:3310-----> 10.0.0.51:330810、db01
mysql -S /data/3310/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='10.0.0.51', MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
mysql -S /data/3310/mysql.sock -e "start slave;"
mysql -S /data/3310/mysql.sock -e "show slave status\G"
5.1 用户创建及数据库导入
db01:
mysql -S /data/3307/mysql.sock
grantallon*.*to root@'10.0.0.%' identified by '123';
source /root/world.sql
mysql -S /data/3308/mysql.sock
grant all on *.* to root@'10.0.0.%' identified by '123';
source /root/world.sql5.2 配置文件处理
cd /usr/local/mycat/conf
mv schema.xml schema.xml.bak
vim schema.xml
<schema name="TESTDB" dataNode="sh1"></schema><dataNode name="sh1" dataHost="cheng1"database="world"/><dataHost name="cheng1"<writeHost host="db1" url="10.0.0.51:3307"user="root" password="123"><readHost host="db2" url="10.0.0.51:3309"user="root" password="123"/></writeHost></dataHost></mycat:schema>
PS:访问cheng1的时候,写的操作走51:3307 读的操作走51:3309
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
0-10=010-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 8066insertinto t3(id,name)values(1,'a');insertinto t3(id,name)values(2,'b');insertinto t3(id,name)values(3,'c');insertinto t3(id,name)values(4,'d');insertinto t3(id,name)values(11,'aa');insertinto t3(id,name)values(12,'bb');insertinto t3(id,name)values(13,'cc');insertinto t3(id,name)values(14,'dd');insertinto t3(id,name)values(10,'ddd');
分别登陆后端主节点查看数据
mysql -S /data/3307/mysql.sock -e "select * from taobao.t3"
mysql -S /data/3308/mysql.sock -e "select * from taobao.t3"
12.取模分片(mod-long):
1.取余分片方式:分片键(一个列)与节点数量进行取余,得到余数,将数据写入对应节点
vim schema.xml
<table name="t4" dataNode="sh1,sh2"rule="mod-long"/>
vim rule.xml
<property name="count">2</property>
准备测试环境
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
insertinto t4(id,name)values(1,'a');insertinto t4(id,name)values(2,'b');insertinto t4(id,name)values(3,'c');insertinto t4(id,name)values(4,'d');5.分别登录后端节点查询数据
mysql -S /data/3307/mysql.sock
use taobao
select*from t4;
mysql -S /data/3308/mysql.sock
use taobao
select*from t4;
13.枚举分片
t5 表
id name telnum
1 bj 12122 sh 222223 bj 33334 sh 444445 bj 5555
sharding-by-intfile
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><property name="defaultNode">0</property></function>partition-hash-int.txt 配置:
bj=0
sh=1
DEFAULT_NODE=1columns 标识将要分片的表字段,algorithm 分片函数, 其中分片函数配置中,mapFile标识配置文件名称
准备测试环境
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
mycat restart
mysql -uroot -p123456 -h10.0.0.51-P8066
use TESTDB
insertinto t5(id,name)values(1,'bj');insertinto t5(id,name)values(2,'sh');insertinto t5(id,name)values(3,'bj');insertinto t5(id,name)values(4,'sh');insertinto t5(id,name)values(5,'tj');
14.Mycat全局表
a b c d
join
t
select t1.name ,t.x from t1
join t
select t2.name ,t.x from t2
join t
select t3.name ,t.x from t3
join t
使用场景:
如果你的业务中有些数据类似于数据字典,比如配置文件的配置,
常用业务的配置或者数据量不大很少变动的表,这些表往往不是特别大,
而且大部分的业务场景都会用到,那么这种表适合于Mycat全局表,无须对数据进行切分,
要在所有的分片上保存一份数据即可,Mycat 在Join操作中,业务表与全局表进行Join聚合会优先选择相同分片内的全局表join,
避免跨库Join,在进行数据插入操作时,mycat将把数据分发到全局表对应的所有分片执行,在进行数据读取时候将会随机获取一个节点读取数据。
vim schema.xml
<table name="t_area" primaryKey="id"type="global" dataNode="sh1,sh2"/>
后端数据准备
mysql -S /data/3307/mysql.sock
use taobao
createtable t_area (id intnotnullprimarykeyauto_increment,name varchar(20)notnull);
mysql -S /data/3308/mysql.sock
use taobao
createtable t_area (id intnotnullprimarykeyauto_increment,name varchar(20)notnull);
重启mycat
mycat restart
测试:
mysql -uroot -p123456 -h10.0.0.52-P8066
use TESTDB
insertinto t_area(id,name)values(1,'a');insertinto t_area(id,name)values(2,'b');insertinto t_area(id,name)values(3,'c');insertinto t_area(id,name)values(4,'d');
15.E-R分片
A
join
B
为了防止跨分片join,可以使用E-R模式
A join B
on a.xx=b.yy
join C
on A.id=C.id
a
id name aid
1 a 112 b 223 c 334 d 445 e 55
b
id addr tel
11 bj 11022 sh 12033 tj 11944 cq 11255 sz 114
a
2 b 224 d 44
b
22 sh 12044 cq 112
a
1 a 113 c 335 e 55
b
11 bj 11033 tj 11955 sz 114
配置:
1.schema.xml
<table name="a" dataNode="sh1,sh2"rule="mod-long1"><childTable name="b" joinKey="id" parentKey="aid"/></table>2.rule.xml
<tableRule name="mod-long1"><rule><columns>id</columns><algorithm>mod-long1</algorithm></rule></tableRule><function name="mod-long1" class="io.mycat.route.function.PartitionByMod"><!-- how many data nodes --><property name="count">2</property></function>3. 准备数据
mysql -S /data/3307/mysql.sock -e "use taobao;create table a (id int not null primary key auto_increment,name varchar(20) not null,aid int not null);"
mysql -S /data/3308/mysql.sock -e "use taobao;create table a (id int not null primary key auto_increment,name varchar(20) not null,aid int not null);"
mysql -S /data/3307/mysql.sock -e "use taobao;create table b (id int not null ,addr varchar(20) not null,tel int not null);"
mysql -S /data/3308/mysql.sock -e "use taobao;create table b (id int not null ,addr varchar(20) not null,tel int not null);"4. 重启mycat 测试:
mycat restart
mysql -uroot -p123456 -h10.0.0.51-P8066
use TESTDB
insertinto a(id,name,aid)values(1,'a',11);insertinto a(id,name,aid)values(2,'b',22);insertinto a(id,name,aid)values(3,'c',33);insertinto a(id,name,aid)values(4,'d',44);insertinto a(id,name,aid)values(5,'e',55);insertinto b(id,addr,tel)values(11,'bj',110);insertinto b(id,addr,tel)values(22,'sh',220);insertinto b(id,addr,tel)values(33,'tj',330);insertinto b(id,addr,tel)values(44,'cq',440);insertinto b(id,addr,tel)values(55,'sz',550);5. 分别登录后端节点查询数据
mysql -S /data/3307/mysql.sock -e "select * from taobao.a;"
mysql -S /data/3307/mysql.sock -e "select * from taobao.b;"
mysql -S /data/3308/mysql.sock -e "select * from taobao.a;"
mysql -S /data/3308/mysql.sock -e "select * from taobao.b;"
16. 月份分片 (9T)
1. 修改配置文件
[root@db01~]# cat /usr/local/mycat/conf/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="t5" dataNode="sh1,sh2"rule="sharding-by-month"/></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>selectuser()</heartbeat><writeHost host="db1" url="10.0.0.51:3307"user="root" password="123"><readHost host="db2" url="10.0.0.51:3309"user="root" password="123"/></writeHost><writeHost host="db3" url="10.0.0.52:3307"user="root" password="123"><readHost host="db4" url="10.0.0.52: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>selectuser()</heartbeat><writeHost host="db1" url="10.0.0.51:3308"user="root" password="123"><readHost host="db2" url="10.0.0.51:3310"user="root" password="123"/></writeHost><writeHost host="db3" url="10.0.0.52:3308"user="root" password="123"><readHost host="db4" url="10.0.0.52:3310"user="root" password="123"/></writeHost></dataHost></mycat:schema>2.rule配置
[root@db01 conf]# cat rule.xml <?xml version="1.0" encoding="UTF-8"?><!-- - - Licensed under the Apache License, Version 2.0 (the "License"); - you may notuse this fileexceptin 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 toin writing, software -distributed under the License isdistributedon an "AS IS" BASIS,- WITHOUT
WARRANTIES OR CONDITIONS OFANY 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/"><tableRule name="rule1"><rule><columns>id</columns><algorithm>func1</algorithm></rule></tableRule><tableRule name="sharding-by-date"><rule><columns>createTime</columns><algorithm>partbyday</algorithm></rule></tableRule><tableRule name="rule2"><rule><columns>user_id</columns><algorithm>func1</algorithm></rule></tableRule><tableRule name="sharding-by-intfile"><rule><columns>name</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="mod-long1"><rule><columns>id</columns><algorithm>mod-long1</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>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><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"><property name="count">2</property><!-- 要分片的数据库节点数量,必须指定,否则没法分片 --></function><function name="hash-int"
class="io.mycat.route.function.PartitionByFileMap"><property name="mapFile">partition-hash-int.txt</property><property name="type">1</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">2</property></function><function name="mod-long1" class="io.mycat.route.function.PartitionByMod"><!-- how many data nodes --><property name="count">2</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="partbyday"
class="io.mycat.route.function.PartitionByDate"><property name="dateFormat">yyyy-MM-dd</property><property name="sNaturalDay">0</property><property name="sBeginDate">2014-01-01</property><property name="sEndDate">2014-01-31</property><property name="sPartionDay">10</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>3.准备测试环境
mysql -S /data/3307/mysql.sock -e "create database taobao ;use taobao;create table t5 (id int not null primary key auto_increment,name varchar(20) not null,time datetime);"
mysql -S /data/3308/mysql.sock -e "create database taobao ;use taobao;create table t5 (id int not null primary key auto_increment,name varchar(20) not null,time datetime);"4.重启mycat
mycat restart
mysql -uroot -p123456 -h10.0.0.51-P8066
use TESTDB
insertinto t5(id,name,time)values(1,'a','2015-01-03');insertinto t5(id,name,time)values(2,'b','2015-02-06');5. 分别登录后端节点查询数据
mysql -S /data/3307/mysql.sock -e "select * from taobao.t5;"
mysql -S /data/3308/mysql.sock -e "select * from taobao.t5;"