当前位置:网站首页>搭建MyCat2双主双从的MySQL读写分离
搭建MyCat2双主双从的MySQL读写分离
2022-08-04 19:50:00 【丨Jack_Chen丨】
搭建MyCat2双主双从的MySQL读写分离
搭建MyCat2读写分离
搭建MySQL双主双从复制
搭建MySQL双主双从复制参考:在Windows环境与Linux环境下搭建MySQL主从同步
1.做好双主双从的MySQL中有mydb
这个库,且已经存在表和数据
2.登录主库1:mysql -h localhost -P3306 -uroot -p
mysql> CREATE DATABASE mydb;
Query OK, 1 row affected (0.00 sec)
mysql> use mydb;
Database changed
mysql> CREATE TABLE mytb(id INT,name VARCHAR(30));
Query OK, 0 rows affected (0.02 sec)
mysql> INSERT INTO mytb VALUES(1,'mycat');
Query OK, 1 row affected (0.01 sec)
2.登录主库2:mysql -h localhost -P3307 -uroot -p
mysql> use mydb;
Database changed
mysql> select * from mytb;
+------+---------+
| id | name |
+------+---------+
| 1 | mycat |
+------+---------+
1 row in set (0.00 sec)
3.登录从库1:mysql -h localhost -P3308 -uroot -p
mysql> use mydb;
Database changed
mysql> select * from mytb;
+------+---------+
| id | name |
+------+---------+
| 1 | mycat |
+------+---------+
1 row in set (0.00 sec)
4.登录从库2:mysql -h localhost -P3309 -uroot -p
mysql> use mydb;
Database changed
mysql> select * from mytb;
+------+---------+
| id | name |
+------+---------+
| 1 | mycat |
+------+---------+
1 row in set (0.00 sec)
安装MyCat2
安装MyCat2参考:MyCat2的介绍与安装以及基本使用
逻辑库配置
登录Mycat:mysql -uroot -p123456 -h localhost -P 8066
,创建逻辑库
mysql> create database mydb;
Query OK, 1 row affected (0.00 sec)
执行create database mydb;
后,将在/mycat/conf/schemas/
自动生成mydb.schema.json
文件
{
"customTables":{
},
"globalTables":{
},
"normalProcedures":{
},
"normalTables":{
},
"schemaName":"mydb",
"shardingTables":{
},
"views":{
}
}
customTables:mycat默认的表的配置
globalTables:全局表的配置
shardingTables:分片表的配置
normalTables:普通表的配置
修改schema的配置,指定mydb逻辑库默认的targetName,mycat会自动加载mydb下已经有的物理表或者视图作为单表
{
"customTables":{
},
"globalTables":{
},
"normalProcedures":{
},
"normalTables":{
},
"schemaName":"mydb",
"targetName":"prototype"
"shardingTables":{
},
"views":{
}
}
查看修改之后的schema的配置,发现已经自动加载了相关信息到normalTables中
{
"customTables":{
},
"globalTables":{
},
"normalProcedures":{
},
"normalTables":{
"mytb":{
"createTableSQL":"CREATE TABLE `mydb`.`mytb` (\n\t`id` int(11) DEFAULT NULL,\n\t`name` varchar(30) DEFAULT NULL\n) ENGINE = InnoDB CHARSET = utf8",
"locality":{
"schemaName":"mydb",
"tableName":"mytb",
"targetName":"prototype"
}
}
},
"schemaName":"mydb",
"shardingTables":{
},
"views":{
}
}
登录Mycat:mysql -uroot -p123456 -h localhost -P 8066
,进行查询
mysql> use mydb;
Database changed
mysql> SELECT * FROM mytb;
+------+-------+
| id | name |
+------+-------+
| 1 | mycat |
+------+-------+
1 row in set (0.03 sec)
添加数据源
添加Master1数据源
/*+ mycat:createDataSource{ "name":"write1","url":"jdbc:mysql://112.74.96.150:3306/mydb?useSSL=false&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true", "user":"mycat","password":"123456" } */;
添加Master2数据源
/*+ mycat:createDataSource{ "name":"write2","url":"jdbc:mysql://112.74.96.150:3307/mydb?useSSL=false&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true", "user":"root","password":"123456" } */;
添加Slave1数据源
/*+ mycat:createDataSource{ "name":"red1","url":"jdbc:mysql://112.74.96.150:3308/mydb?useSSL=false&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true", "user":"root","password":"123456" } */;
添加Slave2数据源
/*+ mycat:createDataSource{ "name":"red2","url":"jdbc:mysql://112.74.96.150:3309/mydb?useSSL=false&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true", "user":"root","password":"123456" } */;
查询配置数据源结果
/*+ mycat:showDataSources{} */;
通过注释命名添加数据源后,在对应目录会生成相关配置文件,查看数据源配置文件:mycat/conf/datasources
[root@administrator mycat]# ls conf/datasources
prototypeDs.datasource.json red2.datasource.json write2.datasource.json
red1.datasource.json write1.datasource.json
更新集群配置
使用mycat自带的默认集群:prototype
,对其修改更新即可
/*! mycat:createCluster{"name":"prototype","masters":["write1","write2"],"replicas":["red1","red2","write2"]} */;
查看配置集群信息
/*+ mycat:showClusters{} */;
查看集群配置文件,发现集群配置信息已经更新
vim /usr/local/mycat/conf/clusters/prototype.cluster.json
{
"clusterType":"MASTER_SLAVE",
"heartbeat":{
"heartbeatTimeout":1000,
"maxRetryCount":3,
"minSwitchTimeInterval":300,
"showLog":false,
"slaveThreshold":0.0
},
"masters":[
"write1",
"write2"
],
"maxCon":2000,
"name":"prototype",
"readBalanceType":"BALANCE_ALL",
"replicas":[
"red1",
"red2",
"write2"
],
"switchType":"SWITCH"
}
readBalanceType:查询负载均衡策略
BALANCE_ALL(默认值):获取集群中所有数据源
BALANCE_ALL_READ:获取集群中允许读的数据源
BALANCE_READ_WRITE:获取集群中允许读写的数据源,但允许读的数据源优先
BALANCE_NONE:获取集群中允许写数据源,即主节点中选择
switchType:控制主从切换
NOT_SWITCH:不进行主从切换
SWITCH:进行主从切换
此时,架构如下
验证读写分离
修改MySQL的配置文件:my.cnf
,设置logbin格式binlog_format=STATEMENT
,重启MySQL,确保此时主从复制正常
登录MyCat:mysql -uroot -p123456 -h IP -P 8066
,向数据表插入系统变量
值,以此造成主从数据不一致,便于验证读写分离。
mysql> use mydb;
Database changed
mysql> INSERT INTO mytb VALUES(2,@@hostname);
登录双主双从MySQL4个库查看表数据
mysql -uroot -h IP -P 3306 -p
mysql -uroot -h IP -P 3307 -p
mysql -uroot -h IP -P 3308 -p
mysql -uroot -h IP -P 3309 -p
登录MyCat:mysql -uroot -p123456 -h IP -P 8066
,查询验证
mysql> use mydb;
Database changed
mysql> SELECT * FROM mytb;
+------+--------------+
| id | name |
+------+--------------+
| 1 | mycat |
| 2 | 3cf0d174674d |
+------+--------------+
2 rows in set (0.01 sec)
mysql> SELECT * FROM mytb;
+------+--------------+
| id | name |
+------+--------------+
| 1 | mycat |
| 2 | 5990c4387bd9 |
+------+--------------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM mytb;
+------+--------------+
| id | name |
+------+--------------+
| 1 | mycat |
| 2 | 4e4f0d81922a |
+------+--------------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM mytb;
+------+---------------+
| id | name |
+------+---------------+
| 1 | mycat |
| 2 | administrator |
+------+---------------+
2 rows in set (0.01 sec)
mysql> SELECT * FROM mytb;
+------+--------------+
| id | name |
+------+--------------+
| 1 | mycat |
| 2 | 5990c4387bd9 |
+------+--------------+
2 rows in set (0.00 sec)
边栏推荐
- 【Attention演变史】翻译模型seq2seq (第二弹)
- Query the published version records of the APP Store
- How to promote the implementation of rural revitalization
- 37.轮播图
- Spark提交参数说明和常见优化
- 「 WAIC 2022 · 黑客马拉松」蚂蚁财富两大赛题邀你来战!
- 七夕福利!中奖名单:书籍免费送!
- Exploration and Practice of Database Governance
- NLP技术为何在工业界这么卷?前沿案例解析来了
- How to monitor code cyclomatic complexity by refactoring indicators
猜你喜欢
从卖产品到卖“链路”:20条策略 解读直播带货迭代玩法
多用户同时远程登录连接到一台服务器
Pedestrian fall detection experiment based on YOLOV5
刷题-洛谷-P1304 哥德巴赫猜想
AWS SES 的监控和告警
力扣题(5)—— 最长回文子串
"WAIC 2022 · hackers marathon" two ants wealth competition invited you to fight!
Go学习笔记(篇一)配置Go开发环境
小软件大作用 | 如何省时省力进行Gerber图层快速对比?
Orthodontic MIA micro-implant anchorage technology China 10th anniversary exchange meeting was held in Shenyang
随机推荐
really time ntp service start command
Use "green computing" technology to promote sustainable development of computing power
高效目标检测:动态候选较大程度提升检测精度(附论文下载)
The difference between Client Side Cache and Server Side Cache
运维就业现状怎么样?技能要求高吗?
Latex分章节、分段落编译:input{}与include{}的区别
c sqlite ... ...
电脑一键重装系统后连不上远程了?教你设置的方法
Video Object Detection
密码学系列之:PEM和PKCS7,PKCS8,PKCS12
简易数据缓存层的建立
The list of Kubernetes - watch mechanism
带你了解数据分布式存储原理
前3名突然变了,揭秘 7 月编程语言最新排行榜
zynq records
二叉树是否对称
visual studio 与 visual studio code
阿里的arthas使用,入门报错:Unable to attach to 32-bit process running under WOW64
面试官:JVM运行时数据区包含哪几部分?作用是啥?
工业相机CCD与CMOS