mycat的读写分离

1、什么是读写分离?

2、读写分离的配置?

3、关于schema.xml文件中的dataHost标签的重要属性


1、什么是读写分离?

  在同一个系统(项目),把对数据库的读操作和写操作进行分离到不同节点上完成,

  这种模式就称为读写分离。

2、读写分离的配置:

  server.xml:配置了mycat的用户以及schemas(相当于mysql的数据库)

  rule.xml:配置了DataNode的数量。指定了mycat的分片(dataHost)的规则

       一个dataHost就是一个分片

  schema.xml:真正的读写分离的配置

配置:首先是mysql数据的配置:在前面的随笔中有,只是在此基础上添加了部分配置

  配置主节点(mysql03为主节点):

    在my.cnf文件中添加部分信息:vim /etc/my.cnf

    在[mysqld]标签之下,在[mysqld.safe]标签之上

    配置主从复制的数据库名,要求和dataHost标签中的database属性值一样

    binlog-do-db=teach

    配置忽略MySQL数据库的主从复制,也就是说MySQL自带的mysql数据库不参与

      主从复制(此配置可以不配)

    binlog-ignore-db=mysql

    开启二进制日志(可以不配)

    log-bin=mysql-bin

    配置唯一标识符(id)(这个唯一标识符不能和其他任何mysql服务器一致,这个必须要

      保证唯一性,一般情况下使用该服务器的IP)

    server-id=131

  配置完成后重启mysql服务:service mysqld restart

  进入到mysql中,在bin目录下:./mysql -u root  -p

  向其它的从节点进行授权:

  grant file on *.* to 'root'@'%' identified by '123456';

  开启想从库复制数据:

  grant replication slave,replication client on *.* to 'root'@'%' identified by '123456';

  也是授权命令(实现了最大程度的授权,向从节点开放超管模式,包括主从复制)

  推荐:grant all on *.* to 'root'@'%' identified by '123456' with grant option;

  提交并刷新:

  flush privileges;

  查看从节点状态:

  show master status;(这一步很需要,配置从节点是需要其中的信息)

配置从节点:

  修改my.cnf文件:

  (1)在[mysqld]标签之下,在[mysqld.safe]标签之上:

  server-id=ip

  (2)重启mysql服务

  service mysqld restart

  (3)进入到mysql中

  在bin目录下:./mysql -u root -p

  (4)认主

  change master to master_host='192.168.23.131',master_port=3306,

  master_user='root',master_password='123456',master_log_file='mysql-bin.000001',

  master_log_pos=1153;

  注意:写命令时注意IP地址,

    master_host:主节点的ip地址
    master_port:主节点的端口号
    master_user:主节点的用户名
    master_password:主节点的密码
    master_log_file:主节点的日志文件--->在主节点使用show master status;查看(File)
    master_log_pos:主节点的定位信息在主节点使用show master status;查看(Position)

  (5)刷新并提交

  flush privileges;

  (6)开启主从复制

  start slave;

  (7)查看从节点的状态(在Navicat中新建连接后,在查询中输入):

    show slave status;

     字段Slave_IO_Running=YES

     字段Slave_SQL Running=YES

3、在schema.xml中的dataHost标签里有三个重要属性

!!!!!mycat也是支持负载均衡的!!!!!
    3.1.balance
        0:不使用负载均衡
        1:所有的writeHost的从节点都要参与负载(无论是读数据还是写数据),但是writeHost的主节点只写数据,不读数据
            writeHost-->有5个从节点(一般情况下,这5个写数据的从节点是不工作的,当主节点宕机了以后从节点才会开始工作)
            一旦把balance的值配置为1,说明所有的writeHost的从节点都必须要参与读和写的操作(也就是说所有的从节点和主节点平等)
        面试题:
            writeHost的主节点是否参与读数据的操作?
                不参与
            mycat为什么这样设计?
                必须要知道什么时候会配置1-->因为访问量过大导致主节点压力过大
                当峰值访问量结束后-->需要回归最初的状态-->一台主节点和5台从节点
                如果说在峰值访问的数据量非常大--->直接导致了主节点宕机-->但是从节点完好-->峰值数据过去后-->主节点处于宕机状态-->从节点无法找到主节点-->还是从节点(不工作的)
