MyCat原理
Mycat实际上只是一个数据库的中间件,他并不存储任何数据。所以当有一天我们不用Mycat了只需要把mysql中的数据归集以后就能正常使用。
Mycat的作用是统一出口,查询缓存,查询优化。
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。
安装环境
3.进入D:mycatin 修改 schema.xml
Schema
name:逻辑库名称,sqlMaxLimit:默认显示条数
Table
name:逻辑表名称,rule:分片规则
4.进入D:mycatin 修改 Server.xml 该文件配置MyCat连接的账户信息
5.进入D:mycatin 修改 rule.xml
tableRule
name:规则名称 , column:分片的列 ,algorithm:function的名字
function
name:方法名称 , class:实现分片的具体类
6.启动MayCat,双击startup_nowrap.bat启动mycat服务
7.打开navicat客户端,连接mycat。 账户:root,密码:123456,端口:8066
8.连接mysql ,账户:root,密码:123456,端口:3306
根据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='用户信息表';
根据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='收益表';
在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');