参考: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里面去了