zoukankan      html  css  js  c++  java
  • mycat的分库分表

    1、什么是分库分表

    2、分库分表的大致分配

    3、Linux下配置mycat服务器


    1、什么是分库分表?

      在同一个系统(项目)中,把一张数据比较多的表拆分到不同的集群节点上,

      这种模式叫做分库分表

    2、分库分表的实现:

      使用mycat实现分库分表(HDFS:namenode datanode secondarynamdenode)

      在整个mycat中也会用到namenode和datanode

      

      (1)先配置一台mycat服务器(192.168.23.164)

      (2)然后配置两台mysql服务器:mysql01(192.168.23.129)和mysql02(192.168.23.130)

        在mysql01服务器下有一个teach数据库,在teach下有一个student表

        在mysql02服务器下有一个teach数据库,在teach下有一个student表

      当使用mycat把进行存储是,数据会自动往mysql01或mysql02中存储.

    3、配置mycat

      3.1 规定Linux的用户名和全名不能叫mycat,否则mycat不会生效(原因是影响了Linux系统的环境变量)

      3.2 vim /etc/sysconfig/network也不能叫mycat

      3.3 mycat使用的是Java语言编写-->所以必须要在Linux上配置jdk

         (要求jdk是64位,因为mycat是在64位的jdk8 上进行编译的)

      3.4 配置jdk

      3.5 使用xftp把mycat的压缩包上传到Linux服务器上(/home/apps/)

      3.6 解压mycat压缩包

        tar -zxvf xxxx

      3.7 配置mycat的环境变量(与jdk的环境变量配置一样)

      3.8 启动mycat(两种方式)

        (1)进入mycat的bin目录: ./mycat console(推荐使用)

        或者在其他的地方的目录:mycat console

        (2)./mycat start

        一旦启动mycat就关不掉,kill -9 进程号  也关不掉,除非重启或者关机,

        据说可以使用Ctrl+C

      3.9 下面就是三个核心配置文件了(最重要的地方):

        所有的读写分离/分库分表都必须要在这三个配置文件完成

        在mycat的conf目录下:

          server.xml:配置了mycat的用户

          rule.xml:mycat的规则

          schema.xml:实现了读写分离和分库分表的具体配置

     server:

    <?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/">
    <system>
    <property name="nonePasswordLogin">0</property> <!-- 0为需要密码登陆、1为不需要密码登陆 ,默认为0,设置为1则需要指定默认账户-->
    <property name="useHandshakeV10">1</property>
    <property name="useSqlStat">0</property> <!-- 1为开启实时统计、0为关闭 -->
    <property name="useGlobleTableCheck">0</property> <!-- 1为开启全加班一致性检测、0为关闭 -->

    <property name="sequnceHandlerType">2</property>
    <!--<property name="sequnceHandlerPattern">(?:(s*nexts+values+fors*MYCATSEQ_(w+))(,|)|s)*)+</property>-->
    <!--必须带有MYCATSEQ_或者 mycatseq_进入序列匹配流程 注意MYCATSEQ_有空格的情况-->
    <property name="sequnceHandlerPattern">(?:(s*nexts+values+fors*MYCATSEQ_(w+))(,|)|s)*)+</property>
    <property name="subqueryRelationshipCheck">false</property> <!-- 子查询中存在关联查询的情况下,检查关联字段中是否有分片字段 .默认 false -->
    <!-- <property name="useCompression">1</property>--> <!--1为开启mysql压缩协议-->
    <!-- <property name="fakeMySQLVersion">5.6.20</property>--> <!--设置模拟的MySQL版本号-->
    <!-- <property name="processorBufferChunk">40960</property> -->
    <!--
    <property name="processors">1</property>
    <property name="processorExecutor">32</property>
    -->
    <!--默认为type 0: DirectByteBufferPool | type 1 ByteBufferArena | type 2 NettyBufferPool -->
    <property name="processorBufferPoolType">0</property>
    <!--默认是65535 64K 用于sql解析时最大文本长度 -->
    <!--<property name="maxStringLiteralLength">65535</property>-->
    <!--<property name="sequnceHandlerType">0</property>-->
    <!--<property name="backSocketNoDelay">1</property>-->
    <!--<property name="frontSocketNoDelay">1</property>-->
    <!--<property name="processorExecutor">16</property>-->
    <!--
    <property name="serverPort">8066</property> <property name="managerPort">9066</property>
    <property name="idleTimeout">300000</property> <property name="bindIp">0.0.0.0</property>
    <property name="frontWriteQueueSize">4096</property> <property name="processors">32</property> -->
    <!--分布式事务开关,0为不过滤分布式事务,1为过滤分布式事务(如果分布式事务内只涉及全局表,则不过滤),2为不过滤分布式事务,但是记录分布式事务日志-->
    <property name="handleDistributedTransactions">0</property>

    <!--
    off heap for merge/order/group/limit 1开启 0关闭
    -->
    <property name="useOffHeapForMerge">0</property>

    <!--
    单位为m
    -->
    <property name="memoryPageSize">64k</property>

    <!--
    单位为k
    -->
    <property name="spillsFileBufferSize">1k</property>

    <property name="useStreamOutput">0</property>

    <!--
    单位为m
    -->
    <property name="systemReserveMemorySize">384m</property>


    <!--是否采用zookeeper协调切换 -->
    <property name="useZKSwitch">false</property>

    <!-- XA Recovery Log日志路径 -->
    <!--<property name="XARecoveryLogBaseDir">./</property>-->

    <!-- XA Recovery Log日志名称 -->
    <!--<property name="XARecoveryLogBaseName">tmlog</property>-->
    <!--如果为 true的话 严格遵守隔离级别,不会在仅仅只有select语句的时候在事务中切换连接-->
    <property name="strictTxIsolation">false</property>

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

    </system>

    <!--

    从system标签开始到结束中的内容配置了
    连接 最大连接数 最小连接数 连接超时时间 隔离机制 事务切换....
    相当于在mybatis中配置数据库的最大最小连接数 连接超时时间
    -->

    <!-- 全局SQL防火墙设置 -->
    <!--白名单可以使用通配符%或着*-->
    <!--例如<host host="127.0.0.*" user="root"/>-->
    <!--例如<host host="127.0.*" user="root"/>-->
    <!--例如<host host="127.*" user="root"/>-->
    <!--例如<host host="1*7.*" user="root"/>-->
    <!--这些配置情况下对于127.0.0.1都能以root账户登录-->
    <!--
    <firewall>
    <whitehost>
    <host host="1*7.0.0.*" user="root"/>
    </whitehost>
    <blacklist check="false">
    </blacklist>
    </firewall>
    -->

    <!--
    整个user标签中就是配置了mycat的账号和密码
    name===》就是mycat的账号
    defaultAccount====》表示该账号为管理员账号,而且只能出现一次
    property:属性标签
    password===》就是name的密码
    无论是账号还是密码都可以随意定制,开心就好!!!
    readOnly:只读(该账号只能查询数据,不能写入数据)
    schemas:指向的是mycat的数据库(mycat的虚拟库!!!这个库并不是真实存在的,是需要通过mysql的数据库进行映射的),一个mycat中可以有多个schema
    隐藏mysql的真实库的名字
    连接mysql:
    jdbc:mysql:localhost:3306/数据库名
    连接mycat:
    jdbc:mysql:ip地址:端口号/mycat的虚拟库名
    schemas中的配置和mysql的数据库可以一样,也可以不一样
    -->
    <user name="root" defaultAccount="true">
    <property name="password">123456</property>
    <property name="schemas">TEACH</property>
    </user>
    </mycat:server>

    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对应一个function,如果需要进行配置必须要配置一对,不能单独配置一个(如果单独配置了一个的情况下,mycat启动的时候会直接报错)
    一共有十种规则:
    homework1:
    百度把这10种规则全部弄明白,然后整理一份用自己语言描述的电子文档

    mod-long:(轮询)
    mysql01
    mysql数据库
    teach
    student
    mysql02
    mysql数据库
    teach
    student

    当客户端发送请求===插入数据===》mycat===请求mysql===》会把第一条数据插入进mysql01,
    会把第二条数据插入进mysql02
    依次循环

    -->

    <tableRule name="mod-long">
    <rule>
    <columns>id</columns>
    <algorithm>mod-long</algorithm>
    </rule>
    </tableRule>


    <function name="mod-long" class="io.mycat.route.function.PartitionByMod">
    <!--
    代表了datanode的节点数量
    如果只有两台datanode就必须要写2
    -->
    <property name="count">2</property>
    </function>

    </mycat:rule>

    schema.xml

    <?xml version="1.0"?>
    <!DOCTYPE mycat:schema SYSTEM "schema.dtd">
    <mycat:schema xmlns:mycat="http://io.mycat/">

    <!--
    !!!schema标签的name属性值必须要和server.xml中的schames保持一致!!!
    schema就类似于mysql的数据库
    checkSQLschema:检查SQL语句是否错误(select * from user wher id = 1)
    默认值为false,不需要改为true,把对SQL语句的检查交给mysql处理
    sqlMaxLimit:最大执行SQL语句的数量
    一次性可以执行多少条SQL语句

    在schame标签中有table标签,这个标签就是定义了mycat中的表
    类似于mysql数据库中的表
    和mysql中的表名可以重复也可以不一致

    dataNode:数据节点
    在HDFS中存放数据
    在mycat中映射的就是真实mysql数据库(可以有多个,也可以只有一个),名字可以随意起
    今天的目标是实现分库分表,需要用到两台mysql数据库,所以只需要配置dn1和dn2

    rule:配置规则
    配置一定要和rule.xml中保持一致

    -->
    <schema name="TEACH" checkSQLschema="false" sqlMaxLimit="100">
    <!-- auto sharding by id (long) -->
    <table name="mycat_student" dataNode="dn1,dn2" rule="mod-long" />
    </schema>

    <!--
    datanode标签对应的是table标签中的datanode属性
    也就是说datanode属性值必须要和dataNode的标签name属性值保值一致
    dn1:代表了mysql01服务器==》mysql数据库
    dn2:代表了mysql02服务器==》mysql数据库
    dataHost:才是真正的配置了分库分表和读写分离的核心配置
    database:映射的是真实mysql的数据库名
    -->
    <dataNode name="dn1" dataHost="localhost1" database="mysql01" />
    <dataNode name="dn2" dataHost="localhost2" database="mysql02" />

    <!--
    dataHost标签必须要和dataNode标签中的dataHost属性保持一致
    如果dataHost属性名重复了,映射的是同一个dataHost
    每一个dataHost标签就是一个真实的mysql数据库
    maxCon:最大连接数
    minCon:最小连接数
    homework2:
    把balance,writeType,switchType弄明白,并且用自己的语言描述每个值代表什么意思,记录电子文档
    balance:负载均衡
    writeType:写入的类型(读写分离使用)
    switchType:转换的类型(读写分离所使用的数据)
    dbType:数据库的类型(在最早的版本mycat只支持mysql一种数据库,后来发展支持很多种数据库)
    dbDriver:数据库驱动(使用本地驱动,使用的是mycat所提供的驱动)
    slaveThreshold:主从复制时延
    master:主节点
    slave:从节点
    threshold:入口
    -->
    <dataHost name="localhost1" maxCon="1000" minCon="10" balance="0"
    writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
    <!--
    heartbeat:心跳
    mycat一直在监视mysql的user表(心跳),如果一旦发现mysql的user不再有心跳,就会认为这一台mysql处于宕机状态,也就不会再向这一台mysql进行做增删改查的操作
    -->
    <heartbeat>select user()</heartbeat>
    <!--
    writeHost:标签是该台mysql数据库为读和写的数据库
    readHost:标签是该台mysql数据库为读的数据库
    writeHost如果单独存在,标识该台mysql数据库为读和写的操作
    如果一旦和readHost一起使用,writeHost就只能做些的操作,所有的读操作都使用readHost完成
    readHost必须要配置在writeHost的内容,不允许单独存在
    host:属性只是标识了该台数据库的操作的内容,可以随意定义
    url:数据库的url
    jdbc:mysql://localhost:3306
    !!!!!如果配置了两个writeHost,那么host的值就必须不能重复!!!!!
    -->
    <writeHost host="write01" url="192.168.23.129:3306" user="root"
    password="root" />
    </dataHost>

    <dataHost name="localhost2" maxCon="1000" minCon="10" balance="0"
    writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
    <heartbeat>select user()</heartbeat>
    <writeHost host="write02" url="192.168.23.130:3306" user="root"
    password="root" />
    </dataHost>
    <!--

    mycat中所映射的真实mysql的表必须要mycat所声明的table的名字保持一致
    也就是说mycat的表名必须要和mysql的表名一模一样,否则映射不到

    -->
    </mycat:schema>

    配置完成:

    进入到Navicat中,先新建连接,分别连接mysql01和mysql02,然后分别在mysql01和mysql02

      中分别建立两个数据库(库名可以不一致),再分别建立mycat_student表,

      然后建立mycat01的连接,(在建立连接时,端口号为8066,即通讯端口号,不能使用9066,为管理端口号)

    在mycat01中会有一个TEACH数据库,在库中有一个mycat_student表,

      这是在配置文件中的配置生效了。

    至此,配置结束。使用Java代码进行实现:只需要修改数据源就行了(只用连接mycat01数据库)

    当进行存储的时候,由于我们使用的是轮询规则,数据会自动轮流存储到mysql01和mysql02.

    乾坤未定,你我皆是黑马
  • 相关阅读:
    七层网络模型
    nginx配置本地https
    kong结合consul
    kong添加upstream
    1.创建spring cloud父工程和子模块
    idea自动生成spring实体
    php的json_encode()之后float类型丢失精度
    windows下wnmp配置
    Call to undefined function imageftbbox()
    介绍一款比较好用的画图工具--PlantUml
  • 原文地址:https://www.cnblogs.com/liuqijia/p/11573056.html
Copyright © 2011-2022 走看看