mycat实现mysql读写分离

1、mycat的下载地址 https://github.com/MyCATApache/Mycat-download
这里下载的版本是Mycat-server-1.4-release-20151019230038-win.tar.gz
解压即可

2、需要配置JDK的环境变量;然后修改conf文件夹下的wrapper.conf文件的:

# Java Application
wrapper.java.command=E:\Programs\Java\jdk1.8.0_31\bin
wrapper.working.dir=..

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://org.opencloudb/">
    <system>
    <property name="defaultSqlParser">druidparser</property>
      <!--  <property name="useCompression">1</property>--> <!--1为开启mysql压缩协议-->
    <!-- <property name="processorBufferChunk">40960</property> -->
    <!-- 
    <property name="processors">1</property> 
    <property name="processorExecutor">32</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="mutiNodeLimitType">1</property> 0:开启小数量级(默认) ;1:开启亿级数据排序
            <property name="mutiNodePatchSize">100</property> 亿级数量排序批量
            <property name="processors">32</property> <property name="processorExecutor">32</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> -->
    </system>
    <user name="root">
        <property name="password">root</property>
        <property name="schemas">qs</property>
    </user>

    <user name="user">
        <property name="password">user</property>
        <property name="schemas">qs</property>
        <property name="readOnly">true</property>
    </user>
    <!-- <cluster> <node name="cobar1"> <property name="host">127.0.0.1</property> 
        <property name="weight">1</property> </node> </cluster> -->
    <!-- <quarantine> <host name="1.2.3.4"> <property name="user">test</property> 
        </host> </quarantine> -->

</mycat:server>

注:qs必须和schema.xml的schemas对应;此处的user root 和user 指的是mycat的用户名;可以通过>mysql -uroot -proot -hlocalhost -P8066 -Dqs 登录查询。

4、修改schema.xml文件:

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

    <schema name="qs" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1">
    </schema>

    <dataNode name="dn1" dataHost="localhost1" database="db1" />


    <dataHost name="localhost1" maxCon="1000" minCon="10" balance="3"
        writeType="0" dbType="mysql" dbDriver="native" switchType="2"  slaveThreshold="100">
        <heartbeat>select user()</heartbeat>

        <writeHost host="hostM1" url="192.168.1.56:3306" user="root"
            password="root">

        <readHost host="hostS1" url="192.168.0.83:3306" user="root"
            password="root" />
        </writeHost>

    </dataHost>

</mycat:schema>

没有涉及到分库配置比较简单;database=”db1” db1是两台mysql服务器中都存在的数据库(因为两个数据库已经配置好主从复制,所以数据一样的)。
注:192.168.1.56 和 192.168.0.83 在已经配置好mysql主从复制的情况下,然后通过mycat的读写分离来实现读取数据库只在192.168.0.83,写入数据库只在192.168.1.56。

balance 属性
负载均衡类型,目前的取值有 3 种:
1. balance=”0”, 不开启读写分离机制,所有读操作都发送到当前可用的 writeHost 上。
2. balance=”1”,全部的 readHost 与 stand by writeHost 参与 select 语句的负载均衡,简单的说,当双
主双从模式(M1->S1,M2->S2,并且 M1 与 M2 互为主备),正常情况下,M2,S1,S2 都参与 select 语句的负载
均衡。
3. balance=”2”,所有读操作都随机的在 writeHost、readhost 上分发。
4. balance=”3”,所有读请求随机的分发到 wiriterHost 对应的 readhost 执行,writerHost 不负担读压
力,注意 balance=3 只在 1.4 及其以后版本有,1.3 没有。

writeType 属性
负载均衡类型,目前的取值有 3 种:
1. writeType=”0”, 所有操作发送到配置的第一个 writeHost,第一个挂了切到还生存的第二个
writeHost,重新启动后已切换后的为准,切换记录在配置文件中:dnindex.properties .
2. writeType=”1”,所有写操作都随机发送到配置的 writeHost,1.5 以后废弃不推荐。

switchType 属性
- 0 表示不自动切换
- 1 默讣值,自动切换
- 2 基于 MySQL 主从同步的状态决定是否切换

根据主从延时切换:
1.4 开始支持 MySQL 主从复制状态绑定的读写分离机制,让读更加安全可靠,配置如下:
MyCAT 心跳检查语句配置为 show slave status ,dataHost 上定义两个新属性: switchType=”2” 于
slaveThreshold=”100”,此时意味着开启 MySQL 主从复制状态绑定的读写分离于切换机制,Mycat 心跳机制
通过检测 show slave status 中的 “Seconds_Behind_Master”, “Slave_IO_Running”,
“Slave_SQL_Running” 三个字段来确定当前主从同步的状态以及 Seconds_Behind_Master 主从复制时延,
当 Seconds_Behind_Master>slaveThreshold 时,读写分离筛选器会过滤掉此 Slave 机器,防止读到很久之前
的旧数捤,而当主节点宕机后,切换逻辑会检查 Slave 上的 Seconds_Behind_Master 是否为 0,为 0 时则
表示主从同步,可以安全切换,否则不会切换。

read测试 mycat和192.168.0.83一致:

C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -uroot -proot -hlocalhost -P8066 -Dqs
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.5.8-mycat-1.4-release-20151019230038 MyCat Server (OpenCloundDB)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql> select * from test1;
+—-+——+
| id | name |
+—-+——+
| 1 | 8383 |
+—-+——+
1 row in set (0.00 sec)

C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -uroot -proot -h192.168.0.83 -P3306 -Ddb1
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 411
Server version: 5.7.13-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql> select * from test1;
+—-+——+
| id | name |
+—-+——+
| 1 | 8383 |
+—-+——+
1 row in set (0.00 sec)

测试写入(已关闭mysql主从复制功能):
C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -uroot -proot -hlocalhost -P8066 -Dqs
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.5.8-mycat-1.4-release-20151019230038 MyCat Server (OpenCloundDB)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql> insert test1 values(2,’localhost’);
Query OK, 1 row affected (0.01 sec)

mysql> select * from test1;
+—-+——+
| id | name |
+—-+——+
| 1 | 8383 |
+—-+——+
1 row in set (0.00 sec)

C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -uroot -proot -h192.168.0.83 -P3306 -Ddb1
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 412
Server version: 5.7.13-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql> select * from test1;
+—-+——+
| id | name |
+—-+——+
| 1 | 8383 |
+—-+——+
1 row in set (0.00 sec)

C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -uroot -proot -h192.168.1.56 -P3306 -Ddb1
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 784
Server version: 5.7.13-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql> select * from test1;
+—-+————-+
| id | name |
+—-+————-+
| 1 | 5656565656 |
| 2 | localhost |
+—-+————-+
2 rows in set (0.00 sec)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值