mycat2读写分离

mtcat2安装

1.下载相关安装包

http://dl.mycat.org.cn/2.0/install-template/mycat2-install-template-1.20.zip
http://dl.mycat.org.cn/2.0/1.21-release/

2.将jar放到mycat的lib下面,将mycat文件夹放到linux/usr/local/目录下,并修改权限
mycat2读写分离_第1张图片

mtcat2启动

修改mycat的prototype的配置

1.修改数据源配置

cd /usr/local/mycat/conf/datasources/
vim prototypeDs.datasource.json

mycat2读写分离_第2张图片
2.启动命令

cd mycat/bin
./mycat start  启动
./mycat status  查看启动状态
./mycat stop   停止
./mycat console  前台运行
./mycat restart  重启服务

3.登录
此登录方式用于维护管理

mysql -uroot -pLutu6688@ -P 9066

登录数据窗口,使用这种方式

mysql -uroot -pLutu6688@ -P 8066

mtcat2搭建主从复制

主机配置
修改配置文件 vim /etc/my.cnf
#主服务器唯一id

[mysqld]
server-id=1

#启用二进制日志

log-bin=mysql-bin

#设置不要复制的数据库(可设置多个)

binlog-ignore-db=mysql
binlog-ignore-db=information_schema

#设置需要复制的数据库

binlog-ignore-db=需要复制的主数据库名字

#设置logbin格式

binlog_format=STATEMENT

从机配置
修改配置文件 vim /etc/my.cnf
#从服务器唯一id

[mysqld]
server-id=2

#启用中继日志

relay-log=mysql-relay

如何停止从服务复制功能

stop slave

如何从新配置主从功能

stop slave
reset master

Navicat连接Mycat

修改/usr/local/mycat/conf/users/root.user.json中的用户名密码去连接

mtcat2读写分离配置

1.登录Mycat,创建逻辑库,配置数据源
#在Mycat里创建数据库test_sync
#创建db2逻辑库

create database test_sync

修改test_sync.json指定数据源"targetName":“prototype”,配置主机数据源

vim /usr/local/mycat/conf/schemas/test_sync.schema.json 添加"targetName":"prototype"

mycat2读写分离_第3张图片
3.使用注解方式添加数据源
写数据源

/*+ mycat:createDataSource{ "name" :"rwsepw" , "url":"jdbc:mysql://192.168.0.110:3306/test_sync?useSSL=false&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true","user":"root","password" :"Lutu6688@"} */;

读数据源

/*+ mycat:createDataSource{ "name" :"rwsepr" , "url":"jdbc:mysql://192.168.0.141:3306/test_sync?useSSL=false&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true","user":"root","password" :"Lutu6688@"} */;

查询数据源

/*+ mycat:showDataSources{} */;

4.更新集群信息(此处命令有问题,可进入/usr/local/mycat/conf/clusters目录修改prototype.cluster.json文件如下图所示)

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

查看集群信息

/*+ mycat:showClusters{} */;

mycat2读写分离_第4张图片
readBalanceType:
查询负载均衡策略
BALANCE_ALL(默认)
获取集群中所有数据源
BALANCE_ALL_READ
获取集群中允许读的数据源
BALANCE_READ_WRITE
获取集群中允许读写的数据源,但允许读的数据源优先
BALANCE_NONE
获取集群中允许写数据源,即主节点中选择

switchType
NOT_SWITCH:不进行主从切换
SWITCH:进行主从切换

重启mycat并验证

mysql双主双从

一个主机m1用于处理所有写请求,它的从机s1和另一台主机m2还有它的从机s2负责所有读请求。当m1主机宕机后,m2主机负责写请求,m1,m2互为备机。

编号 角色 ip地址 机器名
1 Master1 192.168.140.100 atguigu01
2 Slave1 192.168.140.99 atguigu02
3 Master2 192.168.140.98 atguigu03
4 Slave2 192.168.140.97 atguigu04

1.搭建mysql数据库的主从复制(双主双从)
①->Master1修改配置

vim /etc/my.cnf

[mysqld]
server-id=1

#启用二进制日志

log-bin=mysql-bin

#设置不要复制的数据库(可设置多个)

binlog-ignore-db=mysql
binlog-ignore-db=information_schema

#设置需要复制的数据库

binlog-ignore-db=需要复制的主数据库名字

#设置logbin格式

binlog_format=STATEMENT

#在作为从数据库的时候,有写入操作也要更新二进制日志文件

log-slave-updates

#表示自增长字段每次递增的量,指自增字段的起始值,其默认值是1,取值范围是1…65535

auto-increment-increment=2

#表示自增长字段从哪个数开始,指字段一次递增多少,它的取值范围是1…65535

auto-increment-offset=1

②->Master2修改配置

vim /etc/my.cnf

[mysqld]
server-id=3

#启用二进制日志

log-bin=mysql-bin

#设置不要复制的数据库(可设置多个)

binlog-ignore-db=mysql
binlog-ignore-db=information_schema

#设置需要复制的数据库

binlog-ignore-db=需要复制的主数据库名字

#设置logbin格式

binlog_format=STATEMENT

#在作为从数据库的时候,有写入操作也要更新二进制日志文件

log-slave-updates

#表示自增长字段每次递增的量,指自增字段的起始值,其默认值是1,取值范围是1…65535

auto-increment-increment=2

#表示自增长字段从哪个数开始,指字段一次递增多少,它的取值范围是1…65535

auto-increment-offset=2

③->Slave1从机修改配置
修改配置文件

vim /etc/my.cnf

server-id=2
relay-log=mysql-relay  启用中继日志

④->Slave2从机修改配置
修改配置文件

vim /etc/my.cnf

server-id=4
relay-log=mysql-relay  启用中继日志

