概述
Maxwell 针对与MySQL做实时数据同步的一个开源中间件,监控MySQL binlog日志并记录每行的操作记录(insert,update,delete)。它设计的初衷是实时采集Mysql数据到Kafka,它是Kafka的Producer。
- 支持全表load数据
- 支持自动断点还原
- 支持按照列将数据发送到Kafka不同分区
Maxwell下载地址:https://github.com/zendesk/maxwell/releases/download/v1.22.3/maxwell-1.22.3.tar.gz Maxwell官网:http://maxwells-daemon.io/
配置MySQL
开启binlog
#修改mysql配置文件,开启binlog及设置server-id vi my.cnf [mysqld] server_id=1 log-bin=master binlog_format=row #在mysqld 中配置binlog,并设置为ROW模式,设置server_id。必须配置server_id以及binlog格式为Row #修改完成后,退出文件。重启mysql service mysqld restart #若生产上数据库不能重启,则可以在运行的服务器上设置 set global binlog_format=ROW; set global binlog_row_image=FULL; # 没有设置 server_id 的话需要设置 set GLOBAL server_id = 1 #注意: #1、配置全局变量后,建议在配置文件同样做相应修改,否则数据库重启后,变量失效,还需要重启配置全局变量。 #2、binlog_format是会话级别的属性。需要关闭所有活动连接才能完全转换为行的复制。
创建Maxwell的数据库和用户
# 登录要监控的mysql,创建用户及数据库。maxwell默认会将运行数据存储在mysql的waxwell数据库中 CREATE USER ‘maxwell‘@‘%‘ IDENTIFIED BY ‘123456‘; GRANT ALL ON maxwell.* TO ‘maxwell‘@‘%‘; GRANT SELECT, REPLICATION CLIENT, REPLICATION SLAVE ON *.* TO ‘maxwell‘@‘%‘; FLUSH PRIVILEGES;
快速启动Maxwell
stdout producer方式启动Maxwell
bin/maxwell --user=‘maxwell‘ --password=‘123456‘ --host=‘127.0.0.1‘ --producer=stdout # --user:上述创建的用户 # --password:maxwell对应的密码 # --host:mysql的ip # --producer:生产者,即将数据写入到哪里。stdout表示将数据输出到控制台,kafka表示写入到kafka.
Kafka producer方式启动Maxwell
bin/maxwell --user=‘maxwell‘ --password=‘123456‘ --host=‘127.0.0.1‘ --producer=kafka --kafka.bootstrap.servers=127.0.0.1:9092 --kafka_topic=maxwell # --producer=kafka:将数据写入到kafka # --kafka.bootstrap.servers:kafka broker集群的地址 # --kafka_topic:写入到kafka指定的topic,注意不需要提前创建topic。 #还有更多配置信息请参考[Maxwell](http://maxwells-daemon.io/)官网,如maxwell中配置kafka分区等。
Maxwell输出的数据格式
insert
{"database":"rz","table":"maxwell_test","type":"insert","ts":1564244120,"xid":1852,"commit":true,"data":{"name":"lisi","id":1,"age":20}}
update
{"database":"rz","table":"maxwell_test","type":"update","ts":1564244281,"xid":2069,"xoffset":0,"data":{"name":"wangwu","id":1,"age":16},"old":{"name":"lisi","age":20}} {"database":"rz","table":"maxwell_test","type":"update","ts":1564244281,"xid":2069,"commit":true,"data":{"name":"wangwu","id":2,"age":16},"old":{"name":"lisi","age":20}}
delete
{"database":"rz","table":"maxwell_test","type":"delete","ts":1564244299,"xid":2097,"xoffset":0,"data":{"name":"wangwu","id":1,"age":16}} {"database":"rz","table":"maxwell_test","type":"delete","ts":1564244299,"xid":2097,"commit":true,"data":{"name":"wangwu","id":2,"age":16}}