Mycat2+Mysql一主一从实现读写分离配置
前置配置
- Mysql一主一从搭建
- Mycat2环境搭建
环境信息
ip地址 | 软件 | 角色 | 版本 |
---|---|---|---|
192.168.1.19 | Mysql | 主 | 8.0.40-0ubuntu0.20.04.1 |
192.168.1.19 | Mycat2 | —— | 1.21-release-3-14 |
192.168.1.20 | Mysql | 从 | 8.0.40-0ubuntu0.20.04.1 |
操作步骤
1. 修改并增加数据源
- 新增配置文件
${mycat安装目录}/conf/datasources/ds0.datasource.json
,该文件在环境搭建时应该已修改好
{"dbType":"mysql","idleTimeout":60000,"initSqls":[],"initSqlsGetConnection":true,"instanceType":"READ_WRITE","maxCon":1000,"maxConnectTimeout":3000,"maxRetryCount":5,"minCon":1,"name":"ds0","password":"123456","type":"JDBC","url":"jdbc:mysql://localhost:3306/test?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8&useSSL=false","user":"mycat","weight":0
}
- 增加配置文件
${mycat安装目录}/conf/datasources/ds1.datasource.json
,ds1为随意取的名字,与文件中name
的值保持一致即可
{"dbType":"mysql","idleTimeout":60000,"initSqls":[],"initSqlsGetConnection":true,"instanceType":"READ","maxCon":1000,"maxConnectTimeout":3000,"maxRetryCount":5,"minCon":1,"name":"ds1","password":"123456","type":"JDBC","url":"jdbc:mysql://192.168.1.20:3306/test?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8&useSSL=false","user":"mycat","weight":0
}
- instanceType:instanceType配置实例只读还是读写,读节点可以设置为READ
2. 修改集群配置
- 复制prototype.cluster.json文件为master-slave.cluster.json
cp prototype.cluster.json master-slave.cluster.json
- 修改配置文件
${mycat安装目录}/conf/cluster/master-slave.cluster.json
{"clusterType":"MASTER_SLAVE","heartbeat":{"heartbeatTimeout":1000,"maxRetry":3,"minSwitchTimeInterval":300,"slaveThreshold":0},"masters":["ds0"],"replicas":["ds1"],"maxCon":200,"name":"master-slave","readBalanceType":"BALANCE_ALL_READ","switchType":"NOT_SWITCH"
}
-
解释:
-
replicas: 配置datasource目录下Mysql从机数据源名称
-
name:后面schema配置会用到
-
readBalanceType: 查询负载均衡策略
- BALANCE_ALL(默认值):获取集群中所有数据源
- BALANCE_ALL_READ:获取集群中允许读的数据源
- BALANCE_READ_WRITE:获取集群中允许读写的数据源,但允许读的数据源优先
- BALANCE_NONE:获取集群中允许写数据源,即主节点中选择
-
switchType:切换模式
- NOT_SWITCH:不进行主从切换
- SWITCH:进行主从切换
-
3. 配置物理库 schemas和mycat数据源/集群的关联关系
- 新增配置文件
${mycat安装目录}/conf/schemas/test.schema.json
,命名为${数据库}.schema.json
{"schemaName": "test","targetName": "master-slave","normalTables": {}
}
- 解释
- schemaName:指向物理库
- targetName:指向集群或者数据源, 这里配置与前面集群配置的name一致
测试
测试前准备
- 开启Mysql主从的日志
#日志输出到表中 对应的表是mysql.general_log
SET GLOBAL log_output = 'TABLE'; #打开general_log,重启后失效
SET GLOBAL general_log = 'ON';# 测试完成后将日志重写入文件
SET GLOBAL log_output = 'FILE';
SET GLOBAL general_log = 'OFF'; #关闭
- 通过如下语句查看最近执行的语句
SELECT event_time,user_host,thread_id,server_id,command_type,CAST(argument AS CHAR(500) CHARACTER SET utf8mb4) argument
FROM mysql.general_log
ORDER BY event_time DESC;
- 为避免干扰,可在Mysql主机使用
truncate mysql.general_log
命令先清空表中日志再查询
连接Mycat
测试写入
- 在mycat的test数据库中写入插入命令
Mysql主机结果
Mysql从机查询结果
- 同步方式为ROW,可以看出从机并没有执行insert语句,但是数据已增加。尝试多次,结果一致,符合预期
测试查询
先清空mysql.general_log,并在Mycat中查询表user_info的最后三个结果
select * from `user_info` order by id desc limit 3;
两个数据库均有查询语句,符合预期
- Mysql主库
- Mysql从库
参考
- mycat2实现读写分离
- Mycat2权威指南 集群
- Mycat2权威指南 逻辑库与逻辑表