15

canal学习一:本地运行 canal

 3 years ago
source link: https://suiyia.github.io/2019/10/13/canal%E5%AD%A6%E4%B9%A0%E4%B8%80%EF%BC%9A%E6%9C%AC%E5%9C%B0%E8%BF%90%E8%A1%8C-canal/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

canal学习一:本地运行 canal

2019-10-13 canal 92 评论 字数统计: 1.2k(字) 阅读时长: 5(分)

a. canal 是什么?

  • MySQL 数据库 Binlog 的增量订阅 & 消费组件。简单来说,它可以监测 MySQL 的数据变化情况。

b. 用途

  • 数据同步、主从复制、数据库镜像等业务场景。

主要内容: 本地运行 canal,对数据库进行数据变更,查看 canal 输出结果。

运行环境: Ubuntu 18.04 LTS ;虚拟机,内存分配 4G;Windwos 系统运行步骤类似。

2. 基本环境配置

  • git:apt install git
  • maven:apt install maven
  • MySQL安装地址,目前 canal 开源版本支持支持 5.7 及以下的版本

    • 如果已经安装 MySQL,先查看 MySQL 版本,版本太高或太低会有兼容性问题。

      mysql> select version();
    • 更改 MySQL 配置,目录为 /etc/mysql/mysql.conf.d/mysqld.cnf,Windows 系统配置文件在 C:\ProgramData\MySQL\MySQL Server 5.7,文件的开头已经标明「The MySQL database server configuration file」,在 [mysqld] 下面增加如下字段:

    [mysqld]
    log-bin=mysql-bin #添加这一行就ok
    binlog-format=ROW #选择row模式
    server_id=1 #配置mysql replaction需要定义,不能和canal的slaveId重复

    重启 MySQL,执行:
    mysql > SHOW MASTER STATUS; // 看是否有结果,返回 binlog 名称以及位置
    mysql > SHOW VARIABLES LIKE '%binlog_format%'; // 查看配置文件是否生效,应该返回 ROW
  • 创建子用户,赋予相关权限,用于读取 MySQL Binlog 日志

    CREATE USER canal IDENTIFIED BY 'canal';  
    GRANT SELECT, SHOW VIEW, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'canal'@'%';
    -- GRANT ALL PRIVILEGES ON *.* TO 'canal'@'%' ; 需要具有SHOW VIEW 权限
    FLUSH PRIVILEGES;

3. canal 本地运行

a. 下载解压,并修改配置文件

  • release 下载页 ,下载解压 canal.deployer-xxx.tar.gz
  • 编辑 canal.deployer-xxx/conf/example/instance.properties,下面是需要注意
    canal.instance.master.address = 127.0.0.1:3306  # Mysql 地址端口
    canal.instance.dbUsername = canal # 用于读取 binlog 日志的用户,测试环境可以用 root 用户代替
    canal.instance.dbPassword = canal
    canal.instance.defaultDatabaseName = test # 选择对 test 数据库进行监控
    canal.instance.filter.regex = .\.. # 正则匹配需要监控的表
  • canal.deployer-xxx/bin 目录下,运行 ./startup.sh
  • 查看日志:vi logs/example/example.log,vi logs/canal/canal.log 查看 canal 是否报错。

c. 对数据库 test 进行数据变更,查看 canal 输出

  • demo 工程下载地址,下载 canal.example-xxx.tar.gz ,bin 目录下直接执行 ./startup.sh
  • 进入 canal.example-xxx/logs 目录,对数据库进行增删改操作,可以发现有输出,那么 canal 的本地运行算是成功了。

4. 配置文件解读

下载 release 包 ,其中配置可以分为两个部分:

  • canal.properties:系统根配置文件,分为两个小部分。common argument 定义全局 canal server 属性,destinations 定义多个 instance 的部分属性
  • instance.properties:具体的单个实例的配置

1. canal.properties 配置如下,主要讲解我觉得需要注意的地方:

#################################################
######### common argument ############# 定义 canal server 属性
#################################################
canal.id= 1
canal.ip= # canal server 绑定的本地IP信息,如果不配置,默认选择一个本机IP进行启动服务
canal.port=11111 #canal server 对外提供服务的端口
canal.zkServers=127.0.0.1:2181 # canal server 链接 zookeeper 集群的链接信息,多个用 逗号分隔
# tcp, kafka, RocketMQ
canal.serverMode = tcp # 服务方式,默认为 tcp,如果需要将 canal 消息发送到 kafka 就选 kafka
canal.instance.tsdb.dbUsername=canal # 数据库用户名
canal.instance.tsdb.dbPassword=canal # 密码
#################################################
######### destinations ############# instance列表定义,列出当前server上有多少个instance
#################################################
canal.destinations= example # 当前server上部署的instance列表,定义了canal.destinations后,需要在canal.conf.dir对应的目录下建立同名的文件,多个实例用 逗号分隔 canal.destinations = example1,example2
# conf root dir
canal.conf.dir = ../conf
# auto scan instance dir add/remove and start/stop instance
canal.auto.scan = true # instance 文件变化会立马表现出,不用重新启动 canal server
canal.auto.scan.interval = 5

canal.instance.global.mode = spring
canal.instance.global.lazy = false
canal.instance.global.spring.xml = classpath:spring/file-instance.xml # canal 实例通过 file-instance.xml 方式加载

2. instance.properties 单个实例的具体配置,每个 canal.destinations 内定义的实例都会有这个配置文件

canal.instance.master.address=127.0.0.1:3306 # 监控的数据库地址
canal.instance.dbUsername=canal # 监控的数据库用户名
canal.instance.dbPassword=canal
canal.instance.connectionCharset = UTF-8
canal.instance.defaultDatabaseName =test # 选择监控的数据库
canal.instance.filter.regex=.*\\..* # 白名单,选择监控哪些表
canal.instance.filter.black.regex= # 黑名单,选择不监控哪些表

5. 本地运行遇到的问题

  • Windows 平台运行 canal,点击 startup.bat 秒退。

    解决:在 startup.bat 最后添加 pause,查看具体报错原因

  • ‘Error: missing ‘server’ JVM at `C:\Program Files (x86)\Java\jre1.8.0_181\bin\server\jvm.dll’

    解决:环境配置问题,将 Java\jre1.8.0_181\bin\client 文件夹内文件移动到 Java\jre1.8.0_181\bin\server 目录下,没有 server 目录就新建一个。
    参考

  • 异常 [MultiStageCoprocessor-other-example-0] WARN com.taobao.tddl.dbsync.binlog.LogDecoder - Decoding Query failed from: binlog.000012:2831 java.io.IOException: Read Q_FLAGS2_CODE error: limit excceed: 64

    解决: 兼容性问题,使用 MySQL 5.7 ,该错误会在后续版本更新 https://github.com/alibaba/canal/wiki/BinlogChange%28MySQL8%29


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK