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)

大家都在看

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