一、canal简介
canal主要用途是对MySQL数据库增量日志进行解析,提供增量数据的订阅和消费,简单说就是可以对MySQL的增量数据进行实时同步,支持同步到MySQL、Elasticsearch、HBase等数据存储中去。
早期阿里巴巴因为杭州和美国双机房部署,存在跨机房同步的业务需求,实现方式主要是基于业务 trigger 获取增量变更。从 2010 年开始,业务逐步尝试数据库日志解析获取增量变更进行同步,由此衍生出了大量的数据库增量订阅和消费业务。
二、工作原理
canal会模拟MySQL主库和从库的交互协议,从而伪装成MySQL的从库,然后向MySQL主库发送dump协议,MySQL主库收到dump请求会向canal推送binlog,canal通过解析binlog将数据同步到其他存储中去。
三、实操
3.1 组件下载
-
首先下载canal的各个组件canal-server、canal-adapter、canal-admin,下载地址:https://github.com/alibaba/canal/releases
-
canal的各个组件的用途:
canal-deploy:可以直接监听MySQL的binlog,把自己伪装成MySQL的从库,只负责接收数据,并不做处理。
canal-adapter:相当于canal的客户端,会从canal-deploy中获取数据,然后对数据进行同步。
canal-admin:为canal提供整体配置管理、节点运维等面向运维的功能,提供相对友好的WebUI操作界面,方便更多用户快速和安全的操作。 -
版本约束
应用 | 版本 |
---|---|
MySQL | 5.7 |
Elasticsearch | 7.6.2 |
Kibanba | 7.6.2 |
MySQL | 5.7 |
Canal | 1.1.5-alpha-2 |
3.2 MySQL配置
- 由于canal是通过订阅MySQL的binlog来实现数据同步的,所以需要开启MySQL的binlog写入功能,并设置binlog-format为ROW模式,我的配置文件为 D:\environment\mysql-5.7.9\my.ini,修改如下内容即可;
[mysqld]
## 指定不需要同步的数据库名称
binlog-ignore-db=mysql
## 开启二进制日志功能
log-bin=D:\environment\mysql-5.7.9\mysql-bin
## 设置二进制日志使用内存大小(事务)
binlog_cache_size=1M
## 设置使用的二进制日志格式(mixed,statement,row)
binlog_format=row
## 二进制日志过期清理时间。默认值为0,表示不自动清理。
expire_logs_days=7
## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断。
## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
slave_skip_errors=1062
- 配置完成后需要重新启动MySQL,重启成功后通过如下命令查看binlog是否启用;
show variables like '%log_bin%'
- 再查看下MySQL的binlog模式;
show variables like 'binlog_format%';
- 接下来需要创建一个拥有从库权限的账号,用于订阅binlog,这里创建的账号为canal:canal;
CREATE USER canal IDENTIFIED BY 'canal';
GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'canal'@'%';
FLUSH PRIVILEGES;
- 创建好测试用的数据库canal-test,之后创建一张商品表product,建表语句如下。
CREATE TABLE `product` (`id` bigint(20) NOT NULL AUTO_INCREMENT,`title` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,`sub_title` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,`price` decimal(10, 2) NULL DEFAULT NULL,`pic` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
3.3 canal-server使用
- 解压 canal.deployer-1.1.5-SNAPSHOT.tar.gz,到指定目录canal-server
- 修改配置文件conf/example/instance.properties,按如下配置即可,主要是修改数据库相关配置;
# 需要同步数据的MySQL地址
canal.instance.master.address=127.0.0.1:3306
canal.instance.master.journal.name=
canal.instance.master.position=
canal.instance.master.timestamp=
canal.instance.master.gtid=
# 用于同步数据的数据库账号
canal.instance.dbUsername=canal
# 用于同步数据的数据库密码
canal.instance.dbPassword=canal
# 数据库连接编码
canal.instance.connectionCharset = UTF-8
# 需要订阅binlog的表过滤正则表达式
canal.instance.filter.regex=.*\\..*
- 双击startup.sh脚本启动canal-server服务;
- 启动成功后,logs/canal/canal.log查看服务日志信息;
2023-12-12 09:14:05.014 [main] INFO com.alibaba.otter.canal.deployer.CanalLauncher - ## set default uncaught exception handler
2023-12-12 09:14:05.047 [main] INFO com.alibaba.otter.canal.deployer.CanalLauncher - ## load canal configurations
2023-12-12 09:14:05.057 [main] INFO com.alibaba.otter.canal.deployer.CanalStarter - ## start the canal server.
2023-12-12 09:14:05.176 [main] INFO com.alibaba.otter.canal.deployer.CanalController - ## start the canal server[172.16.5.5(172.16.5.5):11111]
2023-12-12 09:14:06.306 [main] INFO com.alibaba.otter.canal.deployer.CanalStarter - ## the canal server is running now ......
- 启动成功后,logs/example/example.log 查看instance日志信息;
2023-12-12 09:03:08.122 [main] INFO c.a.o.c.i.spring.support.PropertyPlaceholderConfigurer - Loading properties file from class path resource [canal.properties]
2023-12-12 09:03:08.126 [main] INFO c.a.o.c.i.spring.support.PropertyPlaceholderConfigurer - Loading properties file from class path resource [example/instance.properties]
2023-12-12 09:03:08.309 [main] INFO c.a.o.c.i.spring.support.PropertyPlaceholderConfigurer - Loading properties file from class path resource [canal.properties]
2023-12-12 09:03:08.310 [main] INFO c.a.o.c.i.spring.support.PropertyPlaceholderConfigurer - Loading properties file from class path resource [example/instance.properties]
2023-12-12 09:03:08.749 [main] INFO c.a.otter.canal.instance.spring.CanalInstanceWithSpring - start CannalInstance for 1-example
2023-12-12 09:03:08.755 [main] WARN c.a.o.canal.parse.inbound.mysql.dbsync.LogEventConvert - --> init table filter : ^.*\..*$
2023-12-12 09:03:08.755 [main] WARN c.a.o.canal.parse.inbound.mysql.dbsync.LogEventConvert - --> init table black filter :
2023-12-12 09:03:08.828 [destination = example , address = /127.0.0.1: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
2023-12-12 09:03:08.888 [main] INFO c.a.otter.canal.instance.core.AbstractCanalInstance - start successful....
3.4 canal-adapter使用
- 解压 canal.adapter-1.1.5-SNAPSHOT.tar.gz,到指定目录canal-adpter
- 修改配置文件conf/application.yml
canal.conf:mode: tcp # 客户端的模式,可选tcp kafka rocketMQflatMessage: true # 扁平message开关, 是否以json字符串形式投递数据, 仅在kafka/rocketMQ模式下有效zookeeperHosts: # 对应集群模式下的zk地址syncBatchSize: 1000 # 每次同步的批数量retries: 0 # 重试次数, -1为无限重试timeout: # 同步超时时间, 单位毫秒accessKey:secretKey:consumerProperties:# canal tcp consumercanal.tcp.server.host: 127.0.0.1:11111 #设置canal-server的地址canal.tcp.zookeeper.hosts:canal.tcp.batch.size: 500canal.tcp.username:canal.tcp.password:srcDataSources: # 源数据库配置defaultDS:url: jdbc:mysql://127.0.0.1:3306/canal-test?useUnicode=trueusername: canalpassword: canalcanalAdapters: # 适配器列表- instance: example # canal实例名或者MQ topic名groups: # 分组列表- groupId: g1 # 分组id, 如果是MQ模式将用到该值outerAdapters:- name: logger # 日志打印适配器- name: es7 # ES同步适配器hosts: 127.0.0.1:9200 # ES连接地址properties:mode: rest # 模式可选transport(9300) 或者 rest(9200)# security.auth: test:123456 # only used for rest modecluster.name: elasticsearch # ES集群名称
- 新增 conf/es7/product.yml
dataSourceKey: defaultDS # 源数据源的key, 对应上面配置的srcDataSources中的值
destination: example # canal的instance或者MQ的topic
groupId: g1 # 对应MQ模式下的groupId, 只会同步对应groupId的数据
esMapping:_index: canal_product # es 的索引名称_id: _id # es 的_id, 如果不配置该项必须配置下面的pk项_id则会由es自动分配sql: "SELECTp.id AS _id,p.title,p.sub_title,p.price,p.picFROMproduct p" # sql映射etlCondition: "where a.c_time>={}" #etl的条件参数commitBatch: 3000 # 提交批大小
- 双击startup.sh脚本启动canal-adapter服务;
- 查看日志 logs/adapter/adapter.log ,如下启动成功
2023-12-12 09:03:18.120 [main] INFO c.a.o.canal.adapter.launcher.loader.CanalAdapterLoader - Load canal adapter: logger succeed
2023-12-12 09:03:18.459 [main] INFO c.a.o.c.client.adapter.es.core.config.ESSyncConfigLoader - ## Start loading es mapping config ...
2023-12-12 09:03:18.516 [main] INFO c.a.o.c.client.adapter.es.core.config.ESSyncConfigLoader - ## ES mapping config loaded
2023-12-12 09:03:18.694 [main] INFO c.a.o.canal.adapter.launcher.loader.CanalAdapterLoader - Load canal adapter: es7 succeed
2023-12-12 09:03:18.719 [main] INFO c.alibaba.otter.canal.connector.core.spi.ExtensionLoader - extension classpath dir: C:\Users\Administrator\Desktop\canal-adpter\plugin
2023-12-12 09:03:18.738 [main] INFO c.a.o.canal.adapter.launcher.loader.CanalAdapterLoader - Start adapter for canal-client mq topic: example-g1 succeed
2023-12-12 09:03:18.738 [main] INFO c.a.o.canal.adapter.launcher.loader.CanalAdapterService - ## the canal client adapters are running now ......
2023-12-12 09:03:18.738 [Thread-4] INFO c.a.otter.canal.adapter.launcher.loader.AdapterProcessor - =============> Start to connect destination: example <=============
2023-12-12 09:03:18.745 [main] INFO org.apache.coyote.http11.Http11NioProtocol - Starting ProtocolHandler ["http-nio-8080"]
2023-12-12 09:03:18.749 [main] INFO org.apache.tomcat.util.net.NioSelectorPool - Using a shared selector for servlet write/read
2023-12-12 09:03:18.775 [main] INFO o.s.boot.web.embedded.tomcat.TomcatWebServer - Tomcat started on port(s): 8080 (http) with context path ''
2023-12-12 09:03:18.783 [main] INFO c.a.otter.canal.adapter.launcher.CanalAdapterApplication - Started CanalAdapterApplication in 4.039 seconds (JVM running for 4.603)
2023-12-12 09:03:18.853 [Thread-4] INFO c.a.otter.canal.adapter.launcher.loader.AdapterProcessor - =============> Subscribe destination: example succeed <=============
四、数据同步演示
4.1 es 新增 mapping
PUT canal_product
{"mappings": {"properties": {"title": {"type": "text"},"sub_title": {"type": "text"},"pic": {"type": "text"},"price": {"type": "double"}}}
}
4.2 插入数据
INSERT INTO product ( id, title, sub_title, price, pic ) VALUES ( 5, '小米8', ' 全面屏游戏智能手机 6GB+64GB', 1999.00, NULL );
4.3 查询es 数据
GET canal_product/_search