做之前停掉firewalld及本机的mysql
前言:mycat 2 要安装jdk
1.创建安装目录
mkdir /data
cd /data
wget http://dl.mycat.org.cn/2.0/install-template/mycat2-install-template-
1.21.zip
wget http://dl.mycat.org.cn/2.0/1.21-release/mycat2-1.21-release-jar-with-
dependencies.jar
yum -y install unzip
unzip mycat2-install-template-1.21.zip
cd /data/mycat/bin
chmod +x *
4.把所需要的jar复制到/data/mycat/lib目录下
cp /data/tools/mycat2-1.21-release-jar-with-dependencies.jar /data/mycat/lib
mycat目录结构
ll /data/mycat
total 8
drwxr-xr-x 2 root root 4096 Mar 5 2021 bin
drwxr-xr-x 9 root root 275 Mar 5 2021 conf
drwxr-xr-x 2 root root 4096 Mar 20 14:11 lib
drwxr-xr-x 2 root root 6 Mar 5 2021 logs
1)bin 执行命令的目录
2)conf 配置文件
3)lib 依赖包
4)logs 日志包
运行mycat
cd /data/mycat/bin/
./mycat status
/*+ mycat:createDataSource{
"name":"m1",
"password":"123456",
"type":"JDBC",
"url":"jdbc:mysql://127.0.0.1:3306/db1?useUnicode=true&serverTimezone=UTC&characterEncoding=UTF-8",
"user":"root"
} */;
/*+ mycat:createDataSource{
"instanceType":"READ",
"name":"m1s1",
"password":"123456",
"type":"JDBC",
"url":"jdbc:mysql://127.0.0.1:3307/db1?useUnicode=true&serverTimezone=UTC&characterEncoding=UTF-8",
"user":"root"
} */;
/*+ mycat:createDataSource{
"instanceType":"READ",
"name":"m1s2",
"password":"123456",
"type":"JDBC",
"url":"jdbc:mysql://127.0.0.1:3308/db1?useUnicode=true&serverTimezone=UTC&characterEncoding=UTF-8",
"user":"root"
} */;
{
"dbType":"mysql",
"idleTimeout":60000,
"initSqls":[],
"initSqlsGetConnection":true,
"instanceType":"READ_WRITE",
"logAbandoned":true,
"maxCon":1000,
"maxConnectTimeout":30000,
"maxRetryCount":5,
"minCon":1,
"name":"prototypeDs",
"password":"123456",
"queryTimeout":0,
"removeAbandoned":false,
"removeAbandonedTimeoutSecond":180,
"type":"JDBC",
"url":"jdbc:mysql://localhost:3306/mysql?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8&autoReconnect=true",
"user":"root",
"weight":0
}type: 数据源类型NATIVE,只使用NATIVE协议(即Mycat自研的连接MySQL的协议)
JDBC,默认,只使用JDBC驱动连接NATIVE_JDBC,该数据源同一个配置同时可以使用NATIVE,JDBCname: 数据源名称
url: JDBC连接URL对应实际MySQL连接地址,可以无需指定MySQL schema。如果URL中配置了MySQL schema,相应的MySQL schema必须在物理库上先创建好,否则无法启动mycat。
password: MySQL用户密码
instanceType: 配置实例只读还是读写READ=只读节点READ_WRITE=读写节点
queryTimeout: jdbc查询超时时间,单位:ms 默认30mills,根据实际业务需要调整超时时间
maxConnectTimeout: 定时检查闲置连接,单位:ms
initSqlsGetConnection: 每次获取jdbc连接是否都执行initSqls,true|false,默认:false,JDBC禁用SSL属性有助提高性能
检查舒服创建成功(notepad++中查看)
/*! mycat:createCluster{
"clusterType":"MASTER_SLAVE",
"heartbeat":{
"heartbeatTimeout":1000,
"maxRetry":3,
"minSwitchTimeInterval":300,
"slaveThreshold":0
},
"masters":[
"m1"
],
"maxCon":2000,
"name":"prototype",
"readBalanceType":"BALANCE_ALL",
"replicas":[
"m1s1","m1s2"
],
"switchType":"SWITCH"
} */;
查看集群
如果URL中配置了db1,相应的db1必须在物理库上先创建好,否则无法启动mycat。
CREATE DATABASE db1 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
实则就添加了"targetName":"prototype"
vim /data/mycat/conf/schemas/db1.schema.json
{
"customTables":{},
"globalTables":{},
"normalProcedures":{},
"normalTables":{},
"schemaName":"db1",
"shardingTables":{},
"targetName":"prototype",
"views":{}
}
[root@localhost bin]# cd /data/mycat/bin/
[root@localhost bin]# ./mycat restart
Stopping mycat2...
Stopped mycat2.
Starting mycat2...
再在mysql1中执行 (应该在创建逻辑库之前做)
CREATE DATABASE db1 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
use db1
CREATE TABLE SYS_USER( ID BIGINT PRIMARY KEY, USERNAME VARCHAR(200) NOT NULL,
ADDRESS VARCHAR(500));
INSERT INTO SYS_USER(ID,USERNAME,ADDRESS) VALUES(1,"XIAOMING","WUHAN");
最后查看后端物理库:发现物理库和物理表都生成了