1、MyCAT基础架构准备
MyCAT基础架构图
1.1 MyCAT基础架构准备
1.1.1 环境准备:
两台虚拟机 db01(10.0.0.51) db02(10.0.0.52)
每台创建四个mysql实例:3307 3308 3309 3310
1.1.2 删除历史环境:
pkill mysqld
rm -rf /data/330*
mv /etc/my.cnf /etc/my.cnf.bak
1.1.3 创建相关目录初始化数据
mkdir /data/33{07..10}/data -p
mysqld --initialize-insecure --user=mysql --datadir=/data/3307/data --basedir=/application/mysql
mysqld --initialize-insecure --user=mysql --datadir=/data/3308/data --basedir=/application/mysql
mysqld --initialize-insecure --user=mysql --datadir=/data/3309/data --basedir=/application/mysql
mysqld --initialize-insecure --user=mysql --datadir=/data/3310/data --basedir=/application/mysql
1.1.4 准备配置文件和启动脚本
========db01==============
cat >/data/3307/my.cnf</data/3308/my.cnf</data/3309/my.cnf</data/3310/my.cnf</etc/systemd/system/mysqld3307.service</etc/systemd/system/mysqld3308.service</etc/systemd/system/mysqld3309.service</etc/systemd/system/mysqld3310.service</data/3307/my.cnf</data/3308/my.cnf</data/3309/my.cnf</data/3310/my.cnf</etc/systemd/system/mysqld3307.service</etc/systemd/system/mysqld3308.service</etc/systemd/system/mysqld3309.service</etc/systemd/system/mysqld3310.service<
1.1.5 修改权限,启动多实例
chown -R mysql.mysql /data/*
systemctl start mysqld3307
systemctl start mysqld3308
systemctl start mysqld3309
systemctl start mysqld3310
mysql -S /data/3307/mysql.sock -e "show variables like 'server_id'"
mysql -S /data/3308/mysql.sock -e "show variables like 'server_id'"
mysql -S /data/3309/mysql.sock -e "show variables like 'server_id'"
mysql -S /data/3310/mysql.sock -e "show variables like 'server_id'"
1.1.6 节点主从规划
箭头指向谁是主库
10.0.0.51:3307 <-----> 10.0.0.52:3307
10.0.0.51:3309 ------> 10.0.0.51:3307
10.0.0.52:3309 ------> 10.0.0.52:3307
10.0.0.52:3308 <-----> 10.0.0.51:3308
10.0.0.52:3310 -----> 10.0.0.52:3308
10.0.0.51:3310 -----> 10.0.0.51:3308
1.1.7 分片规划
shard1:
Master:10.0.0.51:3307
slave1:10.0.0.51:3309
Standby Master:10.0.0.52:3307
slave2:10.0.0.52:3309
shard2:
Master:10.0.0.52:3308
slave1:10.0.0.52:3310
Standby Master:10.0.0.51:3308
slave2:10.0.0.51:3310
1.1.8 开始配置
shard1
10.0.0.51:3307 <-----> 10.0.0.52:3307
db02
mysql -S /data/3307/mysql.sock -e "grant replication slave on *.* to repl@'10.0.0.%' identified by '123';"
mysql -S /data/3307/mysql.sock -e "grant all on *.* to root@'10.0.0.%' identified by '123' with grant option;"
db01
mysql -S /data/3307/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='10.0.0.52', MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
mysql -S /data/3307/mysql.sock -e "start slave;"
mysql -S /data/3307/mysql.sock -e "show slave status\G"
db02
mysql -S /data/3307/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='10.0.0.51', MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
mysql -S /data/3307/mysql.sock -e "start slave;"
mysql -S /data/3307/mysql.sock -e "show slave status\G"
10.0.0.51:3309 ------> 10.0.0.51:3307
db01
mysql -S /data/3309/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='10.0.0.51', MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
mysql -S /data/3309/mysql.sock -e "start slave;"
mysql -S /data/3309/mysql.sock -e "show slave status\G"
10.0.0.52:3309 ------> 10.0.0.52:3307
db02
mysql -S /data/3309/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='10.0.0.52', MASTER_PORT=3307, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
mysql -S /data/3309/mysql.sock -e "start slave;"
mysql -S /data/3309/mysql.sock -e "show slave status\G"
shard2
10.0.0.52:3308 <-----> 10.0.0.51:3308
db01
mysql -S /data/3308/mysql.sock -e "grant replication slave on *.* to repl@'10.0.0.%' identified by '123';"
mysql -S /data/3308/mysql.sock -e "grant all on *.* to root@'10.0.0.%' identified by '123' with grant option;"
db02
mysql -S /data/3308/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='10.0.0.51', MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
mysql -S /data/3308/mysql.sock -e "start slave;"
mysql -S /data/3308/mysql.sock -e "show slave status\G"
db01
mysql -S /data/3308/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='10.0.0.52', MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
mysql -S /data/3308/mysql.sock -e "start slave;"
mysql -S /data/3308/mysql.sock -e "show slave status\G"
10.0.0.52:3310 -----> 10.0.0.52:3308
db02
mysql -S /data/3310/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='10.0.0.52', MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
mysql -S /data/3310/mysql.sock -e "start slave;"
mysql -S /data/3310/mysql.sock -e "show slave status\G"
10.0.0.51:3310 -----> 10.0.0.51:3308
db01
mysql -S /data/3310/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='10.0.0.51', MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='123';"
mysql -S /data/3310/mysql.sock -e "start slave;"
mysql -S /data/3310/mysql.sock -e "show slave status\G"
1.1.9 检测主从状态
mysql -S /data/3307/mysql.sock -e "show slave status\G"|grep Running:
mysql -S /data/3308/mysql.sock -e "show slave status\G"|grep Running:
mysql -S /data/3309/mysql.sock -e "show slave status\G"|grep Running:
mysql -S /data/3310/mysql.sock -e "show slave status\G"|grep Running:
1.1.10 如果中间出现错误,在每个节点进行执行以下命令(2.9 状态正确,忽略此步骤)
mysql -S /data/3307/mysql.sock -e "stop slave; reset slave all;"
mysql -S /data/3308/mysql.sock -e "stop slave; reset slave all;"
mysql -S /data/3309/mysql.sock -e "stop slave; reset slave all;"
mysql -S /data/3310/mysql.sock -e "stop slave; reset slave all;"
1.2 Mycat的安装
MySQL分布式架构介绍:
1.2.1 安装java环境
yum install -y java
1.2.2 上传到/application 并解压
略
官网:http://www.mycat.io/
1.2.3 配置文件介绍
conf:
schema.xml 主配置文件(读写分离、高可用、分布式策略定制、节点控制)
server.xml mycat软件本身相关的配置
rule.xml 分片规则配置文件,记录分片规则列表、使用方法等
xxxx.txt 分片参数功能有关
logs目录:
wrapper.log ---->mycat启动日志
mycat.log ---->mycat详细工作日志
1.2.4 启动mycat
[root@db02 ~]# vim /etc/profile
export PATH=/application/mycat/bin:$PATH
[root@db02 ~]# source /etc/profile
[root@db02 ~]# mycat start
[root@db02 ~]# mysql -uroot -p123456 -h 10.0.0.52 -P8066
1.3 测试数据准备
## db01:
mysql -S /data/3307/mysql.sock
grant all on *.* to root@'10.0.0.%' identified by '123';
source /root/world.sql
mysql -S /data/3308/mysql.sock
grant all on *.* to root@'10.0.0.%' identified by '123';
source /root/world.sql
1.4 核心配置文件使用介绍(schema.xml)
(1)逻辑库定义
(2)数据节点定义
(3)数据主机定义
select user()
1.5 Mycat实现读写分离功能
(1)配置conf文件--schema.xml
[root@db02 /application/mycat/conf]# cat schema.xml
select user()
(2)Mycat重启生效:
mycat restart
(3)模拟测试
[root@db02 /application/mycat/conf]# mysql -uroot -p123456 -h 10.0.0.52 -P8066
mysql> select @@server_id; ----模拟查询
mysql> begin; select @@server_id;commit; ---模拟写入
1.6 读写分离+高可用功能
[root@db01 conf]# mv schema.xml schema.xml.rw
[root@db01 conf]# vim schema.xml
select user()
真正的 writehost:负责写操作的writehost
standby writeHost :和readhost一样,只提供读服务
当写节点宕机后,后面跟的readhost也不提供服务,这时候standby的writehost就提供写服务,
后面跟的readhost提供读服务
。
测试:
mysql -uroot -p123456 -h 127.0.0.1 -P 8066
show variables like 'server_id';
读写分离测试
mysql -uroot -p -h 127.0.0.1 -P8066
show variables like 'server_id';
show variables like 'server_id';
show variables like 'server_id';
begin;
show variables like 'server_id';
对db01 3307节点进行关闭和启动,测试读写操作
1.7 配置中的属性介绍
1.7.1 balance属性
负载均衡类型,目前的取值有3种:
(1) balance="0", 不开启读写分离机制,所有读操作都发送到当前可用的writeHost上。
(2) balance="1",全部的readHost与standby writeHost参与select语句的负载均衡,简单的说,
当双主双从模式(M1->S1,M2->S2,并且M1与 M2互为主备),正常情况下,M2,S1,S2都参与select语句的负载均衡。
(3) balance="2",所有读操作都随机的在writeHost、readhost上分发。
1.7.2 writeType属性
负载均衡类型,目前的取值有2种:
(1)writeType="0", 所有写操作发送到配置的第一个writeHost,
第一个挂了切到还生存的第二个writeHost,重新启动后已切换后的为主,切换记录在配置文件中:dnindex.properties .
(2)writeType=“1”,所有写操作都随机的发送到配置的writeHost,但不推荐使用
1.7.3 switchType属性
-1 表示不自动切换
1 默认值,自动切换
2 基于MySQL主从同步的状态决定是否切换 ,心跳语句为 show slave status
1.7.4 datahost其他配置
maxCon="1000":最大的并发连接数
minCon="10" :mycat在启动之后,会在后端节点上自动开启的连接线程
tempReadHostAvailable="1"
这个一主一从时(1个writehost,1个readhost时),可以开启这个参数,如果2个writehost,2个readhost时
select user() 监测心跳
1.8 垂直分表
1.8.1 修改schema.xml配置文件
mv schema.xml schema.xml.ha
vim schema.xml
select user()
select user()
1.8.2 创建测试库和表:
mysql -S /data/3307/mysql.sock -e "create database taobao charset utf8;"
mysql -S /data/3308/mysql.sock -e "create database taobao charset utf8;"
mysql -S /data/3307/mysql.sock -e "use taobao;create table user(id int,name varchar(20))";
mysql -S /data/3308/mysql.sock -e "use taobao;create table order_t(id int,name varchar(20))"
1.8.3 重启MyCat
mycat restart
1.8.4 进入MyCat测试,添加数据测试
mysql -uroot -p123456 -h 10.0.0.51 -P 8066 --这里的ip地址是MyCat安装的那个虚拟机,就是哪个虚拟机的ip地址
mysql -S /data/3307/mysql.sock -e "use taobao;show tables;"
mysql -S /data/3308/mysql.sock -e "use taobao;show tables;"
1.9 MyCAT核心特性——分片(水平拆分)
分片:对一个"bigtable",比如说t3表
(1)行数非常多,800w
(2)访问非常频繁
分片的目的:
(1)将大数据量进行分布存储
(2)提供均衡的访问路由
分片策略:
范围 range 800w 1-400w 400w01-800w
取模 mod 取余数
枚举
哈希 hash
时间 流水
优化关联查询
全局表
ER分片
1.9.1 范围分片
(1)范围分片参数配置
比如说t3表
(1)行数非常多,2000w(1-1000w:sh1 1000w01-2000w:sh2)
(2)访问非常频繁,用户访问较离散
cp schema.xml schema.xml.1
==========vim schema.xml
===========vim rule.xml
id
rang-long
autopartition-long.txt
===========vim autopartition-long.txt
0-10=0
11-20=1
(2)创建测试表:
mysql -S /data/3307/mysql.sock -e "use taobao;create table t3 (id int not null primary key auto_increment,name varchar(20) not null);"
mysql -S /data/3308/mysql.sock -e "use taobao;create table t3 (id int not null primary key auto_increment,name varchar(20) not null);"
(3)重启mycat------>mycat restart
(4)登录mycat插入数据
mysql -uroot -p123456 -h 127.0.0.1 -P 8066
insert into t3(id,name) values(1,'a');
insert into t3(id,name) values(2,'b');
insert into t3(id,name) values(3,'c');
insert into t3(id,name) values(4,'d');
insert into t3(id,name) values(11,'aa');
insert into t3(id,name) values(12,'bb');
insert into t3(id,name) values(13,'cc');
insert into t3(id,name) values(14,'dd');
(5)分别登入后端节点测试
mysql -S /data/3307/mysql.sock -e "use taobao;select * from t5;"
mysql -S /data/3308/mysql.sock -e "use taobao;select * from t5;"
1.9.2 取模分片(mod-long)
取余分片方式:分片键(一个列)与节点数量进行取余,得到余数,将数据写入对应节点
(1)取模分片参数配置
=======vim schema.xml
========vim rule.xml
id
mod-long
2
(2)创建测试表:
mysql -S /data/3307/mysql.sock -e "use taobao;create table t4 (id int not null primary key auto_increment,name varchar(20) not null);"
mysql -S /data/3308/mysql.sock -e "use taobao;create table t4 (id int not null primary key auto_increment,name varchar(20) not null);"
(3)重启mycat
mycat restart
(4)模拟数据测试:
mysql -uroot -p123456 -h10.0.0.52 -P8066
use TESTDB
insert into t4(id,name) values(1,'a');
insert into t4(id,name) values(2,'b');
insert into t4(id,name) values(3,'c');
insert into t4(id,name) values(4,'d');
(5)分别登录后端节点查询数据
mysql -S /data/3307/mysql.sock -e "use taobao;select * from t4;"
mysql -S /data/3308/mysql.sock -e "use taobao;select * from t4;"
1.9.3 枚举分片
t5 表
id name telnum
1 bj 1212
2 sh 22222
3 bj 3333
4 sh 44444
5 bj 5555
(1)枚举分片参数配置
=====vim schema.xml
=====vim rule.xml
name
hash-int
partition-hash-int.txt
1
======partition-hash-int.txt 配置:
bj=0
sh=1
DEFAULT_NODE=1
columns 标识将要分片的表字段,algorithm 分片函数, 其中分片函数配置中,mapFile标识配置文件名称
(2)准备测试环境
mysql -S /data/3307/mysql.sock -e "use taobao;create table t5 (id int not null primary key auto_increment,name varchar(20) not null);"
mysql -S /data/3308/mysql.sock -e "use taobao;create table t5 (id int not null primary key auto_increment,name varchar(20) not null);"
(3)重启mycat
mycat restart
(4)模拟数据测试
mysql -uroot -p123456 -h10.0.0.52 -P8066
use TESTDB
insert into t5(id,name) values(1,'bj');
insert into t5(id,name) values(2,'sh');
insert into t5(id,name) values(3,'bj');
insert into t5(id,name) values(4,'sh');
insert into t5(id,name) values(5,'tj');
(5)分别登录后端节点查询数据
mysql -S /data/3307/mysql.sock -e "use taobao;select * from t5;"
mysql -S /data/3308/mysql.sock -e "use taobao;select * from t5;"
1.9.4 Mycat全局表
a b c d
join
t
select t1.name ,t.x from t1
join t
select t2.name ,t.x from t2
join t
select t3.name ,t.x from t3
join t
使用场景:
如果你的业务中有些数据类似于数据字典,比如配置文件的配置,
常用业务的配置或者数据量不大很少变动的表,这些表往往不是特别大,
而且大部分的业务场景都会用到,那么这种表适合于Mycat全局表,无须对数据进行切分,
要在所有的分片上保存一份数据即可,Mycat 在Join操作中,业务表与全局表进行Join聚合会优先选择相同分片内的全局表join,
避免跨库Join,在进行数据插入操作时,mycat将把数据分发到全局表对应的所有分片执行,在进行数据读取时候将会随机获取一个节点读取数据。
(1)Mycat全局表参数配置
=====vim schema.xml
(2)后端数据准备
mysql -S /data/3307/mysql.sock -e "use taobao;create table t_area (id int not null primary key auto_increment,name varchar(20) not null);"
mysql -S /data/3308/mysql.sock -e "use taobao;create table t_area (id int not null primary key auto_increment,name varchar(20) not null);"
(3)重启mycat
mycat restart
(4)模拟数据测试:
mysql -uroot -p123456 -h10.0.0.52 -P8066
use TESTDB
insert into t_area(id,name) values(1,'a');
insert into t_area(id,name) values(2,'b');
insert into t_area(id,name) values(3,'c');
insert into t_area(id,name) values(4,'d');
(5)分别登录后端节点查询数据
mysql -S /data/3307/mysql.sock -e "use taobao;select * from t_area;"
mysql -S /data/3308/mysql.sock -e "use taobao;select * from t_area;"
2.9.5 ER分片
有一类业务,例如商品(goods)跟商品明细(goods_detail),明细表会依赖于商品,也就是说会存在表的主
从关系,这类似业务的切分可以抽象出合适的切分规则,比如根据商品 ID 切分,总之部分业务总会可以抽象出父子关系的表。这类表适用于 ER 分片表,子表的记录与所关联的父表记录存放在同一个数据分片上,避免数据 Join 跨库操作。
goods join goods_detail
on goods.id=goods_detail.good_id
(1)ER分片参数配置
=====vim schema.xml
(2)后端数据表准备
mysql -S /data/3307/mysql.sock -e "use taobao;create table goods (id int not null primary key auto_increment,name varchar(20) not null);"
mysql -S /data/3308/mysql.sock -e "use taobao;create table goods (id int not null primary key auto_increment,name varchar(20) not null);"
mysql -S /data/3307/mysql.sock -e "use taobao;create table goods_detail (detail_id int not null primary key auto_increment,name varchar(20) not null,goods_id int not null);"
mysql -S /data/3308/mysql.sock -e "use taobao;create table goods_detail (detail_id int not null primary key auto_increment,name varchar(20) not null,goods_id int not null);"
(3)重启mycat
mycat restart
(4)模拟数据测试:
mysql -uroot -p123456 -h10.0.0.51 -P8066
===========goods
use TESTDB
insert into goods(id,name) values(1,'dn');
insert into goods(id,name) values(2,'sj');
insert into goods(id,name) values(3,'yf');
insert into goods(id,name) values(4,'bk');
===========goods_detail
use TESTDB
insert into goods_detail(detail_id,name,goods_id) values(1,'电脑',1);
insert into goods_detail(detail_id,name,goods_id) values(2,'500元',1);
insert into goods_detail(detail_id,name,goods_id) values(3,'手机',2);
insert into goods_detail(detail_id,name,goods_id) values(4,'红色',2);
insert into goods_detail(detail_id,name,goods_id) values(5,'衣服',3);
insert into goods_detail(detail_id,name,goods_id) values(6,'s码',3);
insert into goods_detail(detail_id,name,goods_id) values(7,'书籍',4);
insert into goods_detail(detail_id,name,goods_id) values(8,'Linux方向',4);
(5)分别登录后端节点查询数据
mysql -S /data/3307/mysql.sock -e "use taobao;select * from goods;select * from goods_detail;"
mysql -S /data/3308/mysql.sock -e "use taobao;select * from goods;select * from goods_detail;"
[root@db01 ~]# mysql -S /data/3307/mysql.sock -e "use taobao;select * from goods;select * from goods_detail;"
+----+------+
| id | name |
+----+------+
| 2 | sj |
| 4 | bk |
+----+------+
+-----------+-------------+----------+
| detail_id | name | goods_id |
+-----------+-------------+----------+
| 3 | 手机 | 2 |
| 4 | 红色 | 2 |
| 7 | 书籍 | 4 |
| 8 | Linux方向 | 4 |
+-----------+-------------+----------+
[root@db01 ~]# mysql -S /data/3308/mysql.sock -e "use taobao;select * from goods;select * from goods_detail;"
+----+------+
| id | name |
+----+------+
| 1 | dn |
| 3 | yf |
+----+------+
+-----------+--------+----------+
| detail_id | name | goods_id |
+-----------+--------+----------+
| 1 | 电脑 | 1 |
| 2 | 500元 | 1 |
| 5 | 衣服 | 3 |
| 6 | s码 | 3 |
+-----------+--------+----------+
[root@db01 ~]#