当前位置:网站首页>搭建MyCat2一主一从的MySQL读写分离
搭建MyCat2一主一从的MySQL读写分离
2022-08-04 19:50:00 【丨Jack_Chen丨】
搭建MyCat2一主一从的MySQL读写分离
搭建MyCat2读写分离
通过Mycat2与MySQL的主从同步复制配合就可以搭建MySQL数据库的读写分离,这里搭建MyCat2一主一从的MySQL读写分离。
搭建MySQL主从复制
搭建MySQL主从复制参考:在Windows环境与Linux环境下搭建MySQL主从同步
1.做好主从的MySQL中有mydb
这个库,且已经存在表和数据
2.登录主库: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)
3.登录从库: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)
安装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)
此时,架构如下:
添加数据源
登录Mycat:mysql -uroot -p123456 -h localhost -P 8066
,使用注解方式添加数据源
添加主库数据源
mysql> /*+ mycat:createDataSource{ "name":"write","url":"jdbc:mysql://IP:3306/mydb?useSSL=false&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true", "user":"mycat","password":"123456" } */;
Query OK, 0 rows affected (0.00 sec)
添加从库数据源
mysql> /*+ mycat:createDataSource{ "name":"red","url":"jdbc:mysql://IP:3307/mydb?useSSL=false&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true", "user":"root","password":"123456" } */;
Query OK, 0 rows affected (0.00 sec)
查询配置数据源结果
mysql> /*+ mycat:showDataSources{} */;
Query OK, 0 rows affected (0.00 sec)
通过注释命名添加数据源后,在对应目录会生成相关配置文件,查看数据源配置文件:mycat/conf/datasources
[root@administrator mycat]# ls conf/datasources
prototypeDs.datasource.json red.datasource.json write.datasource.json
更新集群配置
使用mycat自带的默认集群:prototype
,对其修改更新即可
/*! mycat:createCluster{"name":"prototype","masters":["write"],"replicas":["red"]} */;
查看配置集群信息
/*+ 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":[
"write"
],
"maxCon":2000,
"name":"prototype",
"readBalanceType":"BALANCE_ALL",
"replicas":[
"red"
],
"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);
登录主库:mysql -uroot -h IP -P 3306 -p
查看表数据
mysql> SELECT * FROM mytb;
+------+---------------+
| id | name |
+------+---------------+
| 1 | mycat |
| 2 | administrator |
+------+---------------+
2 rows in set (0.01 sec)
登录从库:mysql -uroot -h IP -P 3307 -p
查看表数据
mysql> use mydb;
Database changed
mysql> select * from mytb;
+------+--------------+
| id | name |
+------+--------------+
| 1 | mycat |
| 2 | 5990c4387bd9 |
+------+--------------+
2 rows
登录MyCat:mysql -uroot -p123456 -h IP -P 8066
,查询验证
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.01 sec)
边栏推荐
- The book "The Essence of Alipay Experience Design", a record of knowledge related to testing
- awk statistical difference record
- 面试官:JVM运行时数据区包含哪几部分?作用是啥?
- 如何让远在的老板看到你!----------来自财富中国网
- 5G NR 笔记记录
- vscode离线安装插件方法
- Use "green computing" technology to promote sustainable development of computing power
- really time ntp service start command
- 如何推动乡村振兴的落地
- seata源码解析:seata server各种消息处理流程
猜你喜欢
随机推荐
Tensorflow2 环境搭建
《支付宝体验设计精髓》一书,跟测试相关性知识记录
成品升级程序
Openharmony first experience (1)
MMDetection usage example: from entry to exit
Pedestrian fall detection experiment based on YOLOV5
【SQL】触发器同步表数据
Query the published version records of the APP Store
SAP UI5 确保控件 id 全局唯一的实现方法
如何让远在的老板看到你!----------来自财富中国网
2022年国内手机满意度榜单:华为稳坐国产品牌第一
Seata source code analysis: various message processing processes of seata server
zynq records
5G NR 笔记记录
按需视觉识别:愿景和初步方案
Go study notes (Part 1) Configuring the Go development environment
性能测试流程
C#弹出询问对话框
六月 致 -.-- -..- -
awk statistical difference record