mycat的入門教程(二)

在上篇教程中初步介紹了mycat的使用,接下來搭建一個拆分的策略(取模還是按區間劃分等)的入門教程來實現一個分庫分表的功能

三張表users和test,三個數據庫test,test2,dmeo(三個庫在一個數據庫實例上)
users表被分割到test,test2中存儲。
test在dmeo中存儲。

創建三個數據庫,並創建對應的表

-- ----------------------------
-- Table structure for `users`
-- ----------------------------
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `phone` varchar(11) DEFAULT NULL,
  `create_time` datetime NOT NULL,
  `update_time` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT='用戶測試表';

配置如下所示:
(1)rule.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="rule1">
        <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>

(2)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"> 
        <table name="test" primaryKey="ID" type="global"  dataNode="dn1" />
        <table name="users" primaryKey="ID"  dataNode="dn2,dn3" rule="rule1"/> 
    </schema> 

    <dataNode name="dn1" dataHost="localhost1" database="dmeo" />
    <dataNode name="dn2" dataHost="localhost1" database="test" /> 
    <dataNode name="dn3" dataHost="localhost1" database="test2" />  

    <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="root">
            <!-- can have multi read hosts -->
            <readHost host="hostS2" url="127.0.0.1:3306" user="root" password="root" />
        </writeHost> 
    </dataHost> 
</mycat:schema>

(3)server.xml和wrapper.conf這兩文件和mycat的入門教程(一)中配置一樣,沒有發生改變

在邏輯數據庫TESTDB的users表中插入三條數據,如下顯示:
這裏寫圖片描述
對應的test和test2兩個數據庫中的數據,如下顯示:
這裏寫圖片描述
這裏寫圖片描述
通過上面的數據來看,根據實際的路由策略進行了分表,對數據實現了分庫分表!

發佈了40 篇原創文章 · 獲贊 17 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章