Canal同步mysql数据库(支持多库,多表)

3,562 阅读6分钟

通过canal去同步mysql数据库简单入门,因为目前开发用的是window系统,所以就用window演示,其实canal在linux系统上配置和window上配置是一样的。 mysql5.7.37+canal.deployer-1.1.4-SNAPSHOT+canal.adapter-1.1.4-SNAPSHOT

一:数据库设置

查看log_bin是否开启
show variables like 'log_%';
查看log_bin的值是否为ON,如果为OFF,需要在mysql配置文件my.ini中添加一下数据

如果是Linux系统,系统大概会在/home/etc/my.cnf

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

授权 canal 链接 MySQL 账号具有作为 MySQL slave 的权限, 如果已有账户可直接 grant

创建用户、授权:
CREATE USER canal IDENTIFIED BY 'canal';  
GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'canal'@'%';
-- GRANT ALL PRIVILEGES ON *.* TO 'canal'@'%' ;
FLUSH PRIVILEGES;
查看权限:
show grants for canal;

二:canal下载以及配置

下载deployer-1.1.4和adapter-1.1.4
地址:github.com/alibaba/can…
下载完之后解压即可

配置deployer
配置canal.properties
#ip地址,不写默认本地地址
canal.ip =

canal实例名称默认为example,如果需要自定义canal实例名称,例如改为testexample,请执行以下操作。
修改canal.properties文件中canal.destinations的值为自定义的canal实例名称,其他配置均保持默认即可。

canal.destinations = test_ots

在conf路径下创建以canal实例名称命名的文件夹textexample,并将conf/example路径下的instance.properties文件复制到conf/test_ots/路径下。

配置instance.properties
# canal.instance.mysql.slaveId=0 可以不开启,如果开启的话,注意不要和刚才在my.ini配置的server_id一样
#源数据库的地址和端口号、如果不是本地测试一定要写IP地址!!!
canal.instance.master.address=127.0.0.1:3306
canal.instance.dbUsername=root
canal.instance.dbPassword=root

# 配置监听,支持正则表达式\
canal.instance.filter.regex=canal01\\..*\
# 配置不监听,支持正则表达式\
canal.instance.filter.black.regex=mysql\\.slave_.*
配置项是否必填示例值描述
canal.instance.master.address127.0.0.1:3306canal监听的数据库地址,格式为host:port
canal.instance.dbUsernametest数据库账号用户名。
canal.instance.dbPassworddb****数据库账号密码。
canal.instance.filter.regex.\..canal实例关注的表。通过正则表达式匹配。此处表示匹配所有数据库下的所有表。
canal.destinationstest_otscanal实例名称,必须与配置文件所在上层路径相同。例如配置文件的路径为conf/test_ots/instance.properties,则canal实例名称为test_ots。

注:

mysql 数据解析关注的表,Perl正则表达式.\
多个正则之间以逗号(,)分隔,转义符需要双斜杠(\\) \
常见例子:\
1.  所有表:.*   or  .*\\..*\
2.  canal schema下所有表: canal\\..*\
3.  canal下的以canal打头的表:canal\\.canal.*\
4.  canal schema下的一张表:canal.test1\
5.  多个规则组合使用:canal\\..*,mysql.test1,mysql.test2 (逗号分隔)\
注意:此过滤条件只针对row模式的数据有效(ps. mixed/statement因为不解析sql,所以无法准确提取tableName进行过滤)

deployer/bin/start.bat启动

配置adapter
1.配置conf/application.yml
server:
  port: 8081
spring:
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8
    default-property-inclusion: non_null
canal.conf:
  mode: tcp #tcp kafka rocketMQ rabbitMQ
  flatMessage: true
  zookeeperHosts:
  syncBatchSize: 1000
  retries: 3
  timeout:
  accessKey:
  secretKey:
  terminateOnException: true
  consumerProperties:
    # 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:

  srcDataSources:
    defaultDS:
      url: jdbc:mysql://rm-bp15po.mysql.rds.aliyuncs.com:3306/test_ots?useUnicode=true
      username: ****
      password: ****
  canalAdapters:
  - instance: example # canal instance Name or mq topic name
    groups:
    - groupId: g1
      outerAdapters:
      - name: logger
      - name: tablestore
        key: ts
        properties:
          tablestore.endpoint: https://test-2009.cn-hangzhou.ots.aliyuncs.com
          tablestore.accessSecretId: ****
          tablestore.accessSecretKey: ****
          tablestore.instanceName: test-2009    

注意:一定要注意格式,因为解压出来取消注释的话可能存在格式不对齐,启动可能会报错,已经踩过了。

