使用 Canal 从 MySQL 数据库同步数据到 OceanBase 数据库
下载软件包。
下载 canal.adapter-1.1.5.tar.gz。
wget https://github.com/alibaba/canal/releases/download/canal-1.1.5/canal.adapter-1.1.5.tar.gz
将压缩包解压至目录
/Canal_Home/adapter
。mkdir /Canal_Home/adapter && tar zxvf canal.adapter-1.1.5.tar.gz -C /Canal_Home/adapter
修改启动器配置。
修改启动器配置:
conf/application.yml
。首先指定adapter
源端类型,通过mode
指定,这里选择tcp
。后面就要指定canal.tcp
相关属性,包括canal server
的 IP 和端口,数据库的连接用户和密码。之后指定adapter
目标端连接信息。instance
是源端实例名称,在 canal 部署的时候定义的。key 是自定义,名字后面有用。jdbc 相关属性是目标端 OceanBase 的连接方式,可以使用 MySQL 自带的驱动。示例如下:
mode: tcp #tcp kafka rocketMQ rabbitMQ flatMessage: true zookeeperHosts: syncBatchSize: 1000 retries: 0 timeout: accessKey: secretKey: consumerProperties: 1. canal tcp consumer canal.tcp.server.host: 127.0.0.1:11111 canal.tcp.zookeeper.hosts: canal.tcp.batch.size: 500 canal.tcp.username: canal.tcp.password: canalAdapters: - instance: example # canal instance Name or mq topic name groups: - groupId: g1 outerAdapters: - name: logger - name: rdb key: test_mysql_to_ob properties: jdbc.driverClassName: com.mysql.jdbc.Driver jdbc.url: jdbc:mysql://10.10.10.1:2883/test_data?useUnicode=true jdbc.username: root@mysql001#test4000 jdbc.password: ******
RDB 映射文件。
修改
conf/rdb/mytest_user.yml
文件。其中,destination
指定的是canal instance
名称;outerAdapterKey
是前面定义的key
;mirrorDb
指定数据库级别 DDL 和 DML 镜像同步。映射有两种:一是按表映射;二是整库映射。下面以整库映射为例进行配置,注释部分为按表映射的配置:
[root@obce00 adapter]# cat conf/rdb/mytest_user.yml #dataSourceKey: defaultDS #destination: example #groupId: g1 #outerAdapterKey: mysql1 #concurrent: true #dbMapping: 1. database: mytest 1. table: user 1. targetTable: mytest 1. targetPk: 1. id: id 1. mapAll: true 1. targetColumns: 1. id: 1. name: 1. role_id: 1. c_time: 1. test1: 1. etlCondition: "where c_time>={}" 1. commitBatch: 3000 # 批量提交的大小 1. Mirror schema synchronize config dataSourceKey: defaultDS destination: example groupId: g1 outerAdapterKey: test_mysql_to_ob concurrent: true dbMapping: mirrorDb: true database: test_data commitBatch: 1000
启动 RDB。
说明
如果使用了 OceanBase 的驱动,则将目标库 OceanBase 驱动包放入
lib
文件夹。启动 canal-adapter 启动器。
cd /Canal_Home/adapter && sh bin/startup.sh
查看 RDB 日志。
tail -f logs/adapter/adapter.log
停止服务可执行下述命令。
cd /Canal_Home/adapter && bin/stop.sh
查看数据同步情况。
在 MySQL 源端写入数据,在 OceanBase 目标端查看数据同步。