mycat 垂直切分和水平切分配置示例

2 篇文章 0 订阅

mycat 垂直切分和水平切分配置示例

首先是数据库表的分布:
192.168.0.83:db1数据库 test1表;db3数据库 user表 user_detail表
192.168.0.165:db1数据库 test2表;db3数据库 user表 user_detail表
sql:

/*
Navicat MySQL Data Transfer

Source Server         : localhost
Source Server Version : 50713
Source Host           : localhost:3306
Source Database       : db1

Target Server Type    : MYSQL
Target Server Version : 50713
File Encoding         : 65001

Date: 2016-08-26 17:23:56
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `test1`
-- ----------------------------
DROP TABLE IF EXISTS `test1`;
CREATE TABLE `test1` (
  `id` int(11) NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of test1
-- ----------------------------


/*
Navicat MySQL Data Transfer

Source Server         : localhost
Source Server Version : 50713
Source Host           : localhost:3306
Source Database       : db3

Target Server Type    : MYSQL
Target Server Version : 50713
File Encoding         : 65001

Date: 2016-08-26 17:24:04
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) NOT NULL,
  `user_id` varchar(255) DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of user
-- ----------------------------

/*
Navicat MySQL Data Transfer

Source Server         : localhost
Source Server Version : 50713
Source Host           : localhost:3306
Source Database       : db3

Target Server Type    : MYSQL
Target Server Version : 50713
File Encoding         : 65001

Date: 2016-08-26 17:24:18
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `user_detail`
-- ----------------------------
DROP TABLE IF EXISTS `user_detail`;
CREATE TABLE `user_detail` (
  `id` int(11) NOT NULL,
  `user_id` varchar(255) DEFAULT NULL,
  `detail` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of user_detail
-- ----------------------------


/*
Navicat MySQL Data Transfer

Source Server         : song
Source Server Version : 50713
Source Host           : 192.168.0.165:3306
Source Database       : db1

Target Server Type    : MYSQL
Target Server Version : 50713
File Encoding         : 65001

Date: 2016-08-26 17:24:27
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `test2`
-- ----------------------------
DROP TABLE IF EXISTS `test2`;
CREATE TABLE `test2` (
  `id` int(11) NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of test2
-- ----------------------------

schema.xml文件:

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://org.opencloudb/">



         <schema name="qs" checkSQLschema="false" sqlMaxLimit="100">
                <!-- 垂直切分 -->
                <table name="test1" primaryKey="id" type="global" dataNode="nodeTest1" />
                <table name="test2" primaryKey="id" type="global" dataNode="nodeTest2" />

                <!-- 全局表的配置如下(比如配置文件的数据,数据不大很少变动的,但是经常用到查询的) -->
                <!-- <table name="t_area" primaryKey="id" type="global" dataNode="dn1,dn2" /> -->

                <!-- 水平切分 -->
                <!-- ER分片表:如user表和user_detail表的关联字段user_id;保证相同user_id的数据在同一块片区上 -->
                <table name="user" primaryKey="user_id" dataNode="nodeUser01,nodeUser02" rule="mod-long">
                    <childTable name="user_detail" primaryKey="id" joinKey="user_id" parentKey="user_id" />
                </table>
         </schema>




         <dataNode name="nodeUser01" dataHost="dataHost01" database="db3" />
         <dataNode name="nodeUser02" dataHost="dataHost02" database="db3" />

         <dataNode name="nodeTest1" dataHost="dataHost01" database="db1" />
         <dataNode name="nodeTest2" dataHost="dataHost02" database="db1" />



        <dataHost name="dataHost01" maxCon="1000" minCon="10" balance="0"
                writeType="0" dbType="mysql" dbDriver="native">
                <heartbeat>select user()</heartbeat>
                <writeHost host="hostM1" url="192.168.0.83:3306" user="root" password="root"/>
        </dataHost>

        <dataHost name="dataHost02" maxCon="1000" minCon="10" balance="0"
                writeType="0" dbType="mysql" dbDriver="native">
                <heartbeat>select user()</heartbeat>
                <writeHost host="hostM1" url="192.168.0.165:3306" user="root" password="root"/>
        </dataHost>

</mycat:schema>

注: rule=”mod-long” 分片规则是根据分片字段求模运算;
分片规则有:固定分片hash,范围约定,一致性hash,按日期范围等。
详见官网文档 http://www.mycat.org.cn/document/Mycat_V1.6.0.pdf

测试:
C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -uroot -proot -hlocalhost -P8066 -Dqs
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.5.8-mycat-1.4-release-20151019230038 MyCat Server (OpenCloundDB)

Copyright (c) 2000, 2016, 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> show databases;
+———-+
| DATABASE |
+———-+
| qs |
+———-+
1 row in set (0.00 sec)

mysql> show tables;
+————–+
| Tables in qs |
+————–+
| test1 |
| test2 |
| user |
| user_detail |
+————–+
4 rows in set (0.00 sec)

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值