Building a high-performance database cluster Part 2: MySQL read-write separation (based on mycat2-1.22)

I. Overview

Read-write separation is a common database architecture, which generally consists of one master and multiple slaves. In special scenarios, there are also multi-master and multiple slave architectures.

Regardless of the architecture, there are multiple data sources for the application, increasing the complexity of the code. If combined with mycat, it can shield the complexity of the database and provide a unified entrance for applications.

2. Configuration

mycat2 的配置逻辑架构大致如下:

Insert image description here
The configuration files corresponding to mycat are as follows:
Insert image description here
This article uses the unique comments of mycat2 to separate the reading and writing of related configurations.

premise:

1. Two mysql instances, master and slave, have been configured.
2. mycat2 instance.

step:

1. Use Navicat to connect to mycat2
Insert image description here

2. Add the mysql read-write database data source.
Copy the following script and execute it in the navicat query:

/*+ mycat:createDataSource{
	"dbType":"mysql",
	"idleTimeout":60000,
	"initSqls":[],
	"initSqlsGetConnection":true,
	"instanceType":"READ_WRITE",
	"maxCon":1000,
	"maxConnectTimeout":3000,
	"maxRetryCount":5,
	"minCon":1,
	"name":"write01",
	"password":"root",
	"type":"JDBC",
	"url":"jdbc:mysql://192.168.0.201:3306/pmonitor?useUnicode=true&serverTimezone=UTC&characterEncoding=UTF-8",	// 这里要加上数据库名pmonitor,否则mycat在执行delete语句时会报错找不到database
	"user":"root",
	"weight":0
} */;
/*+ mycat:createDataSource{
	"dbType":"mysql",
	"idleTimeout":60000,
	"initSqls":[],
	"initSqlsGetConnection":true,
	"instanceType":"READ_WRITE",
	"maxCon":1000,
	"maxConnectTimeout":3000,
	"maxRetryCount":5,
	"minCon":1,
	"name":"read01",
	"password":"root",
	"type":"JDBC",
	"url":"jdbc:mysql://192.168.0.202:3306/pmonitor?useUnicode=true&serverTimezone=UTC&characterEncoding=UTF-8",	// 这里要加上数据库名pmonitor,否则mycat在执行delete语句时会报错找不到database
	"user":"root",
	"weight":0
} */;

The created data source can be queried in the queryer by executing the following statement

/*+ mycat:showDataSources {} */ 

You can also view the newly created data source configuration file through the configuration folder.
Insert image description here
Subsequent configuration changes can directly modify this configuration file.

vi read01.datasource.json
vi write01.datasource.json

Insert image description here

3. Create a read-write cluster
and copy the following statements to the queryer for execution:

/*+ mycat:createCluster{
	"clusterType":"MASTER_SLAVE",
	"heartbeat":{
		"heartbeatTimeout":1000,
		"maxRetry":3,
		"minSwitchTimeInterval":300,
		"slaveThreshold":0
	},
	"masters":[
		"write01" // 可以配置多个主节点,在主挂的时候会选一个检测存活的数据源作为主节点
	],
	"maxCon":2000,
	"name":"c0",			// 集群的名字
	"readBalanceType":"BALANCE_ALL",	// 读数据的负责均衡策略:BALANCE_ALL -- 获取集群中所有数据源,BALANCE_ALL_READ -- 获取集群中允许读的数据源, BALANCE_READ_WRITE -- 获取集群中允许读写的数据源,但允许读的数据源优先, BALANCE_NONE -- 获取集群中允许写数据源,即主节点中选择
	"replicas":[
		"read01" // 可以配置多个从节点
	],
	"switchType":"SWITCH"
} */;

You can query the created viewing cluster by executing the following statement in the queryer

/*+ mycat:showClusters {} */ 

You can also view the newly created mycat cluster configuration file through the configuration folder.
Insert image description here
Subsequent parameter changes can directly modify this file.

vi co.cluster.json

Insert image description here

4. Create a logic library

/*+ mycat:createSchema{
	"customTables":{},
	"globalTables":{},
	"normalTables":{},
	"schemaName":"pmonitor",		// 与物理库中的数据库名保持一致,注:数据库名不能有符号“-”
	"shardingTables":{},
	"targetName":"c0"		// 	注意 targetName 指的是集群的名字
} */;

You can query the created logical library by executing the following statement in the queryer

/*+ mycat:showSchemas {} */ 

You can also view the newly created mycat cluster configuration file through the configuration folder.
Insert image description here

5. Connection test
After the above execution is successful, reconnect mycat to see the logical library, and open it to see all tables in the library (there is no need to create logical tables for each physical table separately, automatic mapping).

Guess you like

Origin blog.csdn.net/hualinger/article/details/131561167