Mycat实现MySQL读写分离

Mycat介绍:

  • mycat是最近很火的一款国人发明的分布式数据库中间件,它是基于阿里的cobar的基础上进行开发的
  • 准备环境:
  • 主数据库服务器ip:192.168.11.128 操作系统版本Centos7 数据库版本5.7.31
  • 从数据库服务器ip 192.168.11.129 操作系统版本Centos7 数据库版本5.7.31
  • mycat安装服务器ip 192.168.11.130 操作系统版本Centos7
  • mysql数据库读写分离是基于mysql实现主从同步,mysql主从同步可以看之前的博客:MySQL主从同步
  • java环境1.7以上配置jdk

搭建Mycat

  • 下载Mycat压缩包,上传至服务器解压。社区地址:http://www.mycat.org.cn/

    [root@localhost sudytech]# ll
    总用量 0
    drwxr-xr-x. 7   10  143 245 7月   4 2019 jdk1.8.0_221
    drwxr-xr-x. 8 root root  99 8月   2 23:04 mycat
    [root@localhost sudytech]# pwd
    /opt/sudytech
    
  • 配置jdk和mycat环境变量

    export JAVA_HOME=/opt/sudytech/jdk1.8.0_221
    export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
    export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOME/bin:$PATH:/opt/sudytech/mycat/bin
    

配置文件配置

Mycat实现mysql读写分离,只需配置两个文件server.xmlschema.xml

server.xml 该文件主要配置通过Mycat连接时的账号密码

[root@localhost conf]# cat server.xml
        <user name="root" defaultAccount="true"> #拥有读写权限时,mycat连接的账号
                <property name="password">Sudy@12344</property>  连接的密码
                <property name="schemas">rwtest</property> 连接的逻辑库名(可随意填写,默认写成需要真实数据库名称,增强可读性)
                <property name="defaultSchema">rwtest</property>
        </user>
        #通过mycat链接只拥有读的权限的账号密码(我没用到,注释掉了,如果解注schemas值要与上面相同)
          <!--
        <user name="sudy">
                <property name="password">sudy12344</property>
                <property name="schemas">rwtest</property>
                <property name="readOnly">true</property>
                <property name="defaultSchema">rwtest</property>
        </user>
          -->

schema.xml

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

      #name的值需要和server.xml中的值对应
    <schema name="rwtest" checkSQLschema="true" sqlMaxLimit="100" dataNode="dn1"> 
    
        #需注释掉table标签,否则通过mycayt连接时创建的逻辑库中只会生成这travelrecord,address两张表,不会显示真实数据库中的表,注释掉后上面schema标签 需增加dataNode否则报错;
           <!--
            <table name="travelrecord,address" dataNode="dn1,dn2,dn3" rule="auto-sharding-long" splitTableNames ="true"/>-->
            <!-- <table name="oc_call" primaryKey="ID" dataNode="dn1$0-743" rule="latest-month-calldate"
                    /> -->
    </schema>

     #name的值和上面schema标签中dataNode相匹配 databaHost可默认,database为真实数据库名称
    <dataNode name="dn1" dataHost="localhost1" database="rwtest" />

    #name的值与dataNode中dataHost相匹配
    <dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"
                      writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
            <heartbeat>select user</heartbeat>
            <!-- can have multi write hosts -->
    #只写服务器的信息,注意这边的密码为主从数据库中对mycat所在服务器授权的密码,并不是服务器的登录密码,可配置多个writeHost
            <writeHost host="192.168.11.128" url="192.168.11.128:3306" user="root" password="Qwer@12344">
                 <!-- can have multi write hosts -->
                #读属于写,该标签配置只读服务器信息,如果写服务器宕机,读服务器也不能使用,也可配置多个读服务器标签
               <readHost host="192.168.11.129" url="192.168.11.129:3306" user="root"  password="Qwer@12344" />
            </writeHost>
            <!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
    </dataHost>

对于标签,它里面重要的参数列举如下:

1. balance=”0”, 不开启读写分离机制,所有读操作都发送到当前可用的writeHost上。 
2. balance=”1”,全部的readHost与stand bywriteHost参与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属性: 
1.writeType=”0”,所有写操作发送到配置的第一个writeHost,第一个挂了切到还生存的第二个writeHost,重新启动后以切换后的为准,切换记录在配置文件中:dnindex.properties. 
2.writeType=”1”,所有写操作都随机地发送到配置的writeHost,1.5以后废弃不推荐。

------

switchType指的是切换的模式,目前的取值也有4种: 
1. switchType=’-1’ 表示不自动切换 
2. switchType=’1’ 默认值,表示自动切换 
3. switchType=’2’ 基于MySQL主从同步的状态决定是否切换,心跳语句为 show slave status 
4. switchType=’3’基于MySQLgalary cluster的切换机制(适合集群)(1.4.1),心跳语句为 show status like ‘wsrep%’。

------

注意:估计Mycat1.4才开始支持switchType。1.3版本配置该属性的话,日志里会报错:org.xml.sax.SAXParseException;lineNumber: 61; columnNumber: 86; Attribute “switchType” must bedeclared for element type “dataHost”。 
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时则表示主仅同步,可安全切换,否则不会切换。

至此,Mycat配置文件修改完成,启动mycat

[root@localhost mycat]# /opt/sudytech/mycat/bin/mycat start

查看mycat日志:

/opt/sudytech/mycat/log/wrapper.log
STATUS | wrapper  | 2020/08/02 23:42:45 | --> Wrapper Started as Daemon
STATUS | wrapper  | 2020/08/02 23:42:45 | Launching a JVM...
INFO   | jvm 1    | 2020/08/02 23:42:46 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
INFO   | jvm 1    | 2020/08/02 23:42:46 |   Copyright 1999-2006 Tanuki Software, Inc.  All Rights Reserved.
INFO   | jvm 1    | 2020/08/02 23:42:46 |
INFO   | jvm 1    | 2020/08/02 23:42:47 | MyCAT Server startup successfully. see logs in logs/mycat.log

监听8066端口,是否存在服务

[root@localhost logs]# netstat -tpnl | grep 8066
tcp6       0      0 :::8066                 :::*                    LISTEN      20648/java

表示mycat服务成功启动,如果没有启动成功,报错也会在wrapper.log日志中。

Mycat读写分离测试

随意至主数据库或从数据库

/opt/sudytech/mysql/bin/mysql -uroot -pSudy@12344 -P8066 -h192.168.11.130

如果连接失败,请查看防火墙是否开放了8066端口

[root@localhost mysql]# /opt/sudytech/mysql/bin/mysql -u root -pSudy@12344 -P8066 -h192.168.11.130
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 1
Server version: 5.6.29-mycat-1.6.7.4-release-20200105164103 MyCat Server (OpenCloudDB)

Copyright (c) 2000, 2020, 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> 

该情况表示连接成功。

mysql> use rwtest;
Database changed
mysql> show tables;
ERROR 1184 (HY000): Invalid DataSource:0

出现该情况,请检查主从数据库是否对mycat所在服务器ip授权,并且在schema.xml配置文件中writeHost标签中的密码是否填写正确。

mysql> grant all privileges on *.* to root@'192.168.11.130' identified by 'Qwer@12344';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;

授权后重启mycat.

mysql> use rwtest;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+------------------+
| Tables_in_rwtest |
+------------------+
| employees        |
+------------------+
1 row in set (0.00 sec)

mysql> select * from employees;
+--------+------------+------------+-----------+--------+------------+
| emp_no | birth_date | first_name | last_name | gender | hire_date  |
+--------+------------+------------+-----------+--------+------------+
|  10001 | 1953-09-02 | Georgi     | Facello   | M      | 1986-06-26 |
|  10002 | 1953-09-02 | Georgi     | Facello   | M      | 1986-06-26 |
|  10003 | 1953-09-02 | Georgi     | Facello   | M      | 1986-06-26 |
|  10004 | 1953-09-02 | Georgi     | Facello   | M      | 1986-06-26 |
|  10005 | 1953-09-02 | Georgi     | Facello   | M      | 1986-06-26 |
+--------+------------+------------+-----------+--------+------------+
5 rows in set (0.06 sec)

怎么判断mycat读的是从库数据库呢?可以在从库数据库中插入一条数据,在执行查询语句时,显示该条数据就表明mycat查询时读取的是从库数据库的数据。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值