mycat 入门教程
mycat 入门教程
之前已经对mycat的配置进行了详细记得介绍,下面就是一个mycat分库的小例子
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="">
<!-- auto sharding by id (long) -->
<table name="travelrecord" dataNode="dn1,dn2,dn3" rule="auto-sharding-long" /> <!-- global table is auto cloned to all defined data nodes ,so can join
with any table whose sharding node is in the same data node -->
<table name="company" primaryKey="ID" type="global" dataNode="dn1,dn2,dn3" />
<table name="goods" primaryKey="ID" type="global" dataNode="dn1,dn2" />
<!-- random sharding using mod sharind rule -->
<table name="hotnews" primaryKey="ID" autoIncrement="true" dataNode="dn1,dn2,dn3"
rule="mod-long" /> <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> </schema> <dataNode name="dn1" dataHost="localhost1" database="db1" />
<dataNode name="dn2" dataHost="localhost1" database="db2" />
<dataNode name="dn3" dataHost="localhost1" database="db3" /> <dataHost name="localhost1" maxCon="" minCon="" balance=""
writeType="" dbType="mysql" dbDriver="native" switchType="" slaveThreshold="">
<heartbeat>select user()</heartbeat>
<!-- can have multi write hosts -->
<writeHost host="hostM1" url="localhost:3306" user="root"
password="">
<!-- can have multi read hosts -->
<readHost host="hostS2" url="192.168.1.200:3306" user="root" password="xxx" />
</writeHost>
<writeHost host="hostS1" url="localhost:3316" user="root"
password="" />
</dataHost> </mycat:schema>
表简介
travelrecord :
分库dn1,dn2,dn3
分库规则:app_user_id
M=;
-500M=; #-500万 dn1
500M-1000M=; #500万-1000万 dn2
1000M-1500M=;
company:全局表
分库dn1,dn2,dn3
goods:全局表
分库dn1,dn2
hotnews :自增id
分库dn1,dn2,dn3
分库规则:mod-long,根据节点id/节点数,进行平均分配
employee:
分库:dn1,dn2,根据state状态分库
=; # sharding_id= dn1
=; # sharding_id=10010 dn2
customer:主表
分库:dn1,dn2,根据state状态分库
=; # sharding_id= dn1
10010=; # sharding_id=10010 dn2
customer_addr:子表
关联字段:customer_id
链接MYSQL 创建数据库
mysql> CREATE DATABASE `db1` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected
mysql> CREATE DATABASE `db2` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected
mysql> CREATE DATABASE `db3` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected
链接Mycat
mysql -h127.0.0.1 -uroot -p123456 -P8066
插入表
mysql> show mydb;
Database changed
<!-- 根据id进行分段分库 -->
mysql> create table travelrecord (id bigint not null primary key,user_id varchar(),traveldate DATE, fee decimal,app_user_id int);
Query OK, rows affected (0.05 sec) mysql> insert into travelrecord(id,user_id,traveldate,fee,app_user_id) values(,'Victor',,,);
Query OK, row affected (0.09 sec) mysql> insert into travelrecord(id,user_id,traveldate,fee,app_user_id) values(,'Job',,,);
Query OK, row affected (0.00 sec) mysql> insert into travelrecord(id,user_id,traveldate,fee,app_user_id) values(,'Slow',,,);
Query OK, row affected (0.00 sec) 结果:dn1,dn2,dn3分别建表,三条数据根据id分库
<!-- 全局表,每个库中保存全部数据 -->
mysql> create table company (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int);
Query OK, 0 rows affected (0.08 sec)
mysql> insert into company(id,user_id,traveldate,fee,days) values(1,'Victor',20160101,100,10);
Query OK, 1 row affected (0.01 sec)
mysql> insert into company(id,user_id,traveldate,fee,days) values(2,'Job',20160102,100,10);
Query OK, 1 row affected (0.00 sec)
结果:每个库中保存全部数据。
<!-- 分片存储,平均分配到每个库中 -->
mysql> create table hotnews (id bigint not null primary key,user_id varchar(100),traveldate DATE, fee decimal,days int);
Query OK, 0 rows affected (0.06 sec)
mysql> insert into hotnews(id,user_id,traveldate,fee,days) values(1,'Victor',20160101,100,10);
Query OK, 1 row affected (0.01 sec)
mysql> insert into hotnews(id,user_id,traveldate,fee,days) values(2,'Job',20160102,100,10);
Query OK, 1 row affected (0.00 sec)
mysql> insert into hotnews(id,user_id,traveldate,fee,days) values(3,'Job',20160102,100,10);
Query OK, 1 row affected (0.00 sec)
mysql> insert into hotnews(id,user_id,traveldate,fee,days) values(4,'Job',20160102,100,10);
Query OK, 1 row affected (0.00 sec)
mysql> insert into hotnews(id,user_id,traveldate,fee,days) values(5,'Job',20160102,100,10);
Query OK, 1 row affected (0.00 sec)
mysql> insert into hotnews(id,user_id,traveldate,fee,days) values(6,'Job',20160102,100,10);
Query OK, 1 row affected (0.00 sec)
结果:根据id平均分配储存到每个库中
<!-- 枚举法:根据配置字段识别分别分配到每个库中 -->
mysql> create table employee(id bigint not null primary key,name varchar(100),sharding_id int);
Query OK, 0 rows affected (0.04 sec)
mysql> insert into employee(id,name,sharding_id) values(1,'Victor',10000);
Query OK, 1 row affected (0.05 sec)
mysql> insert into employee(id,name,sharding_id) values(2,'Victor',10010);
Query OK, 1 row affected (0.00 sec)
mysql> insert into employee(id,name,sharding_id) values(3,'Victor',10010);
Query OK, 1 row affected (0.00 sec)
mysql> insert into employee(id,name,sharding_id) values(4,'employee',10010);
Query OK, 1 row affected (0.00 sec)
mysql> insert into employee(id,name,sharding_id) values(5000001,'employee',10000);
Query OK, 1 row affected (0.00 sec)
<!-- 关联表:主表:customer 字表customer_addr->
mysql> create table customer(id bigint not null primary key,name varchar(100),sharding_id int);
Query OK, 0 rows affected (0.04 sec)
mysql> insert into customer(id,name,sharding_id) values(1,'Victor',10000);
Query OK, 1 row affected (0.00 sec)
mysql> insert into customer(id,name,sharding_id) values(2,'Job',10010);
Query OK, 1 row affected (0.00 sec)
mysql> create table customer_addr(id bigint not null primary key,addr varchar(100),customer_id int);
Query OK, 0 rows affected (0.05 sec)
mysql> insert into customer_addr(id,addr,customer_id) values(1,'home',1);
Query OK, 1 row affected (0.20 sec)
mysql> insert into customer_addr(id,addr,customer_id) values(2,'this',2);
Query OK, 1 row affected (0.41 sec)
总结:
关于MyCAT的配置其实是蛮简单的,最主要的是熟悉各配置文件的规则。以上用户名,密码,如何分库,都是在配置文件中定义的,后续,有时间再一一详表。
关于配置文件,conf目录下主要以下三个需要熟悉。
server.xml是Mycat服务器参数调整和用户授权的配置文件
schema.xml是逻辑库定义和表以及分片定义的配置文件
rule.xml是分片规则的配置文件
mycat 入门教程的更多相关文章
- mycat入门教程
github https://github.com/MyCATApache/Mycat-Server myCat介绍 myCat的诞生,要从其前身Amoeba和Cobar说起. Amoeba(变形虫) ...
- wepack+sass+vue 入门教程(三)
十一.安装sass文件转换为css需要的相关依赖包 npm install --save-dev sass-loader style-loader css-loader loader的作用是辅助web ...
- wepack+sass+vue 入门教程(二)
六.新建webpack配置文件 webpack.config.js 文件整体框架内容如下,后续会详细说明每个配置项的配置 webpack.config.js直接放在项目demo目录下 module.e ...
- wepack+sass+vue 入门教程(一)
一.安装node.js node.js是基础,必须先安装.而且最新版的node.js,已经集成了npm. 下载地址 node安装,一路按默认即可. 二.全局安装webpack npm install ...
- Content Security Policy 入门教程
阮一峰文章:Content Security Policy 入门教程
- gulp详细入门教程
本文链接:http://www.ydcss.com/archives/18 gulp详细入门教程 简介: gulp是前端开发过程中对代码进行构建的工具,是自动化项目的构建利器:她不仅能对网站资源进行优 ...
- UE4新手引导入门教程
请大家去这个地址下载:file:///D:/UE4%20Doc/虚幻4新手引导入门教程.pdf
- ABP(现代ASP.NET样板开发框架)系列之2、ABP入门教程
点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之2.ABP入门教程 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)” ...
- webpack入门教程之初识loader(二)
上一节我们学习了webpack的安装和编译,这一节我们来一起学习webpack的加载器和配置文件. 要想让网页看起来绚丽多彩,那么css就是必不可少的一份子.如果想要在应用中增加一个css文件,那么w ...
随机推荐
- Linux下Maven+SVN自动打包脚本
公司的开发环境每次部署项目都很麻烦,需要手动打包并上传上去.这个太麻烦了,所以就准备搞个自动打包的脚本.脚本自动从svn代码库里面更新最新的代码下来,然后maven打包,最后把war包丢到to ...
- 使用VS2013和git进行代码管理
git是一款非常流行的分布式版本控制系统,使用Local Repository追踪代码的修改,通过Push和Pull操作,将代码changes提交到Remote Repository,或从Remote ...
- Mysql + Mybatis动态建表
service层业务 package com.zx.common.service.impl; import com.zx.common.entity.SysUser; import com.zx.co ...
- stl源码剖析 详细学习笔记 算法(3)
//---------------------------15/03/30---------------------------- //min_element template<class Fo ...
- 爱普生L313彩色打印相片
操作环境: windows 和MAC 一.普通打印(默认选项) 1.爱普生L313 普通默认打印为快速不清晰打印. 2.以上打印效果出来图片比较快速出图,但是清晰度不够 二.照片打印设置 1.照片设置 ...
- Unity Inspector添加自定义按钮(Button)
在Unity开发游戏的时候,为了有一个更快更方便的工作流,我们往往会在Editor下开发一些方便实用的工具.在工具中,用到最多,最关键的就是按钮,它是工具的首席执行官.下面就用最简单的代码来演示添加一 ...
- muduo网络库学习笔记(三)TimerQueue定时器队列
目录 muduo网络库学习笔记(三)TimerQueue定时器队列 Linux中的时间函数 timerfd简单使用介绍 timerfd示例 muduo中对timerfd的封装 TimerQueue的结 ...
- B1022. D进制的A+B
除基取余法 #include<bits/stdc++.h> using namespace std; stack<int> s; int main(){ long long a ...
- dijkstra算法计算最短路径和并输出最短路径
void dijisitela(int d, int m1) { ], book[], path[], u, v, min; l = ; ; i < n1; i++) { dis[i] = w[ ...
- Actual Time Cost