概览
这是Maxwell的守护进程,这是一个读取MySQL binlogs并将行更新写入Kafka,Kinesis,RabbitMQ,Google Cloud Pub / Sub或Redis(Pub / Sub或LPUSH)作为JSON的应用程序。 Maxwell具有较低的操作栏,可生成一致,易于摄取的更新流。它允许您轻松“固定”流处理系统的一些优点,而无需通过整个代码库来添加(不可靠)检测点。常见用例包括ETL,缓存构建/到期,度量收集,搜索索引和服务间通信。
Download: wget https://github.com/zendesk/maxwell/releases/download/v1.19.0/maxwell-1.19.0.tar.gz
转换样例:
mysql> insert into `test`.`maxwell` set id = 1, daemon = 'Stanislaw Lem';
maxwell: {
"database": "test",
"table": "maxwell",
"type": "insert",
"ts": 1449786310,
"xid": 940752,
"commit": true,
"data": { "id":1, "daemon": "Stanislaw Lem" }
}
mysql> update test.maxwell set daemon = 'firebus! firebus!' where id = 1;
maxwell: {
"database": "test",
"table": "maxwell",
"type": "update",
"ts": 1449786341,
"xid": 940786,
"commit": true,
"data": {"id":1, "daemon": "Firebus! Firebus!"},
"old": {"daemon": "Stanislaw Lem"}
}
安装
从github直接下载后解压即可。
mysql配置
$ vim /etc/my.cnf # centos
[mysqld]
server_id=1
log-bin=master
binlog_format=row
以上为官方的写法,如果上述操作导致mysql无法启动,请自行百度适合的方式
我的写法:
server-id=2
binlog-format=row
log-bin = /var/lib/mysql/mysql-bin
# expire_logs_days=10 # 未使用
# max_binlog_size=100M 未使用
注:和官方的写法区别在于中横线的位置不同,因官方的写法可能会导致mysql无法启动
注意:binlog_format是基于会话的属性。您需要关闭所有活动连接才能完全转换为基于行的复制。
权限:Maxwell需要在schema_database选项(默认maxwell)指定的数据库中存储状态的权限。
mysql> GRANT ALL on maxwell.* to 'maxwell'@'%' identified by 'abcd@1234';
mysql> GRANT SELECT, REPLICATION CLIENT, REPLICATION SLAVE on *.* to 'maxwell'@'%';
mysql> FLUSH PRIVILEGES;
如果提示密码不合要求,可参考以下链接解决。
https://blog.csdn.net/kuluzs/article/details/51924374
maxwell的配置
解压后的文件夹内有文件 config.properties.example
此为config的模板,可以将其复制后将后缀example删除,然后修改其中的内容为自己需要的内容,之后再启动maxwell时指定 --config
参数为此文件即可。我在操作过程中没有使用此方式,而是将所需参数全部写在启动命令中了。
启动maxwell
注意以下所有命令的启动需要在解压后的maxwell目录内
启动时的参数至少要有以下几项:
--user mysql数据库用户名
--password mysql数据库密码
--host mysql 数据库服务器
--producer 指定数据输出到哪里
# 以上为必须要的,以下的kafka相关的是以kafka方式启动时需要的参数,kafka相关分区参数在此未指定
--kafka.bootstrap.servers 指定kafka地址
--kafka_topic 指定kakfa的topic是哪一个
# 以下可选
--daemon 守护进程 后台运行
--filter 过滤条件,可以过滤数据库,表,列等等
命令行
bin/maxwell --user='maxwell' --password='XXXXXX' --host='127.0.0.1' --producer=stdout
- 以上命令将binlog解析后的json直接显示在控制台中
kafka
本次测试仅仅针对单机版kafka,一个topic和一个分区,关于kafka的内容,需自行学习
官方:
bin/maxwell --user='maxwell' --password='XXXXXX' --host='127.0.0.1' \ --producer=kafka --kafka.bootstrap.servers=localhost:9092 --kafka_topic=maxwell
我的测试:操作未对kafka分区进行区分,仅一个分区,所有数据库的变更均发送到一个topic的一个分区中
bin/maxwell --user='maxwell' --password='abcd@1234' --host='192.168.19.148' \ --producer=kafka --kafka.bootstrap.servers=192.168.19.148:9092 --kafka_topic=mysql2ck
更多参数:http://maxwells-daemon.io/config/
maxwell对sql语句的解析
- 一条insert语句插入多条数据,会将其拆为一条条输出到kafka,一条记录为一个json数据
- 类似的还有delete、update,与 insert 一致
使用过程中出现的问题
1、python创建的kafka消费者报错:
kafka.errors.UnsupportedCodecError: UnsupportedCodecError: Libraries for snappy compression codec not found
解决:
https://blog.csdn.net/chenggong2dm/article/details/70172682
https://github.com/andrix/python-snappy
https://blog.csdn.net/yiifaa/article/details/81876338
windows解决:
https://blog.csdn.net/chinewwen/article/details/81071182
https://www.lfd.uci.edu/~gohlke/pythonlibs/
下载安装即可
2、启动maxwell时,循环报错,与mysql的连接断开,
Could not find first log file name in binary log index file
类似上述错误的内容。
原因:修改了 /etc/my.cnf
文件内的log-bin=master 导致出现两种二进制文件,之前是mysql-bin的写法
解决:删除mysql目录内的 master.* 文件,更改/etc/my.cnf 为文章开始处的内容
3、maxwell分区参数的使用
- 测试中只有database方式,可以将不同数据库的操作,插入到不同的分区中
bin/maxwell --user='maxwell' --password='abcd@1234' --host='192.168.19.148' \
--producer=kafka --kafka.bootstrap.servers=192.168.19.148:9092 --kafka_topic=testCK \
--producer_partition_by=database --kafka_partition_hash=default