和垂直分库不同,水平分表,是将那些io频繁,且数据量大的表进行水平切分。
基本的配置和垂直分库一样,我们需要改的就是我们的
schema.xml和rule.xml文件配置(server.xml不用做任何修改)
除此之外,我们还需要在两个分片数据库服务器上建立分片用的数据库10.0.4.181上建立(orderdb01,orderdb02),10.0.4.183上建立(orderdb03,orderdb04)
现在我们对配置文件进行配置。
其中schema.xml这样配置。
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="imooc_db" checkSQLschema="false" sqlMaxLimit="100">
<table name="mytest" primaryKey="id" dataNode="dn1" />
<table name="testfp" primaryKey="id" dataNode="dn2" />
<table name="order_list" primaryKey="id" dataNode="orderdb01,orderdb02,orderdb03,orderdb04" rule="order_list" />
</schema>
<dataNode name="dn1" dataHost="mysql4181" database="imooc_db" />
<dataNode name="dn2" dataHost="mysql4183" database="imooc_db" />
<dataNode name="orderdb01" dataHost="mysql4181" database="orderdb01" />
<dataNode name="orderdb02" dataHost="mysql4181" database="orderdb02" />
<dataNode name="orderdb03" dataHost="mysql4183" database="orderdb03" />
<dataNode name="orderdb04" dataHost="mysql4183" database="orderdb04" />
<dataHost name="mysql4181" maxCon="1000" minCon="10" balance="3" writeType="0" dbType="mysql" dbDriver="native" switchType="1">
<heartbeat>select user()</heartbeat>
<writeHost host="10.0.4.181" url="10.0.4.181:3306" user="im_mycat" password="123456"></writeHost>
</dataHost>
<dataHost name="mysql4183" maxCon="1000" minCon="10" balance="3" writeType="0" dbType="mysql" dbDriver="native" switchType="1">
<heartbeat>select user()</heartbeat>
<writeHost host="10.0.4.183" url="10.0.4.183:3306" user="im_mycat" password="123456"></writeHost>
</dataHost>
</mycat:schema>
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="order_list">
<rule>
<columns>id</columns>
<algorithm>mod-long</algorithm>
</rule>
</tableRule>
<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
<property name="count">4</property>
</function>
</mycat:rule>
配置好后,进行mycat的重启。
mycat restart
登录mycat
mysql -uapp_imooc -p123456 -h10.0.4.180 -P8066
插入数据。
mysql> insert into order_list(id,order_name,order_type) values(1,'order01',1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into order_list(id,order_name,order_type) values(2,'order01',1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into order_list(id,order_name,order_type) values(3,'order01',1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into order_list(id,order_name,order_type) values(4,'order01',1);
Query OK, 1 row affected (0.01 sec)
mysql> insert into order_list(id,order_name,order_type) values(5,'order01',1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into order_list(id,order_name,order_type) values(6,'order01',1);
Query OK, 1 row affected (0.01 sec)
mysql> insert into order_list(id,order_name,order_type) values(7,'order01',1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into order_list(id,order_name,order_type) values(8,'order01',1);
Query OK, 1 row affected (0.01 sec)
mysql> insert into order_list(id,order_name,order_type) values(9,'order01',1);
Query OK, 1 row affected (0.01 sec)
mysql> insert into order_list(id,order_name,order_type) values(10,'order01',1);
Query OK, 1 row affected (0.00 sec)
我们可以看到,我们在mycat上插入的数据,最终通过对id的取模算法,分别插入到了orderdb01,orderdb02,orderdb03,orderdb04
通过以上结果,我们可以看到,数据被平均的分配到了4各数据库中。
但是,做到这里还不算完,并不能用于生产环境,因为还有很多问题,比如全局自增主键的问题和联合查询的问题。上面我的实例之所以分配的很平均是因为,我在插入的时候规定了主键值。所以后端的四个数据库中的全部数据中没有主键重复的,
如果我不规定主键,则order_list中可能出现四个id为1的记录,或四个id为2的记录。