ER分片介绍

   以mycat逻辑库里面自带的例子,例如客户(CUSTOMER)跟订单(orders)以及订单条目(orders_item),订单条目依
赖订单表,订单表依赖客户,这样客户与订单以及订单条目之间存在依赖关系,这类似业务的切分可以抽象出合适的切分
规则,比如根据用户ID切分,其它相关的表都依赖于用户ID,再或者根据订单ID进行切分,总之部分业务总会可以抽象出
父子关系的表。这类表适用于ER分片表,子表的记录与所关联的父表记录存放在同一个数据分片上,避免数据Join跨库操
作,以order与order_item例子为例和customer与order,schema.xml中定义合适的分片配置,order,order_item根
据id迕行数据切分,保证相同id的数据分到同一个分片上,在进行数据插入操作时,Mycat会获取order所在的分片,然
后将order_item也插入到order所在的分片。同理order与customer也是这样关系。
Tables 主键 对应的父表 关联关系
customer id
orders id customer orders.customer_id=customer.id
order_items id orders order_items.order_id=orders.id
customer_addr id customer customer_addr.customer_id= customer.id

1 修改 scehma.xml 添加 ER分片对应的父子表

#customer 采用rule是sharding-by-intfile,是枚举类型,类似range,可以看成是range特例情况.

[root@localhost conf]# vi schema.xml

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://org.opencloudb/"> <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
<table name="travelrecord" dataNode="dn1,dn2,dn3" rule="auto-sharding-long" />
<table name="T_VOTE" dataNode="dn1,dn2,dn3" rule="sharding-by-murmur" />
<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>
</schema>
<dataNode name="dn1" dataHost="192.168.2.130" database="db1" />
<dataNode name="dn2" dataHost="192.168.2.130" database="db2" />
<dataNode name="dn3" dataHost="192.168.2.130" database="db3" />
<dataHost name="192.168.2.130" maxCon="1000" minCon="10" balance="0"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="192.168.2.130:3306" user="root"
password="root123">
</writeHost>
</dataHost>
</mycat:schema>

2 修改 rule.xml

   #修改默认规则columns里面对应sharding_id变成id
<tableRule name="sharding-by-intfile">
<rule>
<columns>id</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>
</function> #修改func 对应的文本:定义2个值对应datanode
[root@localhost conf]# vi partition-hash-int.txt
10000=0
10010=1 #reload config
mysql> reload @@config;
Query OK, 1 row affected (0.24 sec)
Reload config success

3 在逻辑库中创建需要表

注释:创建customer和customer_addr表演示ER分片.
[root@localhost bin]# mysql -h 192.168.2.130 -P8066 -u test -ptest
Warning: Using a password on the command line interfa ce can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.5.8-mycat-1.5.1-RELEASE-20161130213509 MyCat Server (OpenCloundDB) Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
mysql>
mysql> show databases;
+----------+
| DATABASE |
+----------+
| TESTDB |
+----------+
1 row in set (0.00 sec) mysql> use TESTDB
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A Database changed
mysql> show tables;
+------------------+
| Tables in TESTDB |
+------------------+
| customer |
| customer_addr |
| orders |
| order_items |
| travelrecord |
| t_vote |
+------------------+
6 rows in set (0.00 sec) mysql> create table customer(id int not null,customer_id int not null,datanode varchar(10),primary key(id));
Query OK, 0 rows affected (0.09 sec) mysql> create table customer_addr(id int not null,customer_id int not null,customer_addr varchar(200),datanode varchar(10),primary key(id));
Query OK, 0 rows affected (0.04 sec)

插入数据到customer和customer_addr表

a:插入数据到customer
mysql> insert into customer(id,customer_id,datanode) values(10000,1,database());
Query OK, 1 row affected (0.07 sec)

对应的日志



可以看到写入到db1

b:插入数据到 customer_addr
mysql> insert into customer_addr (id, customer_id, customer_addr, datanode)
values (10000, 10000, 'shanghai', DATABASE())
Query OK, 1 row affected (0.11 sec)

对应的日志

c:继续插入数据到dn2
mysql> insert into customer(id,customer_id,datanode) values(10010,2,database());
Query OK, 1 row affected (0.01 sec) mysql> insert into customer_addr(id,customer_id,customer_addr,datanode) values
(10010,10010,'chengdu',database());
Query OK, 1 row affected (0.11 sec)

4 验证ER分片

#获取全部数据
mysql> explain select customer.id,customer.customer_id,customer.datanode,
customer_addr.customer_addr
from customer,customer_addr
where customer.id=customer_addr.customer_id;
+-----------+-----------------------------------------------------------+
| DATA_NODE | SQL |
+-----------+-----------------------------------------------------------+
| dn1 |select customer.id,customer.customer_id,customer.datanode, |
| | customer_addr.customer_addr |
| |from customer,customer_addr |
| |where customer.id=customer_addr.customer_id; |
| | |
| dn2 |select customer.id,customer.customer_id,customer.datanode, |
| | customer_addr.customer_addr |
| |from customer,customer_addr |
| |where customer.id=customer_addr.customer_id; |
+-----------+-----------------------------------------------------------+

对应的日志

由上可知走的是全部路由

