微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

mycat分库分表示例

演 示 案 例 场 景 是 分 别 创 建 了 3 个 数 据 库 db01 、 db02 、 db03 , 有 4 张 表 users 、 item 、 customer 、
orders,并分别在这3个数据库下都创建了相同的4张表。建表语句如下:

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;
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 TABLE customer(id int primary key,name varchar(30));
CREATE TABLE orders(id int primary key,name varchar(30),customer_id int,
constraint fk_companyid foreign key(customer_id)references customer(id));

server.xml数据库名称为teset_mycat,用户名为mycat,密码为redhat,服务端口为8066

<!DOCTYPE mycat:server SYstem "server.dtd">
<mycat:server xmlns:mycat="http://io.mycat/">
        <system>
        <property name="usesqlStat">0</property>  <!-- 1为开启实时统计、0为关闭 -->
        <property name="useGlobleTableCheck">0</property>  <!-- 1为开启全加班一致性检测、0为关闭 -->

                <property name="sequnceHandlerType">2</property>
                <property name="processorBufferPoolType">0</property>
                <property name="handledistributedTransactions">0</property>

                <property name="uSEOffheapForMerge">1</property>
                <property name="memoryPageSize">1m</property>
                <property name="spillsfilebufferSize">1k</property>

                <property name="useStreamOutput">0</property>
                <property name="systemReserveMemorySize">384m</property>

                <property name="useZKSwitch">true</property>

<!--以上内容中,只是删除了一些注释行的内容,主要是下面-->
        </system>
        <user name="mycat" >
                <property name="password">redhat</property>
                <property name="schemas">teset_mycat</property>
        </user>
</mycat:server>

Mycat的schema.xml配置内容如下:

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYstem "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
        <schema name="teset_mycat" checksqlschema="false" sqlMaxLimit="100">
           <table name="users" primaryKey="id" datanode="node_db01"/>
           <table name="item"  primaryKey="id" datanode="node_db02,node_db03" rule="rule1"/>
           <table name="customer" datanode="node_db01,node_db02,node_db03" rule="auto-sharding-long-custom">
           <childTable name="orders" joinKey="customer_id" parentKey="id"/>
        </table>
        </schema>
        <datanode name="node_db01" dataHost="host1" database="db01" />
        <datanode name="node_db02" dataHost="host1" database="db02" />
        <datanode name="node_db03" dataHost="host1" database="db03" />
        <dataHost name="host1" maxCon="1000" minCon="10" balance="0"
                          writeType="0" dbType="MysqL" dbDriver="native">
                <heartbeat>select user()</heartbeat>
                <!-- can have multi write hosts -->
                <writeHost host="hostM1" url="192.168.1.3:3306" user="root"
                                   password="redhat"/>
                <!--readHost host="hostS2" url="192.168.1.4:3306" user="root" password="redhat" /> -->
                        <!-- can have multi read hosts -->
        </dataHost>
</mycat:schema>

分片规则rule.xml的配置Item表的分片规则为rule1,分片算法为取模分片mod-
long;表customer的分片规则是auto-sharding-long-custom,分片算法为rang-long。

<?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>

    <tableRule name="auto-sharding-long-custom">
        <rule>
            <columns>id</columns>
            <algorithm>rang-long</algorithm>
        </rule>
    </tableRule>

    <function name="mod-long" class="io.mycat.route.function.PartitionByMod">
        <!-- how many data nodes -->
        <property name="count">2</property>
    </function>

    <function name="rang-long" class="io.mycat.route.function.AutopartitionByLong">
            <property name="mapFile">auto-sharding-long-custom.txt</property>
    </function>
</mycat:rule>

创建规则文件auto-sharding-long-custom.txt,如图13-14所示,id属于0~1000范围内的在分区1
里、1000~2000的在分区2里、2000~3000的在分区3里

#range start-end,data node index
0-1000=0
1000-2000=1
2000-3000=2

重启mycat验证:
[root@server3 mycat]# MysqL -u mycat -predhat -P 8066 -h 192.168.1.3
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MysqL connection id is 1
Server version: 5.6.29-mycat-1.6.7.1-release-20190627191042 MyCat Server (OpenCloudDB)

copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MysqL [(none)]> show databases;
+-------------+
| DATABASE |
+-------------+
| teset_mycat |
+-------------+
1 row in set (0.02 sec)

MysqL [(none)]> use teset_mycat;
Reading table @R_405_4045@ion for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MysqL [teset_mycat]> show tables;
+-----------------------+
| Tables in teset_mycat |
+-----------------------+
| customer |
| item |
| orders |
| users |
+-----------------------+
4 rows in set (0.00 sec)

