Canal推送到kafka

该文详细介绍了如何配置和使用Canal从MySQL同步数据到TiDB,包括MySQL的binlog配置、Canal的设置、Kafka主题创建以及启动和验证数据同步的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考:Flink 最佳实践之使用 Canal 同步 MySQL 数据至 TiDB - 个人文章 - SegmentFault 思否

1.mysql配置

修改mysql配置文件,开启mysql binlog

vim /etc/my.cnf
[mysqld]
log-bin=mysql-bin # 开启 binlog
binlog-format=ROW # 选择 ROW 模式
server_id=1 # 配置 MySQL replaction 需要定义,不要和 canal 的 slaveId 重复

重启mysql服务

service mysqld restart

创建canal用户,并进行授权,使其具有mysql slave的权限:

CREATE USER canal IDENTIFIED BY 'canal';
GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'canal'@'%';
FLUSH PRIVILEGES;

查看 MySQL Binlog 信息

show master status;

 

2. Canal配置

canal版本:1.1.4

注意,这边1.1.6版本的canal-admin有bug

修改 /opt/canal/conf/canal.properties 配置文件

## modify bellowing configuration
canal.zkServers =192.168.12.24:2181
canal.serverMode = kafka
canal.destinations = example        ## 需要在 /opt/canal/conf 目录下创建一个 example 文件夹,用于存放 destination 的配置
canal.mq.servers = 192.168.12.22:9092

 

修改 /opt/canal/conf/example/instance.properties 配置文件

## modify bellowing configuration
canal.instance.master.address=192.168.12.25:3306
canal.instance.dbUsername=canal
canal.instance.dbPassword=canal
canal.instance.filter.regex=.*\\..*                    ## 过滤数据库的表
canal.mq.topic=canal-kafka

 

3.在 Kafka 中创建一个 Topic

在 Kafka 中创建一个 Topic canal-kafka,这个Topic 的名字要与 Canal 配置文件canal/conf/example/instance.properties 中的 canal.mq.topic=canal-kafka 对应:

kafka-topics.sh --create --zookeeper zk地址 --topic topic名 --partitions num --replication-factor num
--replication-factor 表示副本数。 一个副本最快,2个副本安全好一点,性能下降一点,3个副本安全性最好
--partitions 分区。 分区数量建议是Broker数量的n倍

 

4. 启动canal

canal/bin/startup.sh

 

5. 查看 Canal 日志

canal/logs/example/example.log

2021-02-24 01:41:40.293 [destination = example , address = /192.168.12.25:3306 , EventParser] WARN  c.a.o.c.p.inbound.mysql.rds.RdsBinlogEventParserProxy - ---> begin to find start position, it will be long time for reset or first position
2021-02-24 01:41:40.293 [destination = example , address = /192.168.12.25:3306 , EventParser] WARN  c.a.o.c.p.inbound.mysql.rds.RdsBinlogEventParserProxy - prepare to find start position just show master status
2021-02-24 01:41:40.542 [destination = example , address = /192.168.12.25:3306 , EventParser] WARN  c.a.o.c.p.inbound.mysql.rds.RdsBinlogEventParserProxy - ---> find start position successfully, EntryPosition[included=false,journalName=binlog.000001,position=4,serverId=1,gtid=<null>,timestamp=1614134832000] cost : 244ms , the next step is binlog dump

 

6. 查看kafka消息

开启一个模拟的消费者

kafka-console-consumer.sh --bootstrap-server hadoop101:9092 --topic canal-kafka --from-beginning

 

往表中插入数据,可以看到canal把binlog推到kafka里面去了

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值