Maxwell 配置实时将MySQL数据同步到Kafka


Maxwell是一个通过解析MySQL的binlog日志,伪装成Mysql的slave实时将master的数据变化同步给外部系统的一个工具。因为最近有一个项目中有类似需要将Mysql的数据实时同步到Spark Streaming中使用的场景,所以需要用到maxwell进行配置操作。

环境准备

  1. 需要被同步数据的mysql数据库服务器需要开启binlog日志。
  2. 被同步的mysql数据库的binlog日志模式只能是row或者mixed,不能选择statement。
  3. 安装好JDK,maxwell-1.38.0需要的java版本要高于JDK1.8,所以我事先安装好了java15.
  4. 需要准备一个mysql服务器用于存储maxwell的元数据信息

安装maxwell

下载地址

maxwell-1.38.0下载地址:https://github.com/zendesk/maxwell/releases/download/v1.38.0/maxwell-1.38.0.tar.gz

安装

# 下载文件
[root@k8s-node8 software]# wget https://github.com/zendesk/maxwell/releases/download/v1.38.0/maxwell-1.38.0.tar.gz

# 解压文件
[root@k8s-node8 software]# tar -xzvf maxwell-1.38.0.tar.gz

创建元数据存放数据库

在存放maxwell元数据的mysql服务器(可以与被同步数据的Mysql服务器不是同一个)上中创建maxwell数据库,用于存放maxwell的元数据。

被同步的数据库上创建用于数据复制的账号,并授权

  • binlog配置示例
# /etc/my.cnf

[mysqld]
# maxwell needs binlog_format=row
binlog_format=row
server_id=1 
log-bin=master
  • 创建maxwell账号,用于复制数据
mysql> CREATE USER 'maxwell'@'%' IDENTIFIED BY 'XXXXXX';
mysql> CREATE USER 'maxwell'@'localhost' IDENTIFIED BY 'XXXXXX';

mysql> GRANT ALL ON maxwell.* TO 'maxwell'@'%';
mysql> GRANT ALL ON maxwell.* TO 'maxwell'@'localhost';

mysql> GRANT SELECT, REPLICATION CLIENT, REPLICATION SLAVE ON *.* TO 'maxwell'@'%';
mysql> GRANT SELECT, REPLICATION CLIENT, REPLICATION SLAVE ON *.* TO 'maxwell'@'localhost';

配置mysql同步到kafka任务

在maxwell的安装目录中创建jobs目录,用于存放任务配置文件,并在jobs目录中创建gmall.properties文件,文件内容如下:

gmall.properties 文件内容

# 日志级别
log_level=info
client_id=gmall_client_20220908
replica_server_id=1234

# Maxwell 元数据数据库信息
host=192.168.0.130
port=3306
user=maxwell
password=Maxwell@123

#producer kafka的配置信息
producer=kafka
kafka.bootstrap.servers=k8s-node3:9092,k8s-node5:9092,k8s-node8:9092
kafka_topic=GMALL_WD_DATA_INFO
kafka_partition_hash=murmur3
producer_partition_by=primary_key

#被同步的数据库的信息
replication_host=192.168.0.130
replication_user=maxwell
replication_password=Maxwell@123
replication_port=3306
jdbc_options=useSSL=false&serverTimezone=Asia/Shanghai

# 配置过滤器,可以配置只同步指定的数据库,指定的表的数据变动。
filter=exclude:*.*,include:gmall.*

运行任务

maxwell --config jobs/gmall.properties --daemon 

全表同步

如果初始时数据表中就有数据,但是后面才配置的maxwell,那么初始时的数据是没有监听到binlog的,也就没办法被maxwell同步到Sink。针对这种场景,maxwell提供了一种方式,可以应对这种场景。使用maxwell-bootstrap 就可以将全表的数据以bin-log的日志发送出来。

[root@k8s-node8 maxwell-1.38.0]# bin/maxwell-bootstrap --config jobs/gmall.properties --database gmall --table base_province 

数据样例:

{"database":"gmall","table":"user_info","type":"bootstrap-start","ts":1663599671,"data":{}}
{"database":"gmall","table":"user_info","type":"bootstrap-insert","ts":1663599671,"data":{"id":1,"login_name":"n1i98p7oavyf","nick_name":"妍妍","passwd":null,"name":"许妍","phone_num":"13432677568","email":"n1i98p7oavyf@sohu.com","head_img":null,"user_level":"1","birthday":"1985-04-04","gender":"F","create_time":"2022-04-04 10:23:15","operate_time":null,"status":null}}
{"database":"gmall","table":"user_info","type":"bootstrap-complete","ts":1663599671,"data":{}}

可能遇到的问题

问题一:Exception in thread “main” java.lang.UnsupportedClassVersionError: com/zendesk/maxwell/Maxwell has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/zendesk/maxwell/Maxwell has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:473)
	at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
	at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:601)

JDK的版本太低,升级JDK的版本就可以解决。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值