zoukankan      html  css  js  c++  java
  • MyCat分片集群

    数据库集群会产生的问题:

       自增ID问题

       数据关联查询问题(水平拆分)

       数据同步问题

    数据库集群 自动增长id产生重复的话,解决: UUID形式  (没有排序 不是自增) 设置数据库步长

     其他方案: redis  或者雪花算法

    数据库分库分表的策略:

      

    数据库分表分库策略

    数据库分表分库原则遵循 垂直拆分与水平拆分
    垂直拆分就是根据不同的业务,分为不同的数据库,比如会员数据库、订单数据库、支付数据库等,垂直拆分在大型电商系统中用的非常常见。
    优点:
    拆分后业务清晰,拆分规则明确,系统之间整合或扩展容易。
    缺点:
    部分业务表无法join,只能通过接口方式解决,提高了系统复杂度存在分布式事务问题。

    数据库分表分库原则遵循 垂直拆分与水平拆分

    垂直拆分是把不同的表拆到不同的数据库中,而水平拆分是把同一个表拆到不同的数据库中。
       相对于垂直拆分,水平拆分不是将表的数据做分类,而是按照某个字段的某种规则来分散到多个库之中,每个表中包含一部分数据。简单来说,我们可以将数据的水平切分理解为是按照数据行的切分,就是将表中 的某些行切分到一个数据库,而另外的某些行又切分到其他的数据库中,主要有分表,分库两种模式
    该方式提高了系统的稳定性跟负载能力,但是跨库join性能较差。

     在数据库分库分表原则中,遵循两个设计理论 垂直拆分 水平拆分

     垂直拆分就是根据不同的业务,拆分成不同的数据库,比如会员数据库,订单数据库,支付数据库,消息数据库等。

     垂直拆分缺点:

        跨数据查询 必须采用接口形式通讯、分布式事务问题 

       

     垂直拆分把不容的表拆到不同的数据库中,而水平拆分是把同一个表拆分到不同的数据库中,或者把一张表的数据拆分n多个小表

      

    如果一张表6条数据:

      变成三个库 每个库中存放两条数据,一共三张表,三张表的结构是完全相同的。三个库进行均摊存放

      

       在存的时候根据ID取模存放

       在查询时候依然根据取模算法进行获取

       拆分的好处: 如果数据量大的情况下,就算用索引也就那样。但是进行水平拆分就好了很多

     

       水平分片策略:

         

    MyCat支持10种分片策略
    1、求模算法
    2、分片枚举
    3、范围约定
    4、日期指定
    5、固定分片hash算法
    6、通配取模
    7、ASCII码求模通配
    8、编程指定
    9、字符串拆分hash解析
    详细:http://www.mycat.io/document/mycat-definitive-guide.pdf

      关于分片枚举:

    分片枚举这种规则适用于特定的场景,比如有些业务需要按照省份或区县来做保存,而全国的省份区县固定的,这类业务使用这一规则。配置如下
    1.案例步骤:
    创建数据库userdb_1 、 userdb_2、userdb_3
    2.修改partition-hash-int.txt 规则
    wuhan=0
    shanghai=1
    suzhou=2

    详细配置请参考文档

    根据地区进行分库 湖北数据库、江苏数据库 山东数据库  (三张表)

       

    分片枚举算法就是根据不同的枚举(常量),进行分类存储。

    可以使用分片枚举实现根据地区分类存储到不同数据库进行存放

    环境搭建:

     定义枚举(地区)  每个地区指定数据库存放位置

    schema.xml:

    <?xml version="1.0"?>
    <!DOCTYPE mycat:schema SYSTEM "schema.dtd">
    <mycat:schema xmlns:mycat="http://io.mycat/">
        <!-- mycat_testdb 是mycat的逻辑库名称,链接需要用的 -->
        <schema name="mycat_testdb" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1">
         <!-- 逻辑库里面的表 order_info   并且有三台节点做集群  规则文件是role2  -->
          <table name="order_info"  dataNode="dn1,dn2,dn3" rule="role2" /> 
        
        </schema>
            <!-- database 是MySQL数据库的库名  三台节点  分别对应真实的数据库 user_dbn  -->
        <dataNode name="dn1" dataHost="localhost1" database="user_db1" />
        <dataNode name="dn2" dataHost="localhost1" database="user_db2" />
        <dataNode name="dn3" dataHost="localhost1" database="user_db3" />
        <!--
        dataNode节点中各属性说明:
        name:指定逻辑数据节点名称;
        dataHost:指定逻辑数据节点物理主机节点名称;
        database:指定物理主机节点上。如果一个节点上有多个库,可使用表达式db$0-99,     表示指定0-99这100个数据库;
    
        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语句的负载均衡,也就是说,当系统的写操作压力不大的情况下,所有主机都可以承担负载均衡;
    -->
        <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.91.8:3306" user="root" password="root">
                <!-- 可以配置多个从库 -->
                <readHost host="hostS2" url="192.168.91.9:3306" user="root" password="root" />
            </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="role2">
                 <rule>
                      < !-- 表示根据name字段进行分片存储的 -->
                      <columns>name</columns>
                    <algorithm>hash-int</algorithm>
                    </rule>
        </tableRule>
        <!-- 与上面的hash-int对应 --> 
        <function name="hash-int" class="io.mycat.route.function.PartitionByFileMap">
            <!-- 指定枚举文件 -->
            <property name="mapFile">partition-hash-int.txt</property>
             <!-- 枚举文件非数值类型要写1 -->
            <property name="type">1</property>
             <!-- 默认存放的位置 在第一个的位置存放 -->
            <property name="defaultNode">1</property>
        </function>
        
    </mycat:rule>

    注意在实际应用时候 把rule.xml的注释去除掉

    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>

    查询端口号被哪个进程占用:

     netstat -tunlp | grep 8080

    kill -9  666

    分片规则:

    wuhan=0
    shanghai=1
    suzhou=2

    启动mycat

    然后navicat工具连接之:

    往mycat表的虚拟表里面写数据:会映射到实际物理数据库的表里面

    映射的物理数据库:

     如果分片枚举没有的 根据配置会存储到 db2中!

    结构图:

    求模算法:根据ID去进行十进制求模运算,运算结果为分区索引

     注意:数据库节点分片数量无法更改 (和ES集群非常类似)

     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">
           <!-- 分片的算法是role1  -->
        <table name="user_info" dataNode="dn1,dn2,dn3" rule="role1"/>
        
        </schema>
            <!-- database 是MySQL数据库的库名 -->
        <dataNode name="dn1" dataHost="localhost1" database="user_db1" />
        <dataNode name="dn2" dataHost="localhost1" database="user_db2" />
        <dataNode name="dn3" dataHost="localhost1" database="user_db3" />
        <!--
        dataNode节点中各属性说明:
        name:指定逻辑数据节点名称;
        dataHost:指定逻辑数据节点物理主机节点名称;
        database:指定物理主机节点上。如果一个节点上有多个库,可使用表达式db$0-99,     表示指定0-99这100个数据库;
    
        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语句的负载均衡,也就是说,当系统的写操作压力不大的情况下,所有主机都可以承担负载均衡;
    -->
        <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.91.8:3306" user="root" password="root">
                <!-- 可以配置多个从库 -->
                <readHost host="hostS2" url="192.168.91.9:3306" user="root" password="root" />
            </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="role1">
            <rule>
                 <!-- 指定分片规则要玩儿的列名 -->
                <columns>id</columns>
                <!-- 指定下面的分片算法 -->
                <algorithm>mod-long</algorithm>
            </rule>
        </tableRule>
     
        <function name="mod-long" class="io.mycat.route.function.PartitionByMod">
            <!--指定分片数量,不可以被更改 count数据库的分配数量一共三台 一旦定了就不能修改了! -->
            <property name="count">3</property>
    </function>
    
        
    </mycat:rule>

       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>

    mycat连接到读的虚拟数据库

    然后在真实物理数据库上面创建 user_db1 user_db2 user_db3

    在Myca的写权限的虚拟数据库创建表:

    此时的其他的物理数据库里面也会刷新数同样的表

     然后在write里面写入数据

    会根据ID取模,分片到不同的物理数据库里面

  • 相关阅读:
    java:线上问题排查常用手段
    去fastjson笔记
    如何让java中的注释代码执行?
    spring中aop不生效的几种解决办法
    mysql事务隔离级别/脏读/不可重复读/幻读详解
    业务系统-全球化多时区的解决思路
    mysql虚拟列(Generated Columns)及JSON字段类型的使用
    freeswitch笔记(7)-放音控制
    JVM问题典型案例定位学习
    freeswitch笔记(6)-会议功能简介
  • 原文地址:https://www.cnblogs.com/toov5/p/10332306.html
Copyright © 2011-2022 走看看