上一篇中提到了使用gravity作分表,http://www.javashuo.com/article/p-zwiijkav-b.html html
另外还有个工具也比较好用,就是爱可生公司出品的 DTLE https://github.com/actiontech/dtlenode
关于这2个工具的差别,你们能够自行参考官方文档比对下,各有千秋吧。mysql
我的这2个工具都测试过:git
一、gravity支持的数据源更多,DTLE只支持MySQL,可是支持的姿式更丰富。
github
二、对于src和dest都是同一个实例的状况下,要作分表只能使用gravity来作,DTLE不支持这种玩法。sql
###############################################json
DTLE相关的文档:后端
https://actiontech.github.io/dtle-docs-cn/3/3.0_function_scenario_mapping.htmlapi
https://actiontech.github.io/dtle-docs-cn/1/1.0_mysql_replication.htmlbash
咱们这里演示的是: 经过DTLE,将1个大的实例中某个大表,拆到2个独立的实例里面,作分库分表(分库分表后,还能够结合爱可生的DBLE玩出更多花样,本次就不涉及)。
原始库:
# 演示用的帐号密码都是 dts 192.168.2.4:3306 mysql -udts -pdts -h 192.168.2.4 --port 5725 testdb
2个分库:
# 演示用的帐号密码都是dts 192.168.2.4:5725 192.168.2.4:19226 mysql -udts -pdts -h 192.168.2.4 --port 5725 mysql -udts -pdts -h 192.168.2.4 --port 19226
原表:
create database testdb; use testdb; CREATE TABLE `dtle_t1` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增id', `user_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '用户id', `s_status` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '状态', PRIMARY KEY (`id`), KEY `idx_uid` (`user_id`) USING BTREE ) COMMENT = '测试表' ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
须要按照user_id 作hash拆分,拆分到2个库里面
造些测试用的数据:
for i in {1..10000} ; do mysql -hdts -pdts -h 192.168.2.4 -e "insert into testdb.dtle_t1 (user_id,s_status) values (\"$RANDOM\",'0');" done
大体这样:
mysql -udts -pdts -h 192.168.2.4 --port 5725 testdb [test] > select count(*) from dtle_t1 ; +----------+ | count(*) | +----------+ | 10000 | +----------+ 1 row in set (0.007 sec) [testdb] > select (user_id%2) as hash_id,count(*) FROM dtle_t1 group by (user_id%2); +---------+----------+ | hash_id | count(*) | +---------+----------+ | 0 | 5008 | | 1 | 4992 | +---------+----------+ 2 rows in set (0.009 sec)
在2个分库上, 都执行上面的建表操做(貌似DTLE能自动建立,可是咱们这仍是人工建立下吧):
create database testdb; use testdb; CREATE TABLE `dtle_t1` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增id', `user_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '用户id', `s_status` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '状态', PRIMARY KEY (`id`), KEY `idx_uid` (`user_id`) USING BTREE ) COMMENT = '测试表' ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
启动DTLE进程:
cd /opt/dtle/ mkdir data/ ./bin/dtle server -config=./etc/dtle/dtle.conf
另开一个窗口,执行 查看 dtle 状态的命令,效果以下:
curl -XGET "127.0.0.1:8190/v1/nodes" -s | jq . [ { "CreateIndex": 4, "Datacenter": "dc1", "HTTPAddr": "127.0.0.1:8190", "ID": "821e9f34-a3c3-f981-df41-8fbdc580708a", "ModifyIndex": 22, "Name": "dba-test-node-01", "Status": "ready", "StatusDescription": "" } ]
准备的 shard1.json 内容以下:
{ "Name":"dtle-shard1", "Tasks":[ { "Type":"Src", "Config":{ "ReplicateDoDb":[ { "TableSchema":"testdb", "Tables":[ { "TableName":"dtle_t1", "Where":"user_id%2=0" } ] } ], "ConnectionConfig":{ "Host":"192.168.2.4", "Port":"3306", "User":"dts", "Password":"dts" } } }, { "Type":"Dest", "Config":{ "ConnectionConfig":{ "Host":"192.168.2.4", "Port":"5725", "User":"dts", "Password":"dts" } } } ] }
准备的 shard2.json 内容以下:
{ "Name":"dtle-shard2", "Tasks":[ { "Type":"Src", "Config":{ "ReplicateDoDb":[ { "TableSchema":"testdb", "Tables":[ { "TableName":"dtle_t1", "Where":"user_id%2=1" } ] } ], "ConnectionConfig":{ "Host":"192.168.2.4", "Port":"3306", "User":"dts", "Password":"dts" } } }, { "Type":"Dest", "Config":{ "ConnectionConfig":{ "Host":"192.168.2.4", "Port":"19226", "User":"dts", "Password":"dts" } } } ] }
提交任务到DTLE:
curl -H "Accept:application/json" -XPOST "http://127.0.0.1:8190/v1/jobs" -d @shard1.json -s | jq . curl -H "Accept:application/json" -XPOST "http://127.0.0.1:8190/v1/jobs" -d @shard2.json -s | jq .
结果相似以下:
{
"Index": 56,
"KnownLeader": false,
"LastContact": 0,
"Success": true
}
稍等片刻,等数据同步,这时候能够看到老的主库上面的链接,能够看到有2个GTID dump线程在运行:
[testdb] > show full processlist; +-------+------+-------------------+--------+------------------+------+---------------------------------------------------------------+-----------------------+ | Id | User | Host | db | Command | Time | State | Info | +-------+------+-------------------+--------+------------------+------+---------------------------------------------------------------+-----------------------+ | 43810 | root | localhost | testdb | Query | 0 | starting | show full processlist | | 43829 | root | localhost | testdb | Sleep | 25 | | NULL | | 43830 | dts | 192.168.2.4:38040 | NULL | Sleep | 293 | | NULL | | 43831 | dts | 192.168.2.4:38048 | NULL | Sleep | 293 | | NULL | | 43834 | dts | 192.168.2.4:38056 | NULL | Binlog Dump GTID | 292 | Master has sent all binlog to slave; waiting for more updates | NULL | | 43835 | dts | 192.168.2.4:38060 | NULL | Sleep | 290 | | NULL | | 43836 | dts | 192.168.2.4:38068 | NULL | Sleep | 290 | | NULL | | 43839 | dts | 192.168.2.4:38076 | NULL | Binlog Dump GTID | 289 | Master has sent all binlog to slave; waiting for more updates | NULL | +-------+------+-------------------+--------+------------------+------+---------------------------------------------------------------+-----------------------+ 8 rows in set (0.000 sec)
而后,查看下分库的数据:
[root@dba-test-node-01 /opt/dtle ] # mysql -udts -pdts -h 192.168.2.4 --port 5725 testdb -e "select count(*) from dtle_t1;" +----------+ | count(*) | +----------+ | 5008 | +----------+ [root@dba-test-node-01 /opt/dtle ] # mysql -udts -pdts -h 192.168.2.4 --port 19226 testdb -e "select count(*) from dtle_t1;" +----------+ | count(*) | +----------+ | 4992 | +----------+
咱们这里也能够再老的主库,人工再插入几条测试数据看看是否会自动分流到后端不一样的分片上面,我这里就不继续演示了。
列出当前的job:
curl -XGET "127.0.0.1:8190/v1/jobs" | jq .
列出某做业的全部任务执行
curl -XGET "127.0.0.1:8190/v1/job/8e928a6b-e2be-c7b7-0d4a-745163c87282/allocations" | jq . curl -XGET "127.0.0.1:8190/v1/job/e8780526-9464-9df9-61f2-48c70a991024/allocations" | jq .
删除一个做业:
curl -H "Accept:application/json" -XDELETE "127.0.0.1:8190/v1/job/4079eaba-bcec-08b1-5ac2-78aa4a21a49b"
相关文档: https://actiontech.github.io/dtle-docs-cn/4/4.4_http_api.html