往表中插入数据,sql语句如下:

insert into users(name,indate) values('LL',Now());
insert into users(name,indate) values('HH',Now());
insert into item(id,value,indate) values(1,100,Now());
insert into item(id,value,indate) values(2,100,Now());
insert into customer(id,name) values(999,'dan');
insert into customer(id,name) values(1000,'jiao');
insert into customer(id,name) values(1003,'song');
insert into customer(id,name) values(2002,'yang');
insert into orders(id,name,customer_id) values(1,'mirror',999);
insert into orders(id,name,customer_id) values(2,'banana',2002);
insert into orders(id,name,customer_id) values(3,'apple',1003);
insert into orders(id,name,customer_id)values(4,'pear',2002);

查询各表:
MysqL [teset_mycat]> select * from users;
+----+------+---------------------+
| id | name | indate |
+----+------+---------------------+
| 1 | LL | 2021-04-12 14:10:11 |
| 2 | HH | 2021-04-12 14:10:11 |
+----+------+---------------------+
2 rows in set (0.06 sec)

MysqL [teset_mycat]> select * from item
-> ;
+----+-------+---------------------+
| id | value | indate |
+----+-------+---------------------+
| 2 | 100 | 2021-04-12 14:10:11 |
| 1 | 100 | 2021-04-12 14:10:11 |
+----+-------+---------------------+
2 rows in set (0.15 sec)

MysqL [teset_mycat]> select * from customer;
+------+------+
| id | name |
+------+------+
| 999 | dan |
| 1000 | jiao |
| 1003 | song |
| 2002 | yang |
+------+------+
4 rows in set (0.03 sec)

切换到后端的MysqL服务器,验证结果:
MariaDB [(none)]> select * from db01.users;
+----+------+---------------------+
| id | name | indate |
+----+------+---------------------+
| 1 | LL | 2021-04-12 14:10:11 |
| 2 | HH | 2021-04-12 14:10:11 |
+----+------+---------------------+
2 rows in set (0.00 sec)

MariaDB [(none)]> select * from db02.users;
Empty set (0.01 sec)

MariaDB [(none)]> select * from db03.users;
Empty set (0.01 sec)
切换到后端的MysqL服务器,验证结果users表只存储在数据库db01里

MariaDB [(none)]> select * from db01.item;
Empty set (0.00 sec)

MariaDB [(none)]> select * from db02.item;
+----+-------+---------------------+
| id | value | indate |
+----+-------+---------------------+
| 2 | 100 | 2021-04-12 14:10:11 |
+----+-------+---------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> select * from db03.item;
+----+-------+---------------------+
| id | value | indate |
+----+-------+---------------------+
| 1 | 100 | 2021-04-12 14:10:11 |
+----+-------+---------------------+
1 row in set (0.00 sec)
item表的数据只分布存储在数据库db02和db03中,数据库db01中没有item表的数据

MariaDB [(none)]> select * from db01.customer;
+------+------+
| id | name |
+------+------+
| 999 | dan |
| 1000 | jiao |
+------+------+
2 rows in set (0.00 sec)

MariaDB [(none)]> select * from db02.customer;
+------+------+
| id | name |
+------+------+
| 1003 | song |
+------+------+
1 row in set (0.00 sec)

MariaDB [(none)]> select from db03.customer;
+------+------+
| id | name |
+------+------+
| 2002 | yang |
+------+------+
1 row in set (0.00 sec)
customer表中id属于0~1000范围内的在db01.customer表中,id属于1000~2000的在db02.customer表
中,id属于2000~3000的在db03.customer表中
MariaDB [(none)]> select
from db01.orders;
+----+--------+-------------+
| id | name | customer_id |
+----+--------+-------------+
| 1 | mirror | 999 |
+----+--------+-------------+
1 row in set (0.00 sec)

MariaDB [(none)]> select * from db02.orders;
+----+-------+-------------+
| id | name | customer_id |
+----+-------+-------------+
| 3 | apple | 1003 |
+----+-------+-------------+
1 row in set (0.00 sec)

MariaDB [(none)]> select * from db03.orders;
+----+--------+-------------+
| id | name | customer_id |
+----+--------+-------------+
| 2 | banana | 2002 |
| 4 | pear | 2002 |
+----+--------+-------------+
2 rows in set (0.00 sec)

Mycat支持ER分片,orders表依赖父表customer进行分片,即子表的记录与所关联的父表记录存放在
一个数据分片上。如图13-22所示,orders表的customer_id列对应的customer表的id列属于哪个分片,
orders表的那条记录就在哪个分片内

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