#获取单个分片上的数据
mysql> explain select customer.id,customer.customer_id,customer.datanode,
customer_addr.customer_addr
from customer,customer_addr
where customer.id=customer_addr.customer_id and customer.id=10000;
+-----------+--------------------------------------------------------------------+
| DATA_NODE | SQL |
+-----------+--------------------------------------------------------------------+
| dn1 | select customer.id,customer.customer_id,customer.datanode, |
| | customer_addr.customer_addr |
| | from customer,customer_addr |
| | where customer.id=customer_addr.customer_id and customer.id=10000 |
+-----------+--------------------------------------------------------------------+
1 row in set (0.00 sec)



由上可知路由到了dn1上

Mycat实战之配置EP分片的更多相关文章

  1. linux中mycat的配置,分片,以及主从复制

    1.1    安装环境 1.jdk:要求jdk必须是1.7及以上版本 2.Mysql:推荐mysql是5.5以上版本 1.2  安装步骤 Mycat有windows.linux多种版本.本教程为lin ...

  2. MYCAT实战之分片迁移

    实践扩容 1.要求: travelrecord 表定义为10个分片,尝试将10个分片中的 2 个分片转移到第二台MySQL上, 并完成记录要求,最快的数据迁移做法,中断业务时间最短 2.针对分片以及迁 ...

  3. Mycat实战之主键数据库自增方式

    创建一个 person表,主键为Id,hash方式分片,主键自增(采用数据库方式) #person表结构如下 Id,主键,Mycat自增主键 name,字符串,16字节最长 school,毕业学校,数 ...

  4. MyCAT简单入门配置

    MyCAT简单入门配置 安装jdk 建议1.7以上 安装mysql 安装MyCAT Mycat 源码:https://github.com/MyCATApache/Mycat-Server Mycat ...

  5. AI应用开发实战 - 从零开始配置环境

    AI应用开发实战 - 从零开始配置环境 与本篇配套的视频教程请访问:https://www.bilibili.com/video/av24421492/ 建议和反馈,请发送到 https://gith ...

  6. MyCat基础安装配置-笔记(一)

    概述 Mycat 是一个数据库分库分表中间件,Mycat web 可以对 Mycat进行监控,这里分享一下 Mycat web 的搭建过程 详细内容可以参考 官方文档,下载文档地址:https://g ...

  7. Zookeeper C++编程实战之配置更新

    CZookeeperHelper:https://github.com/eyjian/libmooon/blob/master/include/mooon/net/zookeeper_helper.h ...

  8. webpack4 中的最新 React全家桶实战使用配置指南!

    最新React全家桶实战使用配置指南 这篇文档 是吕小明老师结合以往的项目经验 加上自己本身对react webpack redux理解写下的总结文档,总共耗时一周总结下来的,希望能对读者能够有收获, ...

  9. 【MangoDB分片】配置mongodb分片群集(sharding cluster)

    配置mongodb分片群集(sharding cluster) Sharding cluster介绍 这是一种可以水平扩展的模式,在数据量很大时特给力,实际大规模应用一般会采用这种架构去构建monod ...

随机推荐

  1. 记录下一次错误报http请求500,

    1.请求控制层没问题,能请求到,如果缺少参数都会返回提示信息,但是请求参数都对了以后,居然报500,非常不解 找了好久,不知道哪里错了,最后经理提示是不是有可能,mapper.xml出错了,最后,我将 ...

  2. 【SQL查询】按照多个字段进行排序_order by

    注: 1. 当前面的排序存在重复的项,后面的排序才会起作用. [示例1]:前面的排序不存在重复的项 [示例2]:前面的排序存在重复的项

  3. Ubuntu下搭建WordPress环境

    WordPress是一种使用PHP语言开发的博客平台,用户可以在支持PHP和MySQL数据库的服务器上架设属于自己的网站.也可以把 WordPress当作一个内容管理系统(CMS)来使用.WordPr ...

  4. Lua基础---变量与赋值

    看以下案例: test.lua -- 第一个lua脚本 --注释使用"--"符 --变量未定义时,默认初始化的值为nil --这样的定义为全局 num1 = 1 ; --加了关键字 ...

  5. 1 秒杀系统模拟基础实现,使用DB实现

    本文根据动脑学院的一节类似的课程,改编实现.分别使用DB和redis来完成. 隔离的解释 业务隔离:将秒杀业务独立出来,尽量不与其他业务关联,以减少对其他业务的依赖性.譬如秒杀业务只保留用户id,商品 ...

  6. 使用sqlmap执行SQL注入攻击

    sqlmap是开源的SQL注入自动攻击工具,它可以自动的探测SQL注入点并且进一步控制网站的数据库. Kali Linux默认安装了这个工具. 找到潜在的攻击目标 第一步是找到有SQL注入漏洞的网站. ...

  7. 运行python代码

    IPython IPython 'magic' function documentation

  8. 【Android】Android 学习记录贴

    官网 教程学习笔记 Genymotion 安卓虚拟器太慢,用Genymotion(装载eclipse的插件) 利用Genymotion运行Android应用程序 1.首先,点击 来启动或者创建您要使用 ...

  9. Tomcat启动超时问题Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds

    使用Eclipse启动Tomcat时出现启动超时的问题如下所示: Server Tomcat v7.0 Server at localhost was unable to start within 4 ...

  10. 转载FPGA学习之内嵌乘法器调用

    补充一点,除法的时候如果直接a/b那么就会调用lpm模块,不管输入是否是常数,乘法的时候输入都是reg型变量会调用硬件乘法器,有一个是常数就会调用lpm模块. 上课的时候一直听老师说真正实践的时候你别 ...