MyCAT实现MySQL读写分离

用户连接到MySQL的中间件(代理),中间件接收用户的访问转发给后端的mysql数据库。

是MySQL的一个中间件软件,Mycat是一个开源的分布式数据库系统,是一个实现了MySQL协议的服务器。
用户通过mysql客户端工具访问到Mycat,Mycat将这些请求转发给后端的MySQL服务器。

Mycat它拦截了用户发送过来的SQL语句,首先对SQL语句做了一些特定的分析:判断用户是读还是写,然后把请求发给后端对应的MySQL数据库,并将MySQL返回的数据信息经过处理后返回给用户。

[root@CentOS8 ~]# yum install java -y

[root@CentOS8 ~]# wget http://dl.mycat.org.cn/1.6.7.4/Mycat-server-1.6.7.4-release/Mycat-server-
1.6.7.4-release-20200105164103-linux.tar.gz

[root@CentOS8 ~]# mkdir /apps

[root@CentOS8 ~]# tar xvf Mycat-server-1.6.7.4-release-20200105164103-linux.tar.gz  -C /apps
[root@CentOS8 mycat]# tree -L 1
.
├── bin
├── catlet
├── conf
├── lib
├── logs
└── version.txt

bin mycat命令,启动、重启、停止等

catlet catlet为Mycat的一个扩展功能

conf Mycat 配置信息,重点关注

lib Mycat引用的jar包,Mycat是java开发的

logs 日志文件,包括Mycat启动的日志和运行的日志

version.txt mycat版本说明

logs目录:
wrapper.log mycat启动日志

mycat.log mycat详细工作日志
[root@CentOS8 conf]# tree -L 1
.
......

├── rule.xml
├── schema.xml
......

├── server.xml
.......

存放Mycat软件本身相关的配置文件,比如:连接Mycat的用户,密码,数据库名称等


    0
    0
    1
    1
    0
    0
        300
        1

        (?:(\s*next\s+value\s+for\s*MYCATSEQ_(\w+))(,|\)|\s)*)+
    false
    io.mycat.route.sequence.handler.HttpIncrSequenceHandler

        0

        0

        0

        64k

        1k

        0

        384m

        false

        false

        true

        0

        123456
        TESTDB
        TESTDB

        user
        TESTDB
        true
        TESTDB

参数 说明 user 用户配置节点 name 客户端登录MyCAT的用户名,也就是客户端用来连接Mycat的用户名。 password 客户端登录MyCAT的密码 schemas 数据库名,这里会和schema.xml中的配置关联,多个用逗号分开,例如:db1,db2 privileges 配置用户针对表的增删改查的权限 readOnly mycat逻辑库所具有的权限。true为只读,false为读写都有,默认为false

  • server.xml文件里登录mycat的用户名和密码可以任意定义,这个账号和密码是为客户机登录mycat时使用的账号信息
  • 逻辑库名(如上面的TESTDB,也就是登录mycat后显示的库名,切换这个库之后,显示的就是代理的真实mysql数据库的表)要在schema.xml里面也定义,否则会导致mycat服务启动失败!
  • 这里只定义了一个标签,所以把多余的都注释了。如果定义多个标签,即设置多个连接mycat的用户名和密码,那么就需要在schema.xml文件中定义多个对应的库!

        select user()

参数 说明 schema 数据库设置,此数据库为逻辑数据库,name与server.xml中schema对应 dataNode 分片信息,也就是分库相关配置 dataHost 物理数据库,真正存储数据的数据库

schema:

属性 说明 name 逻辑数据库名,与server.xml中的schema对应 checkSQLschema 数据库前缀相关设置,这里为false sqlMaxLimit select 时默认的limit,避免查询全表

table

属性 说明 name 表名, 物理数据库中表名 dataNode 表存储到哪些节点,多个节点用逗号分隔。节点为下文dataNode设置的name primaryKey 主键字段名,自动生成主键时需要设置 autoIncrement 是否自增 rule 分片规则名,具体规则下文rule详细介绍

dataNode