1.搭建mysql数据库的主从复制(双主双从)

2.双主双从配置
1.双主,双从重启mysql

systemctl restart mysqld

2.账号相同,4个库账号密码都是root/Lutu6688@
3.slave1复制master1,slave2复制master2
①master1主机查看状态
mysql> show master status;

mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000002 |      674 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

#启动服务

start slave

#查看服务器状态

show slave status\G;
下面两个参数都是yes说明成功
Slave IO Running:yes
Slave SQL Running::yes

#Slave1从机上命令

CHANGE MASTER TO MASTER_HOST='192.168.140.100', MASTER_USER='root', MASTER_PASSWORD='Lutu6688@', MASTER_LOG_FILE='binlog.000002', MASTER_LOG_POS=674;

①master2主机查看状态
mysql> show master status;

mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000003 |      675 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

#Slave2从机上命令

CHANGE MASTER TO MASTER_HOST='192.168.140.98', MASTER_USER='root', MASTER_PASSWORD='Lutu6688@', MASTER_LOG_FILE='binlog.000003', MASTER_LOG_POS=675;

#启动服务

start slave

#查看服务器状态

show slave status\G;

3.双主互为主备
Master2复制Master1,Master1复制Master2
1.Master2复制Master1,查看Master1状态

mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000002 |      674 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

#Master2从机上命令

CHANGE MASTER TO MASTER_HOST='192.168.140.100', MASTER_USER='root', MASTER_PASSWORD='Lutu6688@', MASTER_LOG_FILE='binlog.000002', MASTER_LOG_POS=674;

#启动服务

start slave

#查看服务器状态

show slave status\G;

2.Master1复制Master2,查看Master2状态

mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000003 |      675 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

#Master2从机上命令

CHANGE MASTER TO MASTER_HOST='192.168.140.98, MASTER_USER='root', MASTER_PASSWORD='Lutu6688@', MASTER_LOG_FILE='binlog.000003', MASTER_LOG_POS=675;

#启动服务

start slave

#查看服务器状态

show slave status\G;

mycat双主双从配置

1.使用注解方式添加数据源
写数据源

/*+ mycat:createDataSource{ "name" :"rwsepw2" , "url":"jdbc:mysql://192.168.0.98:3306/test_sync?useSSL=false&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true","user":"root","password" :"Lutu6688@"} */;

读数据源

/*+ mycat:createDataSource{ "name" :"rwsepr2" , "url":"jdbc:mysql://192.168.0.97:3306/test_sync?useSSL=false&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true","user":"root","password" :"Lutu6688@"} */;

2.修改集群

vim /usr/local/mycat/conf/clusters/prototype.cluster.json

#修改masters和replicas

"masters":[
		"rwsepw","rwsepw2"
	]
	
"replicas":[
		"rwsepw2","rwsepr","rwsepr2"
	]

#进入bin目录下重启mycat

./mycat restart

mycat2分库分表

1.在一主一从的背景下实现,修改datasource文件下文件,修改clusters下的主从文件
2.现有主从的服务器假设分别为192.168.0.100和192.168.0.99
3.添加数据源

/*+ mycat:createDataSource{ "name" :"dw0" , "url":"jdbc:mysql://192.168.0.100:3306","user":"root","password" :"Lutu6688@"} */;

/*+ mycat:createDataSource{ "name" :"dr0" , "url":"jdbc:mysql://192.168.0.100:3306","user":"root","password" :"Lutu6688@"} */;

/*+ mycat:createDataSource{ "name" :"dw1" , "url":"jdbc:mysql://192.168.0.99:3306","user":"root","password" :"Lutu6688@"} */;

/*+ mycat:createDataSource{ "name" :"dr1" , "url":"jdbc:mysql://192.168.0.99:3306","user":"root","password" :"Lutu6688@"} */;

4.添加集群配置
把新添加的数据源配置成集群

/*! mycat:createCluster{"name":"c0","masters":["dw0"],"replicas":["dro"]} */;
/*! mycat:createCluster{"name":"c1","masters":["dw1"],"replicas":["dr1"]} */;

查看集群信息

cd /usr/local/mycat/conf/clusters

5.创建逻辑库bd1

CREATE DATABASE db1;

创建全局表BROADCAST(广播表)

CREATE TABLE db1.`division_city` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `province_code` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '省级行政区划代码',
  `city_code` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '市级行政区划代码',
  `city_name` text CHARACTER SET utf8 COLLATE utf8_general_ci COMMENT '城市名称',
  `is_gengeral` int DEFAULT NULL COMMENT '是否直辖市 0:否 1:是',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=380 DEFAULT CHARSET=utf8mb3 COLLATE=utf8_croatian_ci BROADCAST;

7.查看db1.schema.json文件

分片表

CREATE TABLE db1.`orders` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `order_type` INT,
  `customer_id` INT,
  amount DECIMAL(10,2),
  PRIMARY KEY (`id`),
  KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT  CHARSET=utf8
dbpartition BY mod_hash(customer_id) tbpartition BY mod_hash(customer_id) tbpartitions  1 tbpartitions 2;
//注释 
dbpartition:数据库根据什么分片
tbpartition :表根据什么分片
tbpartitions  1 tbpartitions 2:表分一片,库分两片

ER表

CREATE TABLE db1.`order_detail` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `detail` VARCHAR(2000),
  `order_id` INT,
  PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT  CHARSET=utf8
dbpartition BY mod_hash(order_id) tbpartition BY mod_hash(order_id) tbpartitions  1 tbpartitions 2;

查看ER分组关系

/*+ mycat:showErGroup{} */;

常用分片规则
1.HASH分片算法默要求c为前缀,数字为后缀,c0为第一个节点,c1为第二个节点。

你可能感兴趣的:(mysql)