2:所有的主节点和从节点都要参与负载(无论是writeHost的主节点还是从节点都必须实现读和写的操作) 3:所有的readHost无论是主节点还是从节点都必须参与负载,但是writeHost并不参与读的操作 readHost如果也需要配置主从(1主3从) 主节点工作,但是从节点一般情况下是不工作的,只有当主节点宕机的时候,从节点才开始工作 所有readHost的主节点和从节点都开始工作(也就是说所有的节点都是平等的),但是writeHost并不参与读的操作,也不参与负载! 3.2.writeType(写入的方式:一定要和主节点和从节点有关) 0:自动主从(在1.4以后支持了主从关系,但是并没有支持主从复制) 也就是mycat会自动分配主从,如果说在同一个dataHost标签中配置了多个writeHost,则mycat会自动分配主从 1:所有写操作都随机的发送到配置的 writeHost(1.5版本之前生效) 1:不进行主从(1.5版本之后生效),随机 !!!!两个writeHost,mycat会随机只向一个writeHost中写数据,就算该writeHost宕机,也不会向另一个writeHost写入数据(???为什么这么设置???) !!并不是标准答案,只是我的个人理解: 那个从来不写数据的writeHost是后备节点,一般情况下和readHost配置的一样,如果writeHost宕机,单独一台readHost是不能存在的,需要后备节点的支持!!!! 2:所有的writeHost节点不再执行写的操作,所有的writeHost都停止写操作,只供查询的操作(查询压力比较大的时候才会使用到) 3.3.switchType(切换的方式) 无论是-1还是1都根据的是mycat所自带的主从关系 -1:不自动切换 writeHost配置的规则为一台主节点一台从节点 如果主节点宕机,从节点还是从节点,并不会自动切换为主节点 1:自动切换 根据的是mysql所配置主从关系 2:根据mysql配置主从进行切换(监视mysql的主从心跳) Slave_IO_Runing(YES) Slave_SQL_Running(YES) 3:和2一样,只是2为单节点,3为集群模式

4、三个配置文件的配置:

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/">
    <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>
    
    <!-- 全局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 name="root" defaultAccount="true">
        <property name="password">root</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 name="mod-long">
        <rule>
            <columns>id</columns>
            <algorithm>mod-long</algorithm>
        </rule>
    </tableRule>
    
    <function name="mod-long" class="io.mycat.route.function.PartitionByMod">
        <!-- how many data nodes -->
        <property name="count">1</property>
    </function>

</mycat:rule>

schema.xml

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

    <!--
        读写分离
            把写数据的操作在mysql04上实现(mysql04没有数据)
            把读数据的操作在mysql03上实现(mysql03有数据)
            也就是说整个读写分离的核心就是实现了mysql03和mysql04的数据同步
            数据同步的核心就是实现mysql03和mysql04的主从复制
            在之前讲的数据同步:
                当实现增删改的时候需要重新把mysql全表扫描然后存入redis中
            主从:
          当只有读写分离的时候(假如两台服务器)
          主库用来写数据,从库用来读数据,主库向从库中同步数据;
          但当加入分库分表时,假如从库不够了,需要多添加几台从库
          此时,由于分库分表,从库就变成了写数据,主库用来读数据 。
          然后从库就需要向主库同步数据。
两台数据库的关系 mysql03是主库(只写数据) mysql04是从库(只读数据) 每一个readHost不能单独存在,必须要配置在writeHost里面
--> <schema name="TEACH" checkSQLschema="false" sqlMaxLimit="100"> <table name="mycat_teach" dataNode="dn1" rule="mod-long" /> </schema> <!-- database:在读写分离的配置中非常重要 因为读写分离需要实现主从复制(在mysql的主从复制中必须非常准确的定位这个database的名字) --> <dataNode name="dn1" dataHost="localhost1" database="teach" /> <dataHost name="localhost1" maxCon="1000" minCon="10" balance="3" writeType="0" dbType="mysql" dbDriver="native" switchType="2" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <!-- can have multi write hosts --> <!-- 实现读写分离的配置: mysql03作为写的数据库 mysql04作为读的数据库 --> <writeHost host="hostM1" url="192.168.23.131:3306" user="root" password="123456"> <readHost host="hostS1" url="192.168.23.132:3306" user="root" password="123456" /> </writeHost> <writeHost host="hostM1" url="192.168.23.132:3306" user="root" password="123456" /> </dataHost> </mycat:schema>

我们在配置完成后使用Navicat连接服务器时,

需要先在mysql03和mysql04中把配置中的数据库和数据表新建出来,

再去对mycat02创建连接。

乾坤未定,你我皆是黑马
原文地址:https://www.cnblogs.com/liuqijia/p/11573465.html