mycat垂直分库案例实现
我们抽象电商系统四个表;
用户表,商品表,订单表,数据字典表
用户表t_user:
CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '编号',
`userName` varchar(20) DEFAULT NULL COMMENT '用户名',
`password` varchar(20) DEFAULT NULL COMMENT '密码',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
商品表t_product
CREATE TABLE `t_product` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '编号',
`name` varchar(50) DEFAULT NULL COMMENT '商品名称',
`price` decimal(10,0) DEFAULT NULL COMMENT '价格',
`description` text COMMENT '描述',
`stock` int(11) DEFAULT NULL COMMENT '库存量',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
订单表t_order:
CREATE TABLE `t_order` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '编号',
`pId` int(11) DEFAULT NULL COMMENT '商品编号',
`num` int(11) DEFAULT NULL COMMENT '购买数量',
`uId` int(11) DEFAULT NULL COMMENT '用户编号',
`orderDate` datetime DEFAULT NULL COMMENT '下单时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
数据字典表t_datadic:
CREATE TABLE `t_datadic` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '编号',
`name` varchar(20) DEFAULT NULL COMMENT '数据字典名称',
`value` varchar(20) DEFAULT NULL COMMENT '数据字典值',
`remark` varchar(100) DEFAULT NULL COMMENT '备注',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
insert into `t_datadic`(`id`,`name`,`value`,`remark`) values (1,'性别','男',NULL),(2,'性别','女',NULL),(3,'性别','男变女',NULL),(4,'性别','女变男',NULL),(5,'政治面貌','共产党',NULL),(6,'政治面貌','共青团员',NULL),(7,'政治面貌','无党派人士',NULL);
数据字典表作为全局表我们后面再说;
我们分两个库,t_user一个库 t_product和t_order搞一个库,因为有join关联;
t_user 放 db_mall_user库;
t_product和t_order放db_mall_order库;
编号 角色 分配IP
1 db_mall_user库; 172.20.0.7
2 db_mall_order库; 172.20.0.8
3 mycat 172.20.0.9
启动两个mysql
docker run -p 3306:3306 --name db_mall_user -d -v /home/mysql7/mysql.conf.d/:/etc/mysql/mysql.conf.d/ -v /home/mysql7/log/:/var/log --net extnetwork --ip 172.20.0.7 -e MYSQL_ROOT_PASSWORD=123456 镜像ID
docker run -p 3307:3306 --name db_mall_order -d -v /home/mysql8/mysql.conf.d/:/etc/mysql/mysql.conf.d/ -v /home/mysql8/log/:/var/log --net extnetwork --ip 172.20.0.8 -e MYSQL_ROOT_PASSWORD=123456 镜像ID
分别建db_mall_user和db_mall_order数据库
配置schemat.xml
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="TESTDB" checkSQLschema="true" sqlMaxLimit="100">
<table name="t_user" primaryKey="id" dataNode="dn1" />
<table name="t_order" primaryKey="id" dataNode="dn2" />
<table name="t_product" primaryKey="id" dataNode="dn2" />
</schema>
<dataNode name="dn1" dataHost="host1" database="db_mall_user" />
<dataNode name="dn2" dataHost="host2" database="db_mall_order" />
<dataHost name="host1" maxCon="1000" minCon="10" balance="3"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<!-- can have multi write hosts -->
<writeHost host="hostM1" url="172.20.0.7:3306" user="root"
password="123456">
</writeHost>
</dataHost>
<dataHost name="host2" maxCon="1000" minCon="10" balance="3"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<!-- can have multi write hosts -->
<writeHost host="hostM2" url="172.20.0.8:3306" user="root"
password="123456">
</writeHost>
</dataHost>
</mycat:schema>
这里我们要配置2个数据主机 以及两个数据节点,以及逻辑表;
启动mycat
docker run -p 8066:8066 -it -v /home/docker/mycat1/conf/:/home/mycat/conf/ -v /home/docker/mycat1/logs/:/home/mycat/logs/ --net extnetwork --ip 172.20.0.9 镜像ID
工具连接后,我们可以看到有三个表;
我们在插入数据;
insert into `t_user`(`id`,`userName`,`password`) values (1,'java1234','123456'),(2,'jack','123'),(3,'marry','456');
insert into `t_product`(`id`,`name`,`price`,`description`,`stock`) values (1,'耐克鞋2020款001A','998','dfs',50),(2,'阿迪达斯休闲衣服2020Add','388','dfs',20);
insert into `t_order`(`id`,`pId`,`num`,`uId`,`orderDate`) values (1,1,1,2,'2020-03-16 22:10:25'),(2,2,3,3,'2020-03-16 22:10:34');
select * from t_order o left join t_product p on o.pId=p.id
上一篇:mycat垂直分库最佳实践
下一篇:mycat垂直分库优缺点