目录
MySQL集群读写分离方案D-(MySQL+GTID+MyCat2+Keepalive)之4/6:MyCat2的读写分离
部分内容参见:https://www.w3cschool.cn/mycat2/mycat2-xok13kry.html
一、配置Macat读写分离
1、登录Mycat,创建逻辑库,配置数据源
(1)登录mycat 8066数据库
注:必须要使用远程ip和端口登录8066端口,在创建逻辑库时,才会在schemas目录下,生成*数据库名*.schema.json。
注意:【不要使用mysql -uroot -prootroot -P 8066登录,其中这种方式的登录密码是mysql数据库的root密码。】
mysql -uroot -p123456 -h192.168.21.176 -P 8066 --注意:远程方式的方式mycat中配置文件root.user.json中的root用户及密码。
(2)在Mycat里创建数据库operationproject
create database operationproject;
会自动创建operationproject的逻辑库以及operationproject的json文件。
(3)#修改operationproject.schema.json 指定数据源格式:"targetName": "prototype"
这里的targetName和后面的集群名关联,实现读写分离。
vi /mysql/app/mycat/conf/schemas/operationproject.schema.json 增加一行: "targetName": "prototype"
(4)添加数据源,完成逻辑库和物理库的映射关系
使用注解方式添加数据源,#登录Mycat,注解方式添加数据源,实现逻辑库和物理库的映射关系。
这里的数据库名称是物理库,在后端的operationproject库中真实存在这些库。
/*+ mycat:createDataSource{ "name":"oper_w1","url":"jdbc:mysql://192.168.21.161:3306/operationproject?useSSL=false&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true", "user":"root","password":"rootroot" } */; /*+ mycat:createDataSource{ "name":"oper_r1","url":"jdbc:mysql://192.168.21.162:3306/operationproject?useSSL=false&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true", "user":"root","password":"rootroot" } */; /*+ mycat:createDataSource{ "name":"oper_r2","url":"jdbc:mysql://192.168.21.163:3306/operationproject?useSSL=false&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true", "user":"root","password":"rootroot" } */; /*+ mycat:createDataSource{ "name":"oper_r3","url":"jdbc:mysql://192.168.21.164:3306/operationproject?useSSL=false&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true", "user":"root","password":"rootroot" } */; /*+ mycat:createDataSource{ "name":"oper_r4","url":"jdbc:mysql://192.168.21.165:3306/operationproject?useSSL=false&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true", "user":"root","password":"rootroot" } */;
或不需要operationproject指定也可以
/*+ mycat:createDataSource{ "name":"oper_w1","url":"jdbc:mysql://192.168.21.161:3306/?useSSL=false&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true", "user":"root","password":"rootroot" } */; /*+ mycat:createDataSource{ "name":"oper_r1","url":"jdbc:mysql://192.168.21.162:3306/?useSSL=false&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true", "user":"root","password":"rootroot" } */; /*+ mycat:createDataSource{ "name":"oper_r2","url":"jdbc:mysql://192.168.21.163:3306/?useSSL=false&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true", "user":"root","password":"rootroot" } */; /*+ mycat:createDataSource{ "name":"oper_r3","url":"jdbc:mysql://192.168.21.164:3306/?useSSL=false&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true", "user":"root","password":"rootroot" } */; /*+ mycat:createDataSource{ "name":"oper_r4","url":"jdbc:mysql://192.168.21.165:3306/?useSSL=false&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true", "user":"root","password":"rootroot" } */;
(5)#查询配置数据源结果
/*+ mycat:showDataSources{} */;
(6)删除/修改数据源
删除或修改/mysql/app/mycat/conf/datasources对应的数据源文件,重启mycat即可。 /*+ mycat:dropDataSource{ "dbType":"mysql", "idleTimeout":60000, "initSqls":[], "initSqlsGetConnection":true, "instanceType":"READ_WRITE", "maxCon":1000, "maxConnectTimeout":3000, "maxRetryCount":5, "minCon":1, "name":"oper_w1", "type":"JDBC", "weight":0 } */;
2、更新集群信息,添加prototype从节点,实现读写分离
(1)#更新集群信息,添加prototype从节点
/*!mycat:createCluster{"name":"prototype","masters":["oper_w1"],"replicas":["oper_r1","oper_r2","oper_r3","oper_r4"]} */;
(2)#查看配置集群信息
/*+ mycat:showClusters{} */;
(3)#查看集群配置文件
less /mysql/app/mycat/conf/clusters/prototype.cluster.json
readBalanceType-查询负载均衡策略-可选值:
BALANCE_ALL(默认值) 获取集群中所有数据源 BALANCE_ALL_READ 获取集群中允许读的数据源 BALANCE_READ_WRITE 获取集群中允许读写的数据源,但允许读的数据源优先 BALANCE_NONE 获取集群中允许写数据源,即主节点中选择
switchType
NOT_SWITCH:不进行主从切换 SWITCH:进行主从切换
3、重新启动Mycat
cd /mysql/app/mycat/bin ./mycat restart
4、验证读写分离
注意:mycat2支持mysql8.0,但不支持mysql5.7的读写分离。
设置了两个点:没有做主从复制,所以两个表获取的数据不是一样的。轮询查看。