Maxwell 监听 MySql binlog 实现增量读取数据
来自Linux78|wiki
Maxwell简介
Maxwell是一个能够读取MySQL binlog并将行更新作为JSON写入Kafka、Kinesis或其他流媒体平台的应用程序。Maxwell的操作开销很低,只需要mysql和一个写操作的地方。它的常用用例包括ETL、缓存构建/过期、指标收集、搜索索引和服务间通信。Maxwell为您提供了事件源的一些好处,而无需重新构建整个平台。
资源地址:https://github.com/zendesk/maxwell 下载地址:https://github.com/zendesk/maxwell/releases/download/v1.21.1/maxwell-1.21.1.tar.gz
下载编译Maxwell
curl -sLo - https://github.com/zendesk/maxwell/releases/download/v1.21.1/maxwell-1.21.1.tar.gz \ | tar zxvf - cd maxwell-1.21.1
docker镜像
docker pull zendesk/maxwell
Mac OS X
brew install maxwell
配置mysql的my.cnf
vi /etc/my.cnf #仅列相关配置项 [mysqld] server_id=1234 log-bin=bin-log binlog_format=row #binlog_row_image=FULL
添加Maxwell权限
CREATE USER 'maxwell'@'%' identified by 'XXXXXX'; GRANT ALL on maxwell.* to 'maxwell'@'%' ; GRANT SELECT, REPLICATION CLIENT, REPLICATION SLAVE on *.* to 'maxwell'@'%';
查看是否配置成功
show variables like '%log_bin%';
maxwell启动命令
#producer=stdout 直接输入到终端窗口 bin/maxwell --user='maxwell' --password='XXXXXX' --host=192.168.31.77 --producer=stdout
kafka
bin/maxwell --user='maxwell' --password='XXXXXX' --host=node1 --producer=kafka --kafka.bootstrap.servers=node1:9092 --kafka_topic=test_maxwell --client_id=maxwell_id 启动maxwell-bootstrap
maxwell-bootstrap向导命令行引导表
Option Description config location of config file(本地的config文件) database database that contains the table to bootstrap(要引导的数据库) table table to bootstrap(引导表) config location of config file(本地的config文件) config location of config file(本地的config文件) config location of config file(本地的config文件) config location of config file(本地的config文件) config location of config file(本地的config文件)
Maxwell参数
Maxwell过滤: #仅匹配foodb数据库的tbl表和所有table_数字的表 --filter = 'exclude: foodb.*, include: foodb.tbl, include: foodb./table_\d+/' #排除所有库所有表,仅匹配db1数据库 --filter = 'exclude: *.*, include: db1.*' #排除含db.tbl.col列值为reject的所有更新 --filter = 'exclude: db.tbl.col = reject' #排除任何包含col_a列的更新 --filter = 'exclude: *.*.col_a = *' #完全排除bad_db数据库,若要恢复,必须删除maxwell库 --filter = 'blacklist: bad_db.*'
Maxwell-bootstrap功能:
使用maxwell-bootstrap时,可用的参数有 --log_level,日志级别 --user,用户名 --password,密码 --host,主机 --port,端口 --database,包含需要bootstrap的表的数据库名 --table,需要bootstrap的表名 --where,限制条件 --client_id,maxwell实例名称
常用配置:
config config.properties, 配置文件 log_level info,日志级别 daemon, 守护进程 schema_database maxwell, 数据库 client_id maxwell, maxwell实例名称 replica_server_id 6379, 类似于server_id,多实例 filter, 过滤器 output_binlog_position true, 记录binlog位点 output_ddl true, 记录ddl变更
实例
stdout启动
主执行程序:
bin/maxwell --user='maxwell' --password='XXXXXX' --host=192.168.31.77 --producer=stdout
启动一个maxwell-bootstrap
bin/maxwell-bootstrap --user='maxwell' --password='123456' --host=node1 --database=test --table=tb_author --log_level=info --client_id=maxwell
docker启动
docker run -it --rm zendesk/maxwell bin/maxwell --user=$MYSQL_USERNAME \ --password=$MYSQL_PASSWORD --host=$MYSQL_HOST --producer=stdout
1 kafka启动
bin/maxwell --user='maxwell' --password='XXXXXX' --host='127.0.0.1' \ --producer=kafka --kafka.bootstrap.servers=localhost:9092 --kafka_topic=maxwell
1、输出到stdout,json包含binlog位点,包含ddl语句
./maxwell --user='maxwell' --password='XXXXXX' --host=192.168.31.77 --output_binlog_position=true --output_ddl=true --producer=stdout
2、输出到redis,使用队列的方式
./maxwell --user='maxwell' --password='XXXXXX' --host=192.168.31.77 --output_binlog_position=true --output_ddl=true --producer=redis --redis_host=192.168.31.77 --redis_auth=xxxxxx --redis_type=lpush --redis_database=5 --redis_list_key=maxwell ./maxwell --user='maxwell' --password='XXXXXX' --host=192.168.31.77 --output_binlog_position=true --output_ddl=true --producer=redis --redis_host=192.168.31.77 --redis_auth=xxxxxx --redis_type=lpush --redis_database=5 --redis_list_key=maxwell --daemon
3、输出到rabbitmq
./maxwell --user='maxwell' --password='XXXXXX' --host=192.168.31.77 --output_binlog_position=true --output_ddl=true --producer=rabbitmq --rabbitmq_host=192.168.31.78 --rabbitmq_user=maxwell --rabbitmq_pass=maxwell --rabbitmq_virtual_host=maxwell_vhost --rabbitmq_exchange=maxwell --rabbitmq_exchange_type=topic --rabbitmq_routing_key_template=%db%.%table% --rabbitmq_declare_exchange=true