前言
maxwell是一款开源MySQL数据抓取工具,可以读取MySQL的binlog,然后转换成json并输出到kafka、redis等消息队列中。
bin/maxwell
,用于增量抓取bin/maxwell-boostrap
,用于全量抓取
搭建环境
应用 | 版本 | Addr |
---|---|---|
MySQL | 8.0.33 | 192.168.3.23:3306 |
Kafka | 2.13_3.4.0 | 192.168.3.23:9092 |
Maxwell | 1.40.0 | 无 |
安装MySQL
MySQL版本:8.0.33
- 修改配置文件,启用binlog和gtid
log_bin = mysql-bin
binlog_format = row
binlog_cache_size = 2M
server_id = 1
gtid_mode = on
enforce_gtid_consistency = on
log-slave-updates = 1
binlog-ignore-db = mysql,information_schema,sys,performance_schema
sync_binlog = 1
auto_increment_offset = 1
auto_increment_increment = 2
- 开启binlog直接输出SQL语句
SET GLOBAL binlog_rows_query_log_events=ON;
- 配置同步用户
CREATE USER 'maxwell'@'%' IDENTIFIED BY '123456';
-- GRANT ALL ON maxwell.* TO 'maxwell'@'%';
GRANT SELECT, REPLICATION CLIENT, REPLICATION SLAVE ON *.* TO 'maxwell'@'%';
安装JDK
后面的kafka和maxwell都需要java运行环境,本文使用的jdk版本为 openjdk 17.0.2
安装kafka(单节点)
Kafka版本2.13-3.4.0,使用kraft。
- 生成uuid
KAFKA_CLUSTER_ID="$(bin/kafka-storage.sh random-uuid)"
- 初始化log目录。可修改配置文件
config/kraft/server.properties
中日志目录路径。
bin/kafka-storage.sh format -t $KAFKA_CLUSTER_ID -c config/kraft/server.properties
- 启动。注意修改配置文件
config/kraft/server.properties
中的监听host地址
bin/kafka-server-start.sh -daemon config/kraft/server.properties
- 创建topic
bin/kafka-topics.sh --create --topic maxwell --bootstrap-server 127.0.0.1:9092
安装maxwell
- 从maxwell的github仓库 https://github.com/zendesk/maxwell 下载release压缩包并解压即可。
增量抓取
- 编辑配置文件
log_level=info
producer=kafka
kafka.bootstrap.servers=192.168.3.23:9092
# mysql连接信息
host=192.168.3.23
port=3306
user=maxwell
password=123456
# 过滤库表
# filter= include: test.*
gtid_mode=true
output_ddl=true
kafka_topic=maxwell
kafka.compression.type=snappy
kafka.retries=0
kafka.acks=1
# 全量抓取的时候会用到
client_id=2301
- 启动
nohup ./bin/maxwell --config ./config.properties > ./nohup.log 2>&1 &
- 测试。在数据库进行一些增删改操作,观测kafka的topic有没有数据,有数据则正常。
全量抓取
maxwell-bootstrap
依赖maxwell
实例,运行时需要指定maxwell的实例id。
./bin/maxwell-bootstrap --host='192.168.3.23' --port=3306 --user='root' --password='123456' --database="test" --table="tb1"