Mycat2实现读写分离
摘要
1.环境准备
linux (CentOS 7), Java8+
主从复制服务器
Mycat 2 192.168.217.129:8066
主节点 192.168.217.129:3306
从节点 192.168.217.128:3316
2.创建数据库用户
# 在主从节点均创建给 Mycat 程序使用的用户并授权
CREATE USER 'mycat'@'%' IDENTIFIED BY 'password';
-- MySQL 8 必须赋予的权限
GRANT XA_RECOVER_ADMIN ON *.* to 'mycat'@'%';
-- 视情况赋予权限
GRANT ALL PRIVILEGES ON *.* to 'mycat'@'%';
FLUSH PRIVILEGES;
# 这里为了省事没有创建用户,后面配置都用的root用户
3.在主节点创建Mycat2使用的数据库 mycat
CREATE DATABASE IF NOT EXISTS `mycat`;
这个库称为 Mycat 的原型库(prototype),Mycat 在启动时,会自动在原型库下创建其运行时所需的数据表。
这里把 Mycat 服务用的 mycat 库和后面主从同步的 test 库都放在了主节点上
4.下载安装Mycat2
mkdir -p /path/to/mycat2
cd /path/to/mycat2
# 下载安装包
wget http://dl.mycat.org.cn/2.0/install-template/mycat2-install-template-1.21.zip
# 下载 Mycat 2 所需依赖 jar
wget http://dl.mycat.org.cn/2.0/1.21-release/mycat2-1.21-release-jar-with-dependencies.jar
unzip mycat2-install-template-1.21.zip
cd mycat
# 复制 Mycat 2 所需依赖 jar 到 mycat 的 lib 文件夹
cp ../mycat2-1.21-release-jar-with-dependencies.jar lib/
# 授予 bin 目录下所有命令可执行权限
chmod +x bin/*
5.Mycat2配置原型库数据源
# 这个库必须配置,否则在启动 Mycat 时会报错
cd /path/to/mycat2/mycat/conf/datasources
vim prototypeDs.datasource.json
{
"dbType":"mysql",
"idleTimeout":60000,
"initSqls":[],
"initSqlsGetConnection":true,
"instanceType":"READ_WRITE",
"maxCon":1000,
"maxConnectTimeout":3000,
"maxRetryCount":5,
"minCon":1,
"name":"test",
"password":"123456",
"type":"JDBC",
"url":"jdbc:mysql://localhost:3306/mycat?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8",
"user":"root",
"weight":0
}
6.添加test数据库的数据源信息
# 复制出主数据源配置
cp prototypeDs.datasource.json test.datasource.json
# 复制出从数据源配置
cp prototypeDs.datasource.json testSlave.datasource.json
# 编辑 主库数据源配置
vim test.datasource.json
# 配置信息如下
{
"dbType":"mysql",
"idleTimeout":60000,
"initSqls":[],
"initSqlsGetConnection":true,
"instanceType":"READ_WRITE",
"maxCon":1000,
"maxConnectTimeout":3000,
"maxRetryCount":5,
"minCon":1,
"name":"test",
"password":"123456",
"type":"JDBC",
"url":"jdbc:mysql://localhost:3306/test?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8",
"user":"root",
"weight":0
}
# 注意:"instanceType":"READ_WRITE",
# 数据库读写类型。在数据库集群时,Mycat 对主节点都是可读可写的
# 编辑从库数据源信息
vim testSlave.datasource.json
# 配置如下
{
"dbType":"mysql",
"idleTimeout":60000,
"initSqls":[],
"initSqlsGetConnection":true,
"instanceType":"READ",
"maxCon":1000,
"maxConnectTimeout":3000,
"maxRetryCount":5,
"minCon":1,
"name":"testSlave",
"password":"123456",
"type":"JDBC",
"url":"jdbc:mysql://192.168.217.128:3316/test?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8",
"user":"root",
"weight":0
}
# 注意:"instanceType":"READ",
# 数据库读写类型。在数据库集群时,Mycat 对从节点都是只读的
7.配置主从数据源的集群(cluster)信息
cd /path/to/mycat2/mycat/conf/clusters
cp prototype.cluster.json test.cluster.json
vim test.cluster.json
{
"clusterType":"MASTER_SLAVE",
"heartbeat":{
"heartbeatTimeout":1000,
"maxRetryCount":0,
"minSwitchTimeInterval":300,
"showLog":true,
"slaveThreshold":0.0
},
//配置多个主节点,在主挂的时候会选一个检测存活的数据源作为主节点
"masters":[
"test"
],
//配置多个从节点
"replicas":[
"testSlave"
],
"maxCon":200,
"name":"prototype",
"readBalanceType":"BALANCE_ALL",
"switchType":"SWITCH"
////////////////////////////////////可选//////////////////////////////////
//MySQL集群心跳周期,配置则开启集群心跳,Mycat主动检测主从延迟以及高可用主从切换
,"timer":{
"initialDelay": 30,
"period":5,
"timeUnit":"SECONDS"
},
//readBalanceName:"BALANCE_ALL",
//writeBalanceName:"BALANCE_ALL",
}
8.配置物理库(schema)和 Mycat 中数据源/数据源集群的关系
cd /path/to/mycat2/mycat/conf/schemas
cp mysql.schema.json test.schema.json
vim test.schema.json
{
"customTables":{},
"globalTables":{},
// 这里可以配置数据表相关的信息,在物理表已存在或需要启动时自动创建物理表时配置此项
"normalTables":{
},
//物理库
"schemaName":"test",
"shardingTables":{},
// 指向集群,或者数据源
"targetName":"test"
}
9.修改 Mycat 登录用户信息
cd /path/to/mycat2/mycat/conf/users
vim root.user.json
{
"dialect":"test",
// ip 为 null,允许任意 ip 登录
"ip":null,
"password":"123456",
"transactionType":"xa",
"username":"root"
}
10.修改 Mycat 服务端口等信息
cd /path/to/mycat2/mycat/conf
vim server.json
{
"loadBalance":{
"defaultLoadBalance":"BalanceRandom",
"loadBalances":[]
},
"mode":"local",
"properties":{},
"server":{
"bufferPool":{
},
"idleTimer":{
"initialDelay":3,
"period":60000,
"timeUnit":"SECONDS"
},
"ip":"0.0.0.0",
"mycatId":1,
# 端口配置
"port":8066,
# 对应 mysql数据库版本及mycat版本(mysql-mycat)
"serverVersion":"8.0.20-mycat-2.0",
"reactorNumber":8,
"tempDirectory":null,
"timeWorkerPool":{
"corePoolSize":0,
"keepAliveTime":1,
"maxPendingLimit":65535,
"maxPoolSize":2,
"taskTimeout":5,
"timeUnit":"MINUTES"
},
"workerPool":{
"corePoolSize":1,
"keepAliveTime":1,
"maxPendingLimit":65535,
"maxPoolSize":1024,
"taskTimeout":5,
"timeUnit":"MINUTES"
}
}
}
到这里就全结束了,启动下mycat 就行了