Mycat sets Mysql read and write separation

1. Preparation:
Download address:
http://dl.mycat.org.cn/2.0/install-template/mycat2-install-template-1.21.zip
http://dl.mycat.org.cn/2.0/1.21 -release/

Downloaded file:
Insert image description here
Unzip the file mycat2-install-template-1.21, copy mycat2-1.21-release-jar-with-dependencies2022-5-5.jar to mycat\lib: 2. Mycat configurationInsert image description here
modification

1. Mycat configuration file location:

Insert image description here

Open the configuration prototypeDs.datasource.json file:

{
	"dbType":"mysql",
	"idleTimeout":60000,
	"initSqls":[],
	"initSqlsGetConnection":true,
	"instanceType":"READ_WRITE",
	"maxCon":1000,
	"maxConnectTimeout":3000,
	"maxRetryCount":5,
	"minCon":1,
	// 数据源名称,不用修改
	"name":"prototypeDs",
	//数据库密码
	"password":"lll123",
	"type":"JDBC",
	//连接master数据库
	"url":"jdbc:mysql://localhost:3306/mysql?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8",
	//数据库用户名
	"user":"root",
	"weight":0
}

After mycat is started, it will create its own mycat library to pre-store some information that mycat needs to use. Mycat will be created by itself. If the creation fails, you can manually create the database and then start it again .

1. Start mycat:

mycat install
mycat start
mycat status

2. Log in: log in to mycat, default username: root password: 123456
Insert image description here
3. Create a logical library

Create database test_mycat in Mycat

create database test_mycat;

Insert image description here

After the logical library is successfully created, the configuration file is automatically generated.
Insert image description here

The main library created the corresponding database.

Insert image description here

Open the test_mycat.schema.json file

{
	"customTables":{},
	"globalTables":{},
	"normalProcedures":{},
	"normalTables":{},
	//逻辑库名,也是mysql中对应的物理数据库名
	"schemaName":"test_mycat",
	//集群
	"targetName":"mycatCluster",
	"shardingTables":{},
	"views":{}
}

Explanation:
customTables places the configuration of mycat's default table
globalTables places the configuration of the global table
shardingTables places the configuration of the sharded table
normalTables places the configuration of the normal table

4. Configure data source

Log in to mycat with cmd or use the command line interface after navigating to connect to mycat and enter:
Comment command to add the main data source:

/*+ mycat:createDataSource{ "name" :"mycatWrite" , "url":"jdbc:mysql://localhost:3306/test_mycat?useSSL=false&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true","user":"root","password" :"ldst123"} */;

Add from data source:

/*+ mycat:createDataSource{ "name" :"mycatRead" , "url":"jdbc:mysql://localhost:3307/test_mycat?useSSL=false&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true","user":"root","password" :"ldst123"} */;

Insert image description here

Check whether the master-slave data source is created successfully!

/*+ mycat:showDataSources{} */

Insert image description here
Insert image description here
The master-slave data source file generated after success:
mycatWrite
mycatWrite:

{
	"dbType":"mysql",
	"idleTimeout":60000,
	"initSqls":[],
	"initSqlsGetConnection":true,
	"instanceType":"READ_WRITE",
	"maxCon":1000,
	"maxConnectTimeout":30000,
	"maxRetryCount":5,
	"minCon":1,
	"name":"mycatWrite",
	"password":"xxx123",
	"queryTimeout":0,
	"type":"JDBC",
	"url":"jdbc:mysql://localhost:3306/test_mycat?useJDBCCompliantTimezoneShift=true&useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8&autoReconnect=true&useSSL=false",
	"user":"root",
	"weight":0
}

mycatRead:

{
	"dbType":"mysql",
	"idleTimeout":60000,
	"initSqls":[],
	"initSqlsGetConnection":true,
	//跟/*+ mycat:showDataSources{} */命令查到的库中属性值不一致?
	"instanceType":"READ_WRITE",
	"maxCon":1000,
	"maxConnectTimeout":30000,
	"maxRetryCount":5,
	"minCon":1,
	"name":"mycatRead",
	"password":"xxx123",
	"queryTimeout":0,
	"type":"JDBC",
	"url":"jdbc:mysql://localhost:3307/test_mycat?serverTimezone=Asia/Shanghai&useJDBCCompliantTimezoneShift=true&characterEncoding=UTF-8&useUnicode=true&autoReconnect=true&useSSL=false",
	"user":"root",
	"weight":0
}

5. Create cluster configuration information

/*! mycat:createCluster{"name":"mycatCluster","masters":["mycatWrite"],"replicas":["mycatRead"]} */

Query cluster information:


/*+ mycat:showClusters{} */;

Insert image description here
After the cluster is successfully created, the file is generated:

Insert image description here

View the mycatCluster.cluster file

{
	"clusterType":"MASTER_SLAVE",
	"heartbeat":{
		"heartbeatTimeout":1000,
		"maxRetryCount":3,
		"minSwitchTimeInterval":300,
		"showLog":false,
		"slaveThreshold":0.0
	},
	"masters":[
		"mycatWrite"
	],
	"maxCon":2000,
	"name":"mycatCluster",
	"readBalanceType":"BALANCE_ALL",
	"replicas":[
		"mycatRead"
	],
	"switchType":"SWITCH"
}

6. After completion, perform operation testing in mycat.

Precautions:

When logging into mycat2 using cmd, the data inserted into the table is garbled. You can specify the character set when logging in:
Insert image description here

When logging in to mycat in cmd, use mysql -uroot -p instead of using mysql
--default-character-set=utf8 -uroot -p123456 -P8066 to log in
***

Guess you like

Origin blog.csdn.net/FORLOVEHUAN/article/details/128919007