搭建MyCat2一主一从的MySQL读写分离

搭建MyCat2一主一从的MySQL读写分离

  • 搭建MyCat2读写分离
    • 搭建MySQL主从复制
    • 安装MyCat2
    • 配置逻辑库
    • 添加数据源
    • 更新集群配置
    • 验证读写分离

搭建MyCat2读写分离

通过Mycat2与MySQL的主从同步复制配合就可以搭建MySQL数据库的读写分离,这里搭建MyCat2一主一从的MySQL读写分离。

搭建MySQL主从复制

搭建MySQL主从复制参考:在Windows环境与Linux环境下搭建MySQL主从同步

1.做好主从的MySQL中有mydb这个库,且已经存在表和数据

2.登录主库:mysql -h localhost -P3306 -uroot -p

mysql> CREATE DATABASE mydb;
Query OK, 1 row affected (0.00 sec)

mysql> use mydb;
Database changed
mysql> CREATE TABLE mytb(id INT,name VARCHAR(30));
Query OK, 0 rows affected (0.02 sec)

mysql> INSERT INTO mytb VALUES(1,'mycat');
Query OK, 1 row affected (0.01 sec)

3.登录从库:mysql -h localhost -P3307 -uroot -p

mysql> use mydb;
Database changed
mysql> select * from mytb;
+------+---------+
| id   |  name   |
+------+---------+
|    1 |  mycat	 |
+------+---------+
1 row in set (0.00 sec)

安装MyCat2

安装MyCat2参考:MyCat2的介绍与安装以及基本使用

配置逻辑库

登录Mycat:mysql -uroot -p123456 -h localhost -P 8066,创建逻辑库

mysql> create database mydb;
Query OK, 1 row affected (0.00 sec)

执行create database mydb;后,将在/mycat/conf/schemas/自动生成mydb.schema.json文件

{
	"customTables":{},
	"globalTables":{},
	"normalProcedures":{},
	"normalTables":{},
	"schemaName":"mydb",
	"shardingTables":{},
	"views":{}
}
customTables:mycat默认的表的配置

globalTables:全局表的配置

shardingTables:分片表的配置

normalTables:普通表的配置

修改schema的配置,指定mydb逻辑库默认的targetName,mycat会自动加载mydb下已经有的物理表或者视图作为单表

{
	"customTables":{},
	"globalTables":{},
	"normalProcedures":{},
	"normalTables":{},
	"schemaName":"mydb",
	"targetName":"prototype"
	"shardingTables":{},
	"views":{}
}

查看修改之后的schema的配置,发现已经自动加载了相关信息到normalTables中

{
	"customTables":{},
	"globalTables":{},
	"normalProcedures":{},
	"normalTables":{
		"mytb":{
			"createTableSQL":"CREATE TABLE `mydb`.`mytb` (\n\t`id` int(11) DEFAULT NULL,\n\t`name` varchar(30) DEFAULT NULL\n) ENGINE = InnoDB CHARSET = utf8",
			"locality":{
				"schemaName":"mydb",
				"tableName":"mytb",
				"targetName":"prototype"
			}
		}
	},
	"schemaName":"mydb",
	"shardingTables":{},
	"views":{}
}

登录Mycat:mysql -uroot -p123456 -h localhost -P 8066,进行查询

mysql> use mydb;
Database changed
mysql> SELECT * FROM mytb;
+------+-------+
| id   | name  |
+------+-------+
|    1 | mycat |
+------+-------+
1 row in set (0.03 sec)

此时,架构如下:

应用程序
mycat
MySQL

添加数据源

登录Mycat:mysql -uroot -p123456 -h localhost -P 8066,使用注解方式添加数据源

添加主库数据源

mysql> /*+ mycat:createDataSource{ "name":"write","url":"jdbc:mysql://IP:3306/mydb?useSSL=false&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true", "user":"mycat","password":"123456" } */;
Query OK, 0 rows affected (0.00 sec)

添加从库数据源

mysql> /*+ mycat:createDataSource{ "name":"red","url":"jdbc:mysql://IP:3307/mydb?useSSL=false&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true", "user":"root","password":"123456" } */;
Query OK, 0 rows affected (0.00 sec)

查询配置数据源结果

mysql> /*+ mycat:showDataSources{} */;
Query OK, 0 rows affected (0.00 sec)

搭建MyCat2一主一从的MySQL读写分离_第1张图片
通过注释命名添加数据源后,在对应目录会生成相关配置文件,查看数据源配置文件:mycat/conf/datasources

[root@administrator mycat]# ls conf/datasources
prototypeDs.datasource.json  red.datasource.json  write.datasource.json

更新集群配置

使用mycat自带的默认集群:prototype,对其修改更新即可

/*! mycat:createCluster{"name":"prototype","masters":["write"],"replicas":["red"]} */;

查看配置集群信息

/*+ mycat:showClusters{} */;

搭建MyCat2一主一从的MySQL读写分离_第2张图片
查看集群配置文件,发现集群配置信息已经更新

vim /usr/local/mycat/conf/clusters/prototype.cluster.json
{
	"clusterType":"MASTER_SLAVE",
	"heartbeat":{
		"heartbeatTimeout":1000,
		"maxRetryCount":3,
		"minSwitchTimeInterval":300,
		"showLog":false,
		"slaveThreshold":0.0
	},
	"masters":[
		"write"
	],
	"maxCon":2000,
	"name":"prototype",
	"readBalanceType":"BALANCE_ALL",
	"replicas":[
		"red"
	],
	"switchType":"SWITCH"
}

readBalanceType:查询负载均衡策略

BALANCE_ALL(默认值):获取集群中所有数据源

BALANCE_ALL_READ:获取集群中允许读的数据源

BALANCE_READ_WRITE:获取集群中允许读写的数据源,但允许读的数据源优先

BALANCE_NONE:获取集群中允许写数据源,即主节点中选择

switchType:控制主从切换

NOT_SWITCH:不进行主从切换

SWITCH:进行主从切换

此时,架构如下

应用程序
mycat
主MySQL
从MySQL

验证读写分离

修改MySQL的配置文件:my.cnf,设置logbin格式binlog_format=STATEMENT,重启MySQL,确保此时主从复制正常

登录MyCat:mysql -uroot -p123456 -h IP -P 8066,向数据表插入系统变量值,以此造成主从数据不一致,便于验证读写分离。

mysql> use mydb;
Database changed
mysql> INSERT INTO mytb VALUES(2,@@hostname);

登录主库:mysql -uroot -h IP -P 3306 -p查看表数据

mysql> SELECT * FROM mytb;
+------+---------------+
| id   | name          |
+------+---------------+
|    1 | mycat         |
|    2 | administrator |
+------+---------------+
2 rows in set (0.01 sec)

登录从库:mysql -uroot -h IP -P 3307 -p查看表数据

mysql> use mydb;
Database changed
mysql> select * from mytb;
+------+--------------+
| id   | name         |
+------+--------------+
|    1 | mycat        |
|    2 | 5990c4387bd9 |
+------+--------------+
2 rows

登录MyCat:mysql -uroot -p123456 -h IP -P 8066,查询验证

mysql> SELECT * FROM mytb;
+------+---------------+
| id   | name          |
+------+---------------+
|    1 | mycat         |
|    2 | administrator |
+------+---------------+
2 rows in set (0.01 sec)

mysql> SELECT * FROM mytb;
+------+--------------+
| id   | name         |
+------+--------------+
|    1 | mycat        |
|    2 | 5990c4387bd9 |
+------+--------------+
2 rows in set (0.01 sec)

你可能感兴趣的:(#,MySQL,mysql,数据库,中间件)