MYCAT使用篇(2)

使用mycat数据分片操作

安装mysql的客户端在当前的机子上,配置mysql对应的环境PATH连接mycat数据端端口默认是8066

 mysql -uroot -p123456 -h127.0.01 -P8066 -Dtest 

连接到mysql的客户端,看到mysql的连接终端。可以看到连接的终端和平时的mysql一样。创建需要测试的表。

CREATE TABLE `travelrecord` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `user_id` varchar(100) DEFAULT NULL,
  `traveldate` date DEFAULT NULL,
  `fee` decimal(10,0) DEFAULT NULL,
  `days` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
//执行后看到3个数据节点都已经创建了对应的数据表,可以看见我们已经在使用mycat中间件了
drop table `travelrecord`;
//执行drop后我们希望看到show tables时候不应该看见对应的数据表,但是还是会看见,实际应删了。

范围分片

上一篇我们配置表travelrecord的分库方式为auto-sharding-long,在rule中找到配置,看到对应的配置文件autopartition-long.txt

<tableRule name="auto-sharding-long">
<rule>
        <columns>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>

对应的autopartition-long.txt配置

//这里配置的M代表10000,K代表1000
 #range start-end ,data node index
 # K=1000,M=10000.
 0-500M=0
 500M-1000M=1
 1000M-10000M=2

执行测试的sql

//落入到db1.test
insert into travelrecord (id,user_id,traveldate,fee,days)values(100001,'janle1','2017-10-20',521,3); 
//落入到db2.test
insert into travelrecord (id,user_id,traveldate,fee,days)values(5000001,'janle2','2017-10-20',521,3); 
 //落入到db3.test
insert into travelrecord (id,user_id,traveldate,fee,days)values(50000001,'janle2','2017-10-20',521,3);
//查询对应的数据,会看到查询出db2.test的数据
explain select * from travelrecord where id='5000001';
//如果不用分库的字段查询,会看到查询时候查询全部的test表。所以建议查询时候都带着分库的字段
explain select * from travelrecord where user_id='janle1';

全局表分片

9066端口是管理端口。前面使用的8066是数据端口

//这里插个小曲,在schame.xml中添加以下全局配置后不需要重新启动mycat,
<table name="company" primaryKey="ID" type="global" dataNode="dn1,dn2,dn3" />
//在终端执行 mysql -uroot -p123456 -h127.0.0.1 -P9066 进入后执行命令reload @@config

执行测试的sql

CREATE TABLE `company` (
  `id` int(11) NOT NULL,
  `name` varchar(100) DEFAULT NULL,
  `sharding_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用于测试全局表';
//执行插入操作
INSERT INTO company (id,name,sharding_id) VALUES(1,'中国移动',1);
//查看会选择一个查询
explain select * from company;

取模分片

这个相对比较简单,就是将数据按照某个指定的数字id进行取模,分别修改schem.xml和rule.xml

//修改schem.xml
<table name="hotnews" primaryKey="ID" autoIncrement="true" dataNode="dn1,dn2,dn3"
               rule="mod-long" />
//配置rule.xml
<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
    <!-- how many data nodes 这里设置为3 -->
    <property name="count">3</property>
</function>
//刷新配置

执行测试的sql

CREATE TABLE `hotnews` (
  `id` int(11) NOT NULL ,
  title VARCHAR (400),
  created_time datetime,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用于测试取模分片';
//执行插入语句到db2中的表中,1%3=1
insert into hotnews(id,title,created_time) values(1,'hot1news',now());
insert into hotnews(id,title,created_time) values(2,'hot2news',now());
insert into hotnews(id,title,created_time) values(3,'hot3news',now());
//注意这里我们执行查询需要带着分片的字段,不然会将所有的库都查询一次。
explain select * from hotnews where id=1;

枚举分片

//修改schem.xml
 <table name="employee" primaryKey="ID" dataNode="dn1,dn2"
               rule="sharding-by-intfile" />

<table name="customer" primaryKey="ID" dataNode="dn1,dn2"
       rule="sharding-by-intfile">
    <childTable name="orders" primaryKey="ID" joinKey="customer_id"
                parentKey="id">
        <childTable name="order_items" joinKey="order_id"
                    parentKey="id" />
    </childTable>
    <childTable name="customer_addr" primaryKey="ID" joinKey="customer_id"
                parentKey="id" />
</table>
//配置rule.xml
<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
    <!-- how many data nodes 这里设置为3 -->
    <property name="count">3</property>
</function>
//刷新配置

执行测试的sql

//--简单测试1
CREATE TABLE employee (
    id INT NOT NULL PRIMARY KEY,
    NAME VARCHAR (100),
    sharding_id INT NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用于测试枚举分片';
//执行插入语句到db2中的表中,1%3=1
insert into employee(id,name,sharding_id) values(1,'leader',10000);
insert into employee(id,name,sharding_id) values(2,'me',10010);
insert into employee(id,name,sharding_id) values(3,'dev',10000);
insert into employee(id,name,sharding_id) values(4,'ui',10010);
//注意这里我们执行查询需要带着分片的字段,不然会将所有的库都查询一次。
explain select * from employee;

//--测试2
CREATE TABLE customer (
    id INT NOT NULL PRIMARY KEY,
    NAME VARCHAR (100),
    company_id INT NOT NULL,
    sharding_id INT NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用于测试取模分片';
CREATE TABLE orders (
    id INT NOT NULL PRIMARY KEY,
    customer_id INT NOT NULL,
    sharding_id INT NOT NULL,
    sataus INT,
    note VARCHAR (100)
) ENGINE = INNODB DEFAULT CHARSET = utf8 COMMENT = '用于测试取模分片';

//插入customer数据
insert into customer(id,name,company_id,sharding_id)values(1,'百度',1,10000);
insert into customer(id,name,company_id,sharding_id)values(2,'腾讯',2,10010);
insert into customer(id,name,company_id,sharding_id)values(3,'阿里',3,10000);
insert into customer(id,name,company_id,sharding_id)values(4,'京东',4,10000);
//插入orders数据
insert into orders(id,customer_id) values(1,3); //db1
insert into orders(id,customer_id) values(2,1); //db1
insert into orders(id,customer_id) values(3,2); //db2
//查询验证 explain select * from orders where customer_id='1';
//发现我们使用查询时候没有在本地查找虽然已经将数据分到当前的机子上。所以我觉得是否可以冗余一个sharding_id在customer的子表中。

猜你喜欢

转载自blog.csdn.net/u013642886/article/details/78297466