Databus for Mysql

首先准备Mysql环境,本次使用的Mysql版本是5.7 root/Welcome_1:3306

1.配置mysql

mysql需要配置的有三点:

  • 开启binlog,
  • 设置binlog格式为ROW(mysql5.7版本默认格式就是ROW,所以这里我没有更改)
  • 禁用binlog_checksum

开启binlog的方法:

vim /etc/my.cnf 
## 添加如下内容:
server-id=1
log-bin=/var/lib/mysql/mysql-bin
## 添加好后重启
systemctl restart mysqld.service;
mysql -uroot -pWelcome_1
mysql>show variables like 'log_bin%';
mysql> show variables like 'binlog_format';

看到binlog已经开启,并且binlog的格式是ROW。
在这里插入图片描述

禁用binlog_checksum的方法:

mysql> show global variables like 'binlog_checksum';
# 结果是CRC32
set global binlog_checksum=none;
# 再次查询
show global variables like 'binlog_checksum';
# 结果是NONE 禁用成功

2. 准备测试数据库

编译databus

# 首先打开这个文件PersonClientMain.java
vim databus2-example/databus2-example-client/src/main/java/com/linkedin/databus/client/example/PersonClientMain.java
# 将26行改为:
static final String PERSON_SOURCE = "com.linkedin.events.example.or_test.Person";
# 不更改的话默认是读取Oracle示例的,否则后面运行cliet会无法捕获数据变更。
# 更改完毕即可编译:
 gradle -Dopen_source=true assemble
# 运测试环境脚本
$ cd build/databus2-example-relay-pkg/distributions
$ tar -zxvf databus2-example-relay-pkg.tar.gz
 vim bin/create_person.sh
# 将第4行端口号改为3306(默认是33066)
MYSQL='/usr/bin/mysql --protocol=tcp --port=3306'
# 配置mysql本地免密码登录:
 vim ~/.my.cnf
# 输入如下内容
[client]
host='localhost'
user='root'
password='Welcome_1'
# 运行测试环境脚本:
$ cd bin && ./create_person.sh

运行结果如下:
在这里插入图片描述
ERROR 1133 (42000) at line 1: Can’t find any matching row in the user table这个错误是因为这个脚本造成的:

${MYSQL} -uroot -e "GRANT ALL ON *.* TO 'or_test'@'127.0.0.1';"

忽略即可,本来就没有创建127.0.0.1相关的用户,不影响后续。

3.运行relay

首先更改build/databus2-example-relay-pkg/distributions/conf/sources-or-person.json
将其中的参数改为我们要使用的测试环境参数,目前已知端口3306,再查询一下server_id。

mysql> show variables like '%server_id%';//我这里查询到的是1,
# 最终更改结果如下:
{
    "name" : "person",
    "id"  : 1,
    "uri" : "mysql://or_test%2For_test@localhost:3306/1/mysql-bin",
    "slowSourceQueryThreshold" : 2000,
    "sources" :
    [
        {
        "id" : 40,
        "name" : "com.linkedin.events.example.or_test.Person",
        "uri": "or_test.person",
        "partitionFunction" : "constant:1"
         }
    ]
}

运行relay:

 sh bin/start-example-relay.sh or_person -Y ./conf/sources-or-person.json
# 测试relay:
 curl -s http://localhost:11115/sources
 [{"name":"com.linkedin.events.example.or_test.Person","id":40}] //得到这个结果代表运行成功。
# 更新一下person表
mysql> update person set first_name='John' where id=1;
#测试relay是否捕获了数据更新事件:
 curl -s http://localhost:11115/containerStats/inbound/events/total?pretty | grep -m1 numDataEvents
"numDataEvents" : 1,//得到这个结果证明已经捕获成功

4.运行Client

cd到example-client文件夹build/databus2-example-client-pkg/distributions

#运行client
 sh bin/start-example-client.sh person
#监控一下client.log日志:
 tail -f logs/client.log
#向person表汇总插入一条数据再删除。
insert into person values(25,'ket','john',sysdate(),false);
delete from person where id=25;

会发现client.log中将两个事件都捕获到了。
在这里插入图片描述
至此,databus for mysql的例子测试完毕。
不过官方文档也指出了目前的软件完成程度Databus-for-MySQL
Future Work

  1. Automatic Avro schema file generation for a given MySQL table
  2. Support for consistent change-capture in a clustered MySQL environment with mastership transfers
  3. Support for global TXID in MySQL-5.6
  4. Multi-tenancy optimizations (w.r.t. number of fetches of binlog files from master and server-side filtering)
  5. Composite keys

目前or_test.person的avro序列化文件是已经生成好的,可以直接使用,但是avro序列化文件生成目前只支持Oracle,官方还没有支持Mysql。

小结:

最近两篇写了databus针对mysql以及Oracle的示例,网络上搜索相关资料比较少,介绍这个工具的文章都说这个工具功能比较完善而且性能好。不过通过这两个例子我发现这个工具相比OGG这种成熟的商用软件,还是显得简陋了。OGG可以通过配置源端和目标端完成数据同步。而databus只完成了数据捕获,如果要实现数据捕获以后同步到目标库,还要自己写程序实现。开发以及学习成本较高。所以我觉得对于多数公司应该不适用。

参考文章:

  1. databus/wiki
  2. inkedin 的 databus 部署
  3. databus bootstrap 部署
  4. Databus架构分析与初步实践(for mysql)(上篇)
  5. Databus架构分析与初步实践(for mysql)(下篇)
  6. Databus部分翻译
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值