配置项是否必填示例值描述
canal.conf:canalAdapters:instancetest_otscanal实例名称,必须与部署Depolyer时自定义的canal实例名称相同。
canal.conf:canalAdapters:outerAdapters: -name:tablestore定义适配器类型。设置此项为tablestore,表示此适配器下游写入Tablestore库。
canal.conf:canalAdapters:outerAdapters: properties:tablestore.endpointtest-2009.cn-hangzhou.ots.aliyuncs.com填写目标Tablestore实例的服务地址
canal.conf:canalAdapters:outerAdapters: properties:tablestore.accessSecretIdLTAn********************登录账号的AccessKey ID和AccessKey Secret,获取方式请参见为RAM用户创建访问密钥
canal.conf:canalAdapters:outerAdapters: properties:tablestore.accessSecretKeyzbnK**************************
canal.conf:canalAdapters:outerAdapters: properties:tablestore.instanceNametest-2009Tablestore实例的名称。
canal.conf: syncBatchSize1000一个请求批次中涉及的行数,统计DML(Data Manipulation Language)操作中的数据大小。如果一次拉取到的DML中涉及到的行数过大,且行数大于此参数值,则该批次数据会被拆分处理。
canal.conf: retries3Adapterprocessor层的重试次数。默认值为3。
canal.conf: timeout1000Adapterprocessor层通过consumer向上游拉取数据的超时时间。单位为毫秒。如果设置timeout为0,则拉取数据时,Adapterprocessor层会处于阻塞状态直到拉取到数据。
canal.conf : consumerProperties:canal.tcp.batch.size500consumer向上游拉取的DML个数。
canal.conf: terminateOnExceptiontrue默认为false。如果配置为true,则数据同步重试后仍失败,程序会暂停实时同步任务,等待用户手动处理。
2. 配置con/rdb/mytest_user.yml
dataSourceKey: defaultDS
destination: test_ots
groupId: g1
outerAdapterKey: ts
threads: 8
updateChangeColumns: false
dbMapping:
  database: test_ots
  table: order_contract_canal
  targetTable: canal_target_order
  targetPk:
    oId: oId
  targetColumns:
    oId:
    create_time: createTime$string
    pay_time: $string
    update_time: updateTime
  etlCondition: 
  commitBatch: 200 # 批量提交的行数。
配置项是否必填示例值描述
dataSourceKeydefaultDS该任务的源数据库标识,您可以在application.yml中canal.conf: srcDataSources下找到该标识对应的数据库。
destinationtest_otscanal实例名称,必须与application.yml中的canal.conf: canalAdapters下instance值相同。
groupIdg1分组ID,与application.yml中canal.conf: canalAdapters: groups下groupId值相同即可。在MQ模式下才需要使用,此处无需关注。
outerAdapterKeyts使用的Adapter标识,必须与application.yml中canal.conf: canalAdapters: groups: outerAdapters下key值相同。
threads8Bucket数量,默认值为1,对应于tablestorewriter中的bucket数量。
dbMapping.databasetest_ots源数据库名称。
dbMapping.tableorder_contract_canal源表名称。
dbMapping.targetTablecanal_target_order目标表名称。
dbMapping.targetPkoId: oId主键配置,格式为id: target_id,即源表主键: 目标表主键。当表存在多个主键时需要配置多个,多个主键配置顺序必须与Tablestore中的主键顺序相同。dbMapping.targetPk中支持配置主键列自增,格式为$$: target_id,表示在目标表中生成一列名称为target_id的主键且该主键列为自增列。当上游数据写入Tablestore时,canal adapter会自动填充该列的值。关于主键列自增的更多信息,请参见主键列自增
dbMapping.targetColumnscreate_time: createTime$string配置需要同步的列名以及列映射,支持配置类型转换。**注意 在dbMapping.targetPk中配置的非自增的主键列也需要在此处再进行配置,自增主键列不需要再进行配置。如果不配置类型转换,则canal会根据源表中字段类型推断目标字段类型。更多信息,请参见源表和目标Tablestore表中字段映射。支持配置的格式包含如下4种,请根据实际需要配置。**说明 在配置映射的字段类型时,字段类型大小写不敏感。- id: target_id$string:表示源表中id字段同步到目标表后为target_id字段,且字段类型映射为string。
id: target_id:表示源表中id字段同步到目标表后为target_id字段。
id::表示id字段同步前后字段名不变,字段类型采用默认映射。
id: $string:等同于id: id$string,表示id字段同步前后字段名不变,且字段类型映射为string。
dbMapping.etlConditionwhere create_time > "2021-01-01"全量抽取数据时的过滤条件,其中字段名称为源表字段名称。
dbMapping.commitBatch200一次批量RPC请求导入的行数,对应于tablestorewriter中的maxBatchRowsCount,默认为writerConfig中的默认值200。
updateChangeColumnsfalse行覆盖或行更新。默认值为false,表示行覆盖,即一行数据更新时,使用该行最新的整行值覆盖Tablestore中的旧行。如果设置为true,则表示行更新,即一行数据更新时,只对变化的字段进行操作。

此文章只适用 于入门,如果哪里描述的有问题还请多多指出。