属性 说明 name 节点名,与table中dataNode对应 datahost 物理数据库名,与datahost中name对应 database 物理数据库中数据库名

dataHost

属性 说明 name 物理数据库名,与dataNode中dataHost对应 balance 均衡负载的方式 writeType 写入方式 dbType 数据库类型 heartbeat 心跳检测语句,注意语句结尾的分号要加

schema.xml文件中有三点需要注意:balance=”1″,writeType=”0″ ,switchType=”1″

schema.xml中的balance的取值决定了负载均衡对非事务内的读操作的处理。

balance 属性的取值范围:

  • 0:不开启读写分离模式
  • 1.表示开启读写分离模式
  • 2:所有读操作都随机的在writeHost、 readhost上分发
  • 3:所有读请求随机的分发到wiriterHost对应的readhost执行(只在 1.4 及其以后版本 有,1.3没有)

这两个标签都指定后端数据库的相关配置给mycat,用于实例化后端连接池。

唯一不同的是:writeHost指定写实例、readHost指定读实例,组着这些读写实例来满足系统的要求。

注意:
Mycat主从分离只是在读的时候做了处理,写入数据的时候,只会写入到writehost,需要通过mycat的
主从复制将数据复制到readhost

实验环境:CentOS8.3
Mycat服务器:10.0.0.10 — 2G内存以上,内存太小跑不动java
mysql:master:10.0.0.13 — MySQL8.0
MySQL:slave:10.0.0.14 — mysql8.0

实验准备:
关闭selinux、防火墙
设置时间同步
创建mysql主从数据库

安装mycat:

[root@CentOS8 ~]# yum install java -y

#下载并安装
[root@centos8 ~]#wgethttp://dl.mycat.org.cn/1.6.7.6/20210303094759/Mycat-server-
1.6.7.6-release-20210303094759-linux.tar.gz

[root@centos8 ~]#mkdir /apps
[root@centos8 ~]#tar xvf Mycat-server-1.6.7.6-release-20210303094759-linux.tar.gz -C /apps/  #-C:解压到对应的目录

配置环境变量:

[root@CentOS8 bin]# pwd
/apps/mycat/bin

echo 'PATH=/apps/mycat/bin:$PATH' > /etc/profile.d/mycat.sh

source /etc/profile.d/mycat.sh

安装好Mycat后启动mycat:
启动方法:

[root@CentOS8 bin]# mycat
Usage: /apps/mycat/bin/mycat { console | start | stop | restart | status | dump }

[root@CentOS8 bin]# mycat start
Starting Mycat-server...

#启动以后查看mycat监听的端口:
[root@CentOS8 bin]# ss -ntlup
Netid     State      Recv-Q     Send-Q          Local Address:Port           Peer Address:Port
udp       UNCONN     0          0              0.0.0.0%virbr0:67                  0.0.0.0:*         users:(("dnsmasq",pid=1266,fd=3))
udp       UNCONN     0          0                     0.0.0.0:111                 0.0.0.0:*         users:(("rpcbind",pid=743,fd=5),("systemd",pid=1,fd=78))
udp       UNCONN     0          0                        [::]:111                    [::]:*         users:(("rpcbind",pid=743,fd=7),("systemd",pid=1,fd=80))
tcp       LISTEN     0          128                   0.0.0.0:111                 0.0.0.0:*         users:(("rpcbind",pid=743,fd=4),("systemd",pid=1,fd=77))
tcp       LISTEN     0          128                   0.0.0.0:22                  0.0.0.0:*         users:(("sshd",pid=827,fd=4))
tcp       LISTEN     0          1                   127.0.0.1:32000               0.0.0.0:*         users:(("java",pid=89594,fd=4))
tcp       LISTEN     0          128                      [::]:111                    [::]:*         users:(("rpcbind",pid=743,fd=6),("systemd",pid=1,fd=79))
tcp       LISTEN     0          128                      [::]:22                     [::]:*         users:(("sshd",pid=827,fd=6))
tcp       LISTEN     0          50                          *:35287                     *:*         users:(("java",pid=89594,fd=66))
tcp       LISTEN     0          50                          *:39839                     *:*         users:(("java",pid=89594,fd=68))
tcp       LISTEN     0          50                          *:1984                      *:*         users:(("java",pid=89594,fd=67))
tcp       LISTEN     0          100                         *:8066                      *:*         users:(("java",pid=89594,fd=91))
tcp       LISTEN     0          70                          *:33060                     *:*         users:(("mysqld",pid=87104,fd=32))
tcp       LISTEN     0          100                         *:9066                      *:*         users:(("java",pid=89594,fd=87))
tcp       LISTEN     0          128                         *:3306                      *:*         users:(("mysqld",pid=87104,fd=34))
#8066端口用于连接MyCAT

