Canal-deployer1.1.8监听mysql数据变化(windows)

参考:

复制代码
  https://blog.csdn.net/zhouzhiwengang/article/details/128900318
  https://www.cnblogs.com/xfeiyun/p/17468158.html

Mysql设置

查看是否开启binlog
复制代码
  show VARIABLES like 'log_bin';
  如果值为"OFF",则需修改为"ON",方法:
    1、修改配置文件
    2、执行全局命令:set global log_bin='ON';
修改配置文件,配置binlog相关参数
复制代码
  #binlog文件名
  log-bin=mysql-bin
  #binlog日志模式
  binlog_format=ROW
  #mysql的server_id不能和canal的slaveId相同
  server_id=1
重启mysql服务
在mysql中添加canal后续需要连接mysql的用户,并添加相应权限
复制代码
  CREATE USER 'canal'@'%' IDENTIFIED BY 'canal';
  GRANT SHOW VIEW, SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'canal'@'%';
  FLUSH PRIVILEGES;

canal部署

下载并解压

地址:https://github.com/alibaba/canal/releases/tag/canal-1.1.8

复制代码
  解压后得到文件目录:
    |- bin
    |- conf
    |- lib
    |- logs
    |- plugin
配置文件

打开 "conf/example/instance.properties",先看以下参数:

复制代码
  #数据库路径
  canal.instance.master.address=127.0.0.1:3306
  #监听mysql的binlog日志 注:该处填写内容可使用"show master status"查看
  canal.instance.master.journal.name=mysql-bin.000086
  #mysql binlog日志 监听开始位置
  canal.instance.master.position=423105
  canal.instance.master.timestamp=
  canal.instance.master.gtid=

  # multi stream for polardbx
  canal.instance.multi.stream.on=false

  # table meta tsdb info
  # "元数据存储账号"设置,用于Canal内部元数据存储
  #是否开启元数据管理,true - 开启
  canal.instance.tsdb.enable=true 
  #连接mysql数据库
  canal.instance.tsdb.url=jdbc:mysql://127.0.0.1:3306/test
  #mysql用户名(注:该账号需要有创建、修改数据库表的权限)
  canal.instance.tsdb.dbUsername=root
  #mysql密码
  canal.instance.tsdb.dbPassword=root

  # username/password
  # "MySQL主库认证账号"设置,用于Canal与MySQL主库的连接,需具备"REPLICATION SLAVE"权限
  #mysql用户名
  canal.instance.dbUsername=canal
  #mysql密码
  canal.instance.dbPassword=canal
  canal.instance.connectionCharset = UTF-8
  # enable druid Decrypt database password
  canal.instance.enableDruid=false
注:可能会用到的mysql命令
复制代码
  # 查看当前服务器使用的biglog文件及大小
  show binary logs;

  # 查看最新一个binlog日志文件名称和Position
  show master status;

  # 查看 binlog 日志列表
  show master logs;
canal启动

打开"bin"目录,双击"startup.bat"

查看日志(logs/example/example.log),如下内容,则启动正常
复制代码
  2025-12-17 11:40:51.679 [destination = example , address = /127.0.0.1:3306 , EventParser] WARN  c.a.o.c.p.inbound.mysql.rds.RdsBinlogEventParserProxy - ---> find start position successfully, EntryPosition[included=false,journalName=mysql-bin.000086,position=422919,serverId=1,gtid=,timestamp=1765934352000] cost : 3226ms , the next step is binlog dump
  2025-12-17 11:40:51.701 [destination = example , address = /127.0.0.1:3306 , EventParser] WARN  c.a.otter.canal.parse.inbound.mysql.MysqlConnection - load MySQL @@version_comment : MySQL Community Server (GPL)

所遇问题

1、mysql连接失败(查看的日志路径:logs/example/example.log)

报错:

复制代码
  ### Error querying database.  Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is com.alibaba.druid.pool.DataSourceClosedException: dataSource already closed at Wed Dec 17 11:08:56 CST 2025
  ### The error may exist in spring/tsdb/sql-map/sqlmap_snapshot.xml
  ### The error may involve com.alibaba.otter.canal.parse.inbound.mysql.tsdb.dao.MetaSnapshotMapper.findByTimestamp
  ### The error occurred while executing a query

原因:

因为1.1.8目前默认使用的是mysql5.0的驱动,但是我的是mysql8,所以需要换驱动

解决方法:

复制代码
  1、下载驱动,路径"https://downloads.mysql.com/archives/c-j/",选择相应的mysql版本,"Operating System"选择"Platform Independent"
  2、解压文件,将得到的".jar"文件,复制到canal目录下的"lib"中
  3、修改文件配置的驱动
    a、打开"conf/canal.properties"文件,然后找到参数"canal.instance.tsdb.spring.xml",值"spring/tsdb/h2-tsdb.xml",则修改文件的路径为"conf/spring/tsdb/h2-tsdb.xml"
    b、打开"conf/spring/tsdb/h2-tsdb.xml"文件,找到"<property name="driverClassName" value="org.h2.Driver" />"
    c、将"value"值改为"com.mysql.cj.jdbc.Driver"
2、mysql用户权限问题

报错:

复制代码
  ### Error querying database.  Cause: java.sql.SQLSyntaxErrorException: Table 'test.meta_snapshot' doesn't exist
  ### The error may exist in spring/tsdb/sql-map/sqlmap_snapshot.xml
  ### The error may involve com.alibaba.otter.canal.parse.inbound.mysql.tsdb.dao.MetaSnapshotMapper.findByTimestamp-Inline
  ### The error occurred while setting parameters

原因:

在"conf/example/instance.properties"配置的参数"canal.instance.tsdb.dbUsername",值为"canan",也就是使用的是mysql中的"canal"账号,之前"canal"账号分配的权限并没有创建表的权限,所以查询时会报表不存在

解决方法:

更换为有创建表权限的用户或者是给"canal"添加相应权限(注:由于是在测试环境,所有我直接使用了mysql默认账号"root")

相关推荐
Bert.Cai23 分钟前
MySQL CURTIME()函数详解
数据库·mysql
Bert.Cai24 分钟前
MySQL CURDATE()函数详解
数据库·mysql
NGSI vimp37 分钟前
MySQL|MySQL 中 `DATE_FORMAT()` 函数的使用
数据库·mysql
秋939 分钟前
MySQL8.0.46 与 MySQL8.4.9:跨越代际的深度差异解析与升级全指南
mysql
HAWK eoni1 小时前
Mysql 驱动程序
数据库·mysql
xxjj998a1 小时前
Laravel4.x核心特性全解析
android·mysql·laravel
何中应1 小时前
CentOS 7安装、卸载MySQL数据库(二)
数据库·mysql·centos
梁萌2 小时前
mysql使用事件做日志表数据转移
数据库·mysql
lThE ANDE2 小时前
MySQL中的TRUNCATE TABLE命令
数据库·mysql
生而为虫2 小时前
Claude Code 最新版安装教程(Windows/Mac/Linux 全平台) 面向普通用户的 Claude Code 安装与模型接入指南
linux·windows·macos