zoukankan      html  css  js  c++  java
  • 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="100">
            <!-- 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="1000" minCon="10" balance="0"
                  writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
            <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="123456" /> 
        </dataHost>
    
    </mycat:schema>

    表简介

         travelrecord :
                分库dn1,dn2,dn3 
                分库规则:app_user_id      
                    M=10000;
                    0-500M=0;    #0-500万 dn1
                    500M-1000M=1;    #500万-1000万 dn2
                    1000M-1500M=2;
            company:全局表
                分库dn1,dn2,dn3        
            goods:全局表
                分库dn1,dn2
            hotnews :自增id
                分库dn1,dn2,dn3    
                分库规则:mod-long,根据节点id/节点数,进行平均分配
            employee:
                分库:dn1,dn2,根据state状态分库
                    10000=0;    #   sharding_id=10000 dn1
                    10010=1;    #   sharding_id=10010 dn2
            customer:主表
                分库:dn1,dn2,根据state状态分库
                    10000=0;    #   sharding_id=10000 dn1
                    10010=1;    #   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(100),traveldate DATE, fee decimal,app_user_id int); Query OK, 0 rows affected (0.05 sec) mysql> insert into travelrecord(id,user_id,traveldate,fee,app_user_id) values(1,'Victor',20160101,100,1); Query OK, 1 row affected (0.09 sec) mysql> insert into travelrecord(id,user_id,traveldate,fee,app_user_id) values(5000001,'Job',20160102,100,2); Query OK, 1 row affected (0.00 sec) mysql> insert into travelrecord(id,user_id,traveldate,fee,app_user_id) values(10000001,'Slow',20160103,100,3); Query OK, 1 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是分片规则的配置文件

  • 相关阅读:
    sql 2008 r2
    js压缩反压缩
    .NET调用Java写的WebServices(可能会碰到的问题)
    ecshop
    Chrome插件
    Unity3d ShaderLab之WorldNormalVector
    Unity 3d中Shader是什么,可以吃吗?
    java,类的继承
    java,关于POJO,查阅资料
    java,编写一个汽车类
  • 原文地址:https://www.cnblogs.com/fxwl/p/7992382.html
Copyright © 2011-2022 走看看