查看mycat的日志,确认是否启动成功

[root@CentOS8 bin]# tail /apps/mycat/logs/wrapper.log
STATUS | wrapper  | 2022/09/27 00:29:37 | --> Wrapper Started as Daemon
STATUS | wrapper  | 2022/09/27 00:29:38 | Launching a JVM...

INFO   | jvm 1    | 2022/09/27 00:29:40 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
INFO   | jvm 1    | 2022/09/27 00:29:40 |   Copyright 1999-2006 Tanuki Software, Inc.  All Rights Reserved.

INFO   | jvm 1    | 2022/09/27 00:29:40 |
INFO   | jvm 1    | 2022/09/27 00:29:45 | MyCAT Server startup successfully. see logs in logs/mycat.log

在客户端使用默认密码123456来连接mycat

root@ubuntu1804-1:~# mysql -uroot -p123456 -h 10.0.0.10 -P8066
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 4
Server version: 5.6.29-mycat-1.6.7.4-release-20200105164103 MyCat Server (OpenCloudDB)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> show databases;
+----------+
| DATABASE |
+----------+
| TESTDB   |
+----------+
1 row in set (0.00 sec)

mysql> exit
Bye

在mycat 服务器上修改server.xml文件配置Mycat的连接信息
例如:指定连接名、密码和用户连接上来能看到的数据库名

#这里只将端口号修改为了3306 下面可以修改默认账号和密码
<property name="serverPort">3306</property> <property name="managerPort">9066</property>
                        <property name="idleTimeout">300000</property> <property name="bindIp">0.0.0.0</property>
                        <property name="dataNodeIdleCheckPeriod">300000</property>
                        <property name="frontWriteQueueSize">4096</property> <property name="processors">32</property>

修改schema.xml实现读写分离策略


        select user()

客户端连接到mycat:

root@ubuntu1804-1:~# mysql -uroot -p123456 -h 10.0.0.10 -P3306
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 2
Server version: 5.6.29-mycat-1.6.7.4-release-20200105164103 MyCat Server (OpenCloudDB)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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.

测试:分别开启master和slave的通用日志,会发现读操作都在slave上进行,写操作都在master上进行。

mysql> show variables like 'general_log';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| general_log   | OFF   |
+---------------+-------+
1 row in set (0.00 sec)

mysql> set global general_log=ON;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'general_log';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| general_log   | ON    |
+---------------+-------+
1 row in set (0.00 sec)

mysql> show variables like 'general_log_file';
+------------------+----------------------------+
| Variable_name    | Value                      |
+------------------+----------------------------+
| general_log_file | /var/lib/mysql/CentOS8.log |
+------------------+----------------------------+
1 row in set (0.02 sec)

Original: https://www.cnblogs.com/heyongshen/p/16732897.html
Author: 背对背依靠
Title: MyCAT实现MySQL读写分离

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/578568/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • Jmeter环境变量配置你不得不知道的事情

    在安装Jmeter的过程中大家肯定需要配置环境,但是为什么要配置JDK的环境变量呢?大家有没有好奇过,有没有仔细去像一下呢,其实在安装Jmeter前,大家应该都知道Jmeter是我…

    Linux 2023年6月14日
    0109
  • 内存管理-物理内存虚拟内存布局

    ARM-linux环境,物理内存和虚拟内存之间的映射关系: Original: https://www.cnblogs.com/fanguang/p/11930358.htmlAu…

    Linux 2023年6月6日
    090
  • vim的使用

    1、概述: Vim 是从 vi 发展出来的一个文本编辑器。具有代码补全、编译及错误跳转等功能 2、vim编辑器的常用命令: 图源:https://vimsky.com/articl…

    Linux 2023年5月27日
    0124
  • 【设计模式】Java设计模式-单例模式

    【设计模式】Java设计模式 – 单例模式 😄 不断学习才是王道🔥 继续踏上学习之路,学之分享笔记👊 总有一天我也能像各位大佬一样🌝分享学习心得,欢迎指正,大家一起学习…

    Linux 2023年6月6日
    0149
  • MySQL — 数据操作语言

    DML 全称 Data Manipulation Language。数据操作语言,用来对数据库表中的数据进行增删改。 插入一条数据 插入多条数据 update &#x886…

    Linux 2023年6月8日
    0105
  • 解决报错 Microsoft Visual C++ 14.0 is required

    环境:Surface Windows 10 专业版 问题:安装 Python3 的第三方库 py7zr 时不成功。而报错的是另外一个依赖库 pycryptodomex distut…

    Linux 2023年6月14日
    0110
  • Guava中这些Map的骚操作,让我的代码量减少了50%

    Guava是google公司开发的一款Java类库扩展工具包,内含了丰富的API,涵盖了集合、缓存、并发、I/O等多个方面。使用这些API一方面可以简化我们代码,使代码更为优雅,另…

    Linux 2023年6月7日
    0114
  • Ubuntu16.04修改IP

    ssh登录到服务。编辑网卡配置文件。 vim /etc/network/interfaces 先关闭DPCP,将 iface eth0 inet dhcp前面加上#号。 设置IP地…

    Linux 2023年6月6日
    0103
  • Redis 全局通用命令整理

    转载请注明出处: 1.查看所有键 该命令会存在线程阻塞问题,keys 命令也可以通过正则匹配获取存在的缓存数据 Redis从2.8版本后,提供了一个新的命令scan,它能有效的解决…

    Linux 2023年5月28日
    089
  • WEB自动化-10-Page Object 模型

    10 Page Object 模型 10.1 概述 在针对一个WEB页面编写自动化测试用例时,需要引用页面中的元素( 数据)才能进行操作( 动作)并显示出页面内容。如果编写的测试用…

    Linux 2023年6月7日
    076
  • USB转双串口产品设计-TTL串口

    基于USB转2路串口芯片CH342,可以为各类主机扩展出2个独立的串口。CH342芯片支持使用操作系统内置的CDC串口驱动,也支持使用厂商提供的VCP串口驱动程序,可支持Windo…

    Linux 2023年6月7日
    0100
  • Centos8 设置中文

    1、一般情况 1.1 进入设置选择 Region&Language 1.2 点击 加号 1.3 点击 汉语(中国) 1.4 选择 汉语(智能拼音) 2、特殊情况 有些虚拟机…

    Linux 2023年5月27日
    0116
  • 网络设备配置–8、利用ospf配置动态路由

    一、前言 同系列前几篇:网络设备配置–1、配置交换机enable、console、telnet密码网络设备配置–2、通过交换机划分vlan网络设备配置&#8…

    Linux 2023年6月8日
    0110
  • [转]全网最!详!细!tarjan算法讲解

    转发地址:https://blog.csdn.net/qq_34374664/article/details/77488976 原版的地址好像挂了….. 看到别人总结的…

    Linux 2023年6月7日
    074
  • Linux 0.11源码阅读笔记-中断过程

    Linux 0.11源码阅读笔记-中断过程 是什么中断 中断发生时,计算机会停止当前运行的程序,转而执行中断处理程序,然后再返回原被中断的程序继续运行。中断包括硬件中断和软件中断,…

    Linux 2023年5月27日
    0113
  • 2-第一个Django程序

    第一个Django程序 从本章节开始将通过实现一个投票应用程序,来让用户逐步的了解Django。这个程序由两步分组成: 公共站点,允许用户访问进行投票,和查看投票。 站点管理,允许…

    Linux 2023年6月7日
    0109
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球