数据库系列课程(06)-MyCat分片按省操作数据库例子

1.引言

MyCat中的路由结果是通过分片字段和分片方法来确定的,如果查询条件中有 id 字段的情况还好,查询将会落到某个具体的分片。

如果查询没有分片的字段,会向所有的db都会查询一遍,让后封装结果级给客户端。

那么实际情况中,在Mycat中分片是如何实现的呢?下面来举个例子。

需求:

  • 数据库中主要存放“省”的信息,如广东省要放到广东省数据库,浙江省要放到浙江省数据。。。每个省都有自己的数据库。现在利用Mycat来实现不同省存放到不同的数据库功能。

2.分片枚举法实现

在服务器192.168.162.132新建四个示例数据库:gd_db(广东数据库)、zj_db(浙江数据库)、bj_db(北京数据库)、other_db(其它省份数据库)。每个数据库均新建表intro_tb,内容如下,现在要求在Mycat根据字段name(省份名称)来存放到对应的数据库:

字段 解析
name 省份名称
createTime 创建日期

在这里插入图片描述

1. 配置分片规则

A . partition-hash-int.txt(分片配置文件):

guangdong=0
beijing=1
zhejiang=2

B. 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="role2">
             <rule>
                  <columns>name</columns>
                  <algorithm>hash-int</algorithm>
              </rule>
    </tableRule>
	
	<function name="hash-int" class="io.mycat.route.function.PartitionByFileMap">
		<property name="mapFile">partition-hash-int.txt</property>
		<property name="type">1</property>
		<property name="defaultNode">3</property>
	</function>
	
</mycat:rule>

解析:

  1. 在rule.xml定义分片规则,名字为“role2”,以表中列名为name来分片。具体的分片算法方法名为“hash-int”
  2. 分片算法“hash-int”,使用的是“分片枚举算法”,声明class=“io.mycat.route.function.PartitionByFileMap”。
  3. 里面的具体name对应的数据库节点枚举看“partition-hash-int.txt”文件,默认节点为3。(每个数据库节点对应的节点号看schema.xml文件

2. 配置schema.xml

内容如下:

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
    <!-- TESTDB1 是mycat的逻辑库名称,链接需要用的 -->
    <schema name="mycat_testdb" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1">
	 
	  <table name="intro_db"  dataNode="dn1,dn2,dn3,dn4" rule="role2" /> 
	
	</schema>
    
    <!-- database 是MySQL数据库的库名 -->
    <dataNode name="dn1" dataHost="localhost1" database="gd_db" />
	<dataNode name="dn2" dataHost="localhost1" database="bj_db" />
	<dataNode name="dn3" dataHost="localhost1" database="zj_db" />
	<dataNode name="dn4" dataHost="localhost1" database="other_db" />
    
    <!--
    dataNode节点中各属性说明:
	    name:指定逻辑数据节点名称;
	    dataHost:指定逻辑数据节点物理主机节点名称;
	    database:指定物理主机节点上。如果一个节点上有多个库,可使用表达式db$0-99,     表示指定0-99这100个数据库;
	 -->
    

    <dataHost name="localhost1" maxCon="1000" minCon="10" balance="3" writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
        <heartbeat>select user()</heartbeat>
        <!-- 可以配置多个主从 -->
        <writeHost host="hostM1" url="192.168.162.132:3306" user="root" password="123456">
            <!-- 可以配置多个从库 -->
            <readHost host="hostS2" url="192.168.162.133:3306" user="root" password="123456" />
        </writeHost>
    </dataHost>
 <!--
 	dataHost 节点中各属性说明:
        name:物理主机节点名称;
        maxCon:指定物理主机服务最大支持1000个连接;
        minCon:指定物理主机服务最小保持10个连接;
        writeType:指定写入类型;
            0,只在writeHost节点写入;
            1,在所有节点都写入。慎重开启,多节点写入顺序为默认写入根据配置顺序,第一个挂掉切换另一个;
        dbType:指定数据库类型;
        dbDriver:指定数据库驱动;
        balance:指定物理主机服务的负载模式。
            0,不开启读写分离机制;
            1,全部的readHost与stand by writeHost参与select语句的负载均衡,简单的说,当双主双从模式(M1->S1,M2->S2,并且M1与 M2互为主备),正常情况下,M2,S1,S2都参与select语句的负载均衡;
            2,所有的readHost与writeHost都参与select语句的负载均衡,也就是说,当系统的写操作压力不大的情况下,所有主机都可以承担负载均衡;
 -->
</mycat:schema>

3. 配置server.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:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://io.mycat/">
   

   <!-- 读写都可用的用户 -->
    <user name="root" defaultAccount="true">
        <property name="password">123456</property>
        <property name="schemas">mycat_testdb</property>

        <!-- 表级 DML 权限设置 -->
        <!--        
        <privileges check="false">
            <schema name="TESTDB" dml="0110" >
                <table name="tb01" dml="0000"></table>
                <table name="tb02" dml="1111"></table>
            </schema>
        </privileges>       
         -->
    </user>

    <!-- 只读用户 -->
    <user name="user">
        <property name="password">user</property>
        <property name="schemas">mycat_testdb</property>
        <property name="readOnly">true</property>
    </user>

</mycat:server>

4. 测试

partition-hash-int.txtrule.xmlscheme.xml以及server.xml文件上传到Mycat下的config目录,重启Mycat服务器。

1.启动Mycat服务器

./mycat start

2.插入广东数据

insert into intro_tb (name,createTime) VALUES ('guangdong','2019-12-23 15:00:00');

在这里插入图片描述
3.查看数据库

可以看到广东数据库有插入了数据了
在这里插入图片描述
其它几个数据库均无数据:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
4.再次插入北京数据库

insert into intro_tb (name,createTime) VALUES ('beijing','2019-12-23 15:15:00');

在这里插入图片描述
结论和前面一样,只有北京数据库插入了数据了,其它数据库都没有插入,此处不再详述。

Mycat 其它分片规则参考:https://www.cnblogs.com/xiaoyuer666/p/7765856.html

发布了2618 篇原创文章 · 获赞 4883 · 访问量 39万+

猜你喜欢

转载自blog.csdn.net/qq_20042935/article/details/103664443