mycat分表分库实践

mysql admin 116℃ 0评论

两张表users和item,三个数据库db01,db02,db03(三个库在一个数据库实例上)
users只在db01中存储。
item表被分割到db02和db03中存储。

create database db01 if not exists db01;
use db01;
CREATE TABLE users ( 
id INT NOT NULL AUTO_INCREMENT, 
name varchar(50) NOT NULL default '', 
indate DATETIME NOT NULL default '0000-00-00 00:00:00', 
PRIMARY KEY (id) 
)AUTO_INCREMENT= 1 ENGINE=InnoDB DEFAULT CHARSET=utf8; 

在db02和db03中分别创建item表,SQL脚本如下

create database db02 if not exists db02;
use db02;
 CREATE TABLE item ( 
    id INT NOT NULL AUTO_INCREMENT, 
    value INT NOT NULL default 0, 
    indate DATETIME NOT NULL default '0000-00-00 00:00:00', 
    PRIMARY KEY (id) 
)AUTO_INCREMENT= 1 ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
create database db03 if not exists db03;
use db03;
CREATE TABLE item ( 
    id INT NOT NULL AUTO_INCREMENT, 
    value INT NOT NULL default 0, 
    indate DATETIME NOT NULL default '0000-00-00 00:00:00', 
    PRIMARY KEY (id) 
)AUTO_INCREMENT= 1 ENGINE=InnoDB DEFAULT CHARSET=utf8;

下载mycat:

http://dl.mycat.io/1.6-RELEASE/

然后在conf目录下编辑service.xml,rule.xml,schema.xml三个文件。
service.xml主要配置mycat服务的参数,比如端口号,myact用户名和密码使用的逻辑数据库等
role.xml主要配置路由策略,主要有分片的片键,拆分的策略(取模还是按区间划分等)
schema.xml文件主要配置数据库的信息,例如逻辑数据库名称,物理上真实的数据源以及表和数据源之间的对应关系和路由策略等。

Service.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE mycat:server SYSTEM "server.dtd"> 
<mycat:server xmlns:mycat="http://io.mycat/"> 
        <system> 
            <!--  
                <property name="processors">32</property> 
                <property name="processorExecutor">32</property>  
                <property name="bindIp">0.0.0.0</property>  
                <property name="frontWriteQueueSize">4096</property> 
                <property name="idleTimeout">300000</property> 
                <property name="mutiNodePatchSize">100</property> 
            --> 
                <property name="defaultSqlParser">druidparser</property> 
                <property name="mutiNodeLimitType">1</property> 
                <property name="serverPort">8066</property> 
                <property name="managerPort">9066</property>  
        </system> 
        <!-- 任意设置登陆 mycat 的用户名,密码,数据库  --> 
        <user name="test"> 
                <property name="password">test</property> 
                <property name="schemas">TESTDB</property> 
        </user> 
 
        <user name="user"> 
                <property name="password">user</property> 
                <property name="schemas">TESTDB</property> 
                <property name="readOnly">true</property> 
        </user> 
        <!--  
        <quarantine>  
           <whitehost> 
              <host host="127.0.0.1" user="mycat"/> 
              <host host="127.0.0.2" user="mycat"/> 
           </whitehost> 
       <blacklist check="false"></blacklist> 
        </quarantine> 
        --> 
</mycat:server> 

Role.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- - - Licensed under the Apache License, Version 2.0 (the "License");
    - you may not use this file except in compliance with the License. - You
    may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0
    - - Unless required by applicable law or agreed to in writing, software -
    distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT
    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the
    License for the specific language governing permissions and - limitations
    under the License. -->
<!DOCTYPE mycat:rule SYSTEM "rule.dtd">
<mycat:rule xmlns:mycat="http://io.mycat/">
 
    <tableRule name="role1">
        <rule>
            <columns>id</columns>
            <algorithm>mod-long</algorithm>
        </rule>
    </tableRule>
 
    <function name="mod-long" class="io.mycat.route.function.PartitionByMod">
        <!-- how many data nodes -->
        <property name="count">2</property>
    </function>
</mycat:rule>

schema.xml 数据库的url,usename,password按实际情况填写。

<?xml version="1.0"?> 
<!DOCTYPE mycat:schema SYSTEM "schema.dtd"> 
<mycat:schema xmlns:mycat="http://io.mycat/"> 
 
    <!-- 设置表的存储方式.schema name="TESTDB" 与 server.xml中的 TESTDB 设置一致  --> 
    <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100"> 
        <table name="users" primaryKey="id"  dataNode="node_db01" /> 
        <table name="item" primaryKey="id" dataNode="node_db02,node_db03" rule="role1" /> 
 
    </schema> 
 
    <!-- 设置dataNode 对应的数据库,及 mycat 连接的地址dataHost --> 
    <dataNode name="node_db01" dataHost="dataHost01" database="db01" /> 
    <dataNode name="node_db02" dataHost="dataHost01" database="db02" /> 
    <dataNode name="node_db03" dataHost="dataHost01" database="db03" /> 
 
    <!-- mycat 逻辑主机dataHost对应的物理主机.其中也设置对应的mysql登陆信息 --> 
    <dataHost name="dataHost01" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native"> 
            <heartbeat>select user()</heartbeat> 
            <writeHost host="server1" url="127.0.0.1:3306" user="root" password="123456"/> 
    </dataHost> 
</mycat:schema>

启动测试
在命令行切换到bin目录下,然后执行如下命令:

bin/startup_nowrap.sh 

在命令行访问mycat逻辑数据库,采用如下的命令:

mysql -utest -ptest -h127.0.0.1 -P8066 -DTESTDB

现在通过数据库查询数据库和表,发现只有逻辑数据库TESTDB而不是db01, db02, db03;而且表也是统一显示的,而不是分布在不同的实际数据库中。

下面进行分表测试:

insert into users(name,indate) values('kk',now());
insert into users(name,indate) values('ss',now());
insert into item(id,value,indate) values(1,100,now());
insert into item(id,value,indate) values(2,100,now());

在mycat上能看到插入成功

登录实际的数据库,看分表是否成功

转载请注明:朋克网 » mycat分表分库实践

喜欢 (0)
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址