mycat原理实现mysql_MyCat原理

本文详细介绍了Mycat的工作原理及其在MySQL集群中的应用,包括读写分离、数据分片、全局表等特性。通过实例展示了如何配置和使用Mycat,以及如何在Mycat上进行数据插入和查询,帮助理解Mycat如何实现数据的自动分片和按月分片策略。
摘要由CSDN通过智能技术生成

MyCat原理

Mycat实际上只是一个数据库的中间件,他并不存储任何数据。所以当有一天我们不用Mycat了只需要把mysql中的数据归集以后就能正常使用。

Mycat的作用是统一出口,查询缓存,查询优化。

abf91e8847e3d946b0396023dd145b62.png

MyCat特点

1.支持 SQL 92标准 支持Mysql集群,可以作为Proxy使用 支持JDBC连接ORACLE、DB2、SQL Server,将其模拟为MySQL Server使用 支持galera for mysql集群,percona-cluster或者mariadb cluster,提供高可用性数据分片集群,自动故障切换,高可用性 。

2.支持读写分离(参考具体的schema.xml配置)。

3.支持Mysql双主多从,以及一主多从的模式 。

4.支持全局表。

5.支持数据自动分片到多个节点,用于高效表关联查询 。

6.垮库join,支持独有的基于E-R 关系的分片策略,实现了高效的表关联查询多平台支持,部署和实施简单。

7.支持nosql数据库mongoDB。

安装环境

系统:windows

JDK版本:1.7

MySql客户端:navicat

MyCat下载地址:http://dl.mycat.io/1.6-RELEASE/

安装步骤

1.下载安装包解压

2.配置环境变量

cef2db670bb83b4cb307a023d779f718.png

2a77ffd3ea66bd876c9a86fa72459ad1.png

3.进入D:\mycat\bin  修改 schema.xml

55eef9bcec5ab89ff365009189649936.png

Schema

name:逻辑库名称,sqlMaxLimit:默认显示条数

Table

name:逻辑表名称,rule:分片规则

4.进入D:\mycat\bin  修改 Server.xml  该文件配置MyCat连接的账户信息

16cb589089f4cdbaf38f72f1178c3940.png

5.进入D:\mycat\bin  修改 rule.xml

e5deac0a5080a6b64f46aa2109d62082.png

47a62f87ee5d1d52bb2cdc7489c05e97.png

ed47b61d430db59bb20f3c9746702875.png

8a1e3aac297aca209b6151db56c868ed.png

tableRule

name:规则名称 , column:分片的列 ,algorithm:function的名字

function

name:方法名称 , class:实现分片的具体类

6.启动MayCat,双击startup_nowrap.bat启动mycat服务

3d3326f65d067a616cafb4fde0d43ec9.png

7.打开navicat客户端,连接mycat。 账户:root,密码:123456,端口:8066

f6867db1f6334f7d2c5abf22306043ca.png

8.连接mysql ,账户:root,密码:123456,端口:3306

57da9be88720a59f1d29b0c4a8ac1a0e.png

根据rule2分片:

在mysql  db1,db2,db3,db4每个库下面建立表t_user,创建表脚本如下:

DROP TABLE IF EXISTS `t_user`;

CREATE TABLE `t_user` (

`user_id` int(11) NOT NULL COMMENT '用户ID',

`receive_address` varchar(256) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '收货地址',

`create_time` datetime NOT NULL,

`province_code` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,

PRIMARY KEY (`user_id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='用户信息表';

1cd7b34b91743549f5619062bcf64893.png

在mycat的TESTDB下面的的表t_user插入3000条数据,数据自动分配到4个mysql数据库中。

根据sharding-by-month分片:

在mysql  db1-db12每个库下面建立表wallet_profit,创建表脚本如下:

DROP TABLE IF EXISTS `wallet_profit`;

CREATE TABLE `wallet_profit` (

`id` bigint(18) NOT NULL AUTO_INCREMENT,

`member_id` bigint(18) NOT NULL COMMENT '会员ID',

`profit_date` date NOT NULL COMMENT '计息时间',

`prod_id` bigint(18) NOT NULL,

`wallet_amount` bigint(22) NOT NULL COMMENT '金额',

`day_wallet_profit` bigint(22) NOT NULL COMMENT '昨日总利息',

`base_profit` bigint(22) NOT NULL COMMENT '基础利息',

`coupon_profit` bigint(22) DEFAULT NULL COMMENT '加息券利息',

`annual_rate` int(11) NOT NULL COMMENT '基础年化',

`coupon_rate` int(11) NOT NULL COMMENT '加息券年化',

`gmt_create` datetime DEFAULT NULL,

`gmt_modify` datetime DEFAULT NULL,

PRIMARY KEY (`id`),

KEY `idx_withdraw_profit_member_id` (`member_id`) USING BTREE,

KEY `idx_withdraw_profit_date` (`profit_date`) USING BTREE

) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='收益表';

acd41aba3c902a2e7f848fb761ecc103.png

在mycat的TESTDB下面的的表wallet_profit插入以下数据,数据将按照月份分配到12个数据库中。

INSERT INTO `wallet_profit` (`id`, `member_id`, `profit_date`, `prod_id`, `wallet_amount`, `day_wallet_profit`, `base_profit`, `coupon_profit`, `annual_rate`, `coupon_rate`, `gmt_create`, `gmt_modify`) VALUES ('100000000000287210', '600000000000015303', '2016-01-01', '100000000000000001', '4000300', '1100', '876', '219', '80000', '20000', '2016-01-02 00:40:00', '2016-01-02 00:40:00');

INSERT INTO `wallet_profit` (`id`, `member_id`, `profit_date`, `prod_id`, `wallet_amount`, `day_wallet_profit`, `base_profit`, `coupon_profit`, `annual_rate`, `coupon_rate`, `gmt_create`, `gmt_modify`) VALUES ('100000000001100149', '600000000000059488', '2016-02-01', '100000000000000001', '2000248200', '548000', '438410', '109602', '80000', '20000', '2016-02-02 00:40:01', '2016-02-02 00:40:01');

INSERT INTO `wallet_profit` (`id`, `member_id`, `profit_date`, `prod_id`, `wallet_amount`, `day_wallet_profit`, `base_profit`, `coupon_profit`, `annual_rate`, `coupon_rate`, `gmt_create`, `gmt_modify`) VALUES ('100000000002350084', '600000000000328547', '2016-03-01', '100000000000000001', '300', '0', '0', '0', '80000', '20000', '2016-03-02 00:40:00', '2016-03-02 00:40:00');

INSERT INTO `wallet_profit` (`id`, `member_id`, `profit_date`, `prod_id`, `wallet_amount`, `day_wallet_profit`, `base_profit`, `coupon_profit`, `annual_rate`, `coupon_rate`, `gmt_create`, `gmt_modify`) VALUES ('100000000003981684', '600000000000005348', '2016-04-01', '100000000000000001', '20000000', '6000', '4383', '1643', '80000', '30000', '2016-04-02 00:10:00', '2016-04-02 00:10:00');

INSERT INTO `wallet_profit` (`id`, `member_id`, `profit_date`, `prod_id`, `wallet_amount`, `day_wallet_profit`, `base_profit`, `coupon_profit`, `annual_rate`, `coupon_rate`, `gmt_create`, `gmt_modify`) VALUES ('100000000005839454', '600000000000036991', '2016-05-01', '100000000000000001', '75783900', '20800', '16610', '4152', '80000', '20000', '2016-05-02 00:10:00', '2016-05-02 00:10:00');

INSERT INTO `wallet_profit` (`id`, `member_id`, `profit_date`, `prod_id`, `wallet_amount`, `day_wallet_profit`, `base_profit`, `coupon_profit`, `annual_rate`, `coupon_rate`, `gmt_create`, `gmt_modify`) VALUES ('100000000007989136', '600000000000009157', '2016-06-01', '100000000000000001', '941194900', '283600', '206289', '77358', '80000', '30000', '2016-06-02 00:10:00', '2016-06-02 00:10:00');

INSERT INTO `wallet_profit` (`id`, `member_id`, `profit_date`, `prod_id`, `wallet_amount`, `day_wallet_profit`, `base_profit`, `coupon_profit`, `annual_rate`, `coupon_rate`, `gmt_create`, `gmt_modify`) VALUES ('100000000010286130', '600000000000362561', '2016-07-01', '100000000000000001', '1949012700', '587400', '427180', '160192', '80000', '30000', '2016-07-02 00:10:00', '2016-07-02 00:10:00');

INSERT INTO `wallet_profit` (`id`, `member_id`, `profit_date`, `prod_id`, `wallet_amount`, `day_wallet_profit`, `base_profit`, `coupon_profit`, `annual_rate`, `coupon_rate`, `gmt_create`, `gmt_modify`) VALUES ('100000000012917800', '600000000000543800', '2016-08-01', '100000000000000656', '4930000', '1200', '1080', '135', '80000', '10000', '2016-08-02 00:10:00', '2016-08-02 00:10:00');

INSERT INTO `wallet_profit` (`id`, `member_id`, `profit_date`, `prod_id`, `wallet_amount`, `day_wallet_profit`, `base_profit`, `coupon_profit`, `annual_rate`, `coupon_rate`, `gmt_create`, `gmt_modify`) VALUES ('100000000016961611', '600000000000614085', '2016-09-01', '100000000000000656', '250068500', '68500', '54809', '13702', '80000', '20000', '2016-09-02 00:10:00', '2016-09-02 00:10:00');

INSERT INTO `wallet_profit` (`id`, `member_id`, `profit_date`, `prod_id`, `wallet_amount`, `day_wallet_profit`, `base_profit`, `coupon_profit`, `annual_rate`, `coupon_rate`, `gmt_create`, `gmt_modify`) VALUES ('100000000023765373', '600000000000006820', '2016-10-01', '100000000000000656', '434073900', '130800', '95139', '35677', '80000', '30000', '2016-10-02 00:10:00', '2016-10-02 00:10:00');

INSERT INTO `wallet_profit` (`id`, `member_id`, `profit_date`, `prod_id`, `wallet_amount`, `day_wallet_profit`, `base_profit`, `coupon_profit`, `annual_rate`, `coupon_rate`, `gmt_create`, `gmt_modify`) VALUES ('100000000031369981', '600000000000684383', '2016-11-01', '100000000000000656', '630350900', '172700', '138159', '34539', '80000', '20000', '2016-11-02 00:10:00', '2016-11-02 00:10:00');

INSERT INTO `wallet_profit` (`id`, `member_id`, `profit_date`, `prod_id`, `wallet_amount`, `day_wallet_profit`, `base_profit`, `coupon_profit`, `annual_rate`, `coupon_rate`, `gmt_create`, `gmt_modify`) VALUES ('100000000039289911', '600000000000732505', '2016-12-01', '100000000000000656', '35000000', '11500', '7671', '3835', '80000', '40000', '2016-12-02 00:10:00', '2016-12-02 00:10:00');

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值