项目需求:
使用canal监控mysql某个库某个表,或者多个库,多个表---- update/inster/create 操作,
系统版本mysql版本java版本canal版本rabbitMQ版本Rocky 9.2MySQL 8.0.26openjdk 11.0.221.1.6rabbitmq-server 3.12.4
mysql 配置搭建
作者使用的mysql配置文件是上述文章里面 { mysql 主从同步 主节点 配置文件 }
MySQL 配置
开启 binlog 首先在 mysql 的配置文件目录中查找配置文件 my.cnf
(mysql如果有主从的话,这一步可略过,canal 在mysql 主库搭建)
vim my.cnf
server-id=1#master端的ID号【必须是唯一的】;log_bin=mysql-bin #同步的日志路径,一定注意这个目录要是mysql有权限写入的
binlog-format=row #行级,记录每次操作后每行记录的变化。
mysql 创建canal用户
CREATE USER canal IDENTIFIED BY 'canal';#创建用户名和密码都为 canal 的用户
GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'canal'@'%';#授予该用户对所有数据库和表的查询、复制主节点数据的操作权限
FLUSH PRIVILEGES;#重新加载权限
RabbitMQ 配置
canal软件下载
## canal-deploy:
可以直接监听MySQL的binlog,把自己伪装成MySQL的从库,只负责接收数据,并不做处理。
## canal-admin:
为canal提供整体配置管理、节点运维等面向运维的功能,提供相对友好的WebUI操作界面,
方便更多用户快速和安全的操作。
## canal-adapter:相当于canal的客户端,会从canal-server中获取数据,然后对数据进行同步,
可以同步到MySQL、Elasticsearch和HBase等存储中去。在1.1.8版本中 canal-adapter 文件不更新了
## canal.example: 示例程序。
# 组件下载canal-deploy、canal-admin
wget https://github.com/alibaba/canal/releases/download/canal-1.1.6-alpha-2/canal.deployer-1.1.6-SNAPSHOT.tar.gz
wget https://github.com/alibaba/canal/releases/download/canal-1.1.6-alpha-2/canal.admin-1.1.6-SNAPSHOT.tar.gz
wget https://github.com/alibaba/canal/releases/download/canal-1.1.6-alpha-1/canal.example-1.1.6-SNAPSHOT.tar.gz
目前环境只用到了 canal-deploy 及 canal-admin
https://github.com/alibaba/canal/releases/
环境部署
安装canal-admin
安装 java
yum -yinstalljava
安装canal
将下载好的安装包 解压到/usr/local/下
mkdir /usr/local/canal-admin/
mkdir /usr/local/canal/
tar-xf canal.deployer-1.1.6-SNAPSHOT.tar.gz -C /usr/local/canal/
tar-xf canal.admin-1.1.6-SNAPSHOT.tar.gz -C /usr/local/canal-admin/
配置环境:
cd /usr/local/canal-admin/
vim conf/application.yml
server:
port: 8089#网页访问端口
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
spring.datasource:
address: 127.0.0.1:3306 # 数据库端口
database: canal_manager # canal的数据库
username: root #数据库账号,这个账号必须有增删改查的权限,因为要往里面写数据。
password: 123456# 数据库密码
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://${spring.datasource.address}/${spring.datasource.database}?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true
hikari:
maximum-pool-size: 30
minimum-idle: 1# canal 这个类似于 api接口使用的 需要和 canal中的conf/canal.properties 对应上# canal.properties 中是密文
canal:
adminUser: admin
adminPasswd: admin
:wq (保存退出)# 将canal_manager库导入到mysql表中
mysql -uroot-p123456source /usr/local/canal-admin/conf/canal_manager.sql
导入完成,退出mysql
启动 canal-admin
./bin/startup.sh
tail-f logs/admin.log
访问:http://IP:8089
账号:admin
密码:123456 默认
安装canal
修改配置一:
cd /usr/local/canal
vim conf/canal.properties
#################################################
######### common argument #############
#################################################
# tcp bind ip
canal.ip =
# register ip to zookeeper
canal.register.ip =
# Canal 服务的主要端口,通常用于数据库变更事件的监听和同步。
canal.port = 11111
# Canal 用于指标拉取的端口,用于监控和收集指标数据
canal.metrics.pull.port = 11112
# canal instance user/passwd
# canal.user = canal
# canal.passwd = E3619321C1A937C46A0D8BD1DAC39F93B27D4458
# canal admin config
#Canal-admin 访问IP及端口
#canal.admin.manager = 127.0.0.1:8089
canal.admin.port = 11110
# Canal 管理端口,用于管理和配置 Canal 实例。
canal.admin.user = admin
# 对应上面application.yml 配置
# 4ACF~441 则是admin 密文
# canal.admin.passwd = 6bb4837eb74329105ee4568dda7dc67ed2ca2ad9 123456
canal.admin.passwd = 4ACFE3202A5FF5CF467898FC58AAB1D615029441
# admin auto register
#canal.admin.register.auto = true
# 是否开启自动注册模式
#canal.admin.register.cluster =
#canal.admin.register.name =
canal.zkServers =
# flush data to zk
canal.zookeeper.flush.period = 1000
canal.withoutNetty = false
# tcp, kafka, rocketMQ, rabbitMQ
# 修改为rabbitmq 下来就直接修改最后几条配置即可
canal.serverMode = rabbitMQ
# flush meta cursor/parse position to file
canal.file.data.dir = ${canal.conf.dir}
canal.file.flush.period = 1000
## memory store RingBuffer size, should be Math.pow(2,n)
canal.instance.memory.buffer.size = 16384
## memory store RingBuffer used memory unit size , default 1kb
canal.instance.memory.buffer.memunit = 1024
## meory store gets mode used MEMSIZE or ITEMSIZE
canal.instance.memory.batch.mode = MEMSIZE
canal.instance.memory.rawEntry = true
## detecing config
canal.instance.detecting.enable = false
#canal.instance.detecting.sql = insert into retl.xdual values(1,now()) on duplicate key update x=now()
canal.instance.detecting.sql = select 1
canal.instance.detecting.interval.time = 3
canal.instance.detecting.retry.threshold = 3
canal.instance.detecting.heartbeatHaEnable = false
# support maximum transaction size, more than the size of the transaction will be cut into multiple transactions delivery
canal.instance.transaction.size = 1024
# mysql fallback connected to new master should fallback times
canal.instance.fallbackIntervalInSeconds = 60
# network config
canal.instance.network.receiveBufferSize = 16384
canal.instance.network.sendBufferSize = 16384
canal.instance.network.soTimeout = 30
# binlog filter config
canal.instance.filter.druid.ddl = true
canal.instance.filter.query.dcl = false
canal.instance.filter.query.dml = false
canal.instance.filter.query.ddl = false
canal.instance.filter.table.error = false
canal.instance.filter.rows = false
canal.instance.filter.transaction.entry = false
canal.instance.filter.dml.insert = false
canal.instance.filter.dml.update = false
canal.instance.filter.dml.delete = false
# binlog format/image check
canal.instance.binlog.format = ROW,STATEMENT,MIXED
canal.instance.binlog.image = FULL,MINIMAL,NOBLOB
# binlog ddl isolation
canal.instance.get.ddl.isolation = false
# parallel parser config
canal.instance.parser.parallel = true
## concurrent thread number, default 60% available processors, suggest not to exceed Runtime.getRuntime().availableProcessors()
#canal.instance.parser.parallelThreadSize = 16
## disruptor ringbuffer size, must be power of 2
canal.instance.parser.parallelBufferSize = 256
# table meta tsdb info
canal.instance.tsdb.enable = true
canal.instance.tsdb.dir = ${canal.file.data.dir:../conf}/${canal.instance.destination:}
canal.instance.tsdb.url = jdbc:h2:${canal.instance.tsdb.dir}/h2;CACHE_SIZE=1000;MODE=MYSQL;
canal.instance.tsdb.dbUsername = canal
canal.instance.tsdb.dbPassword = canal
# dump snapshot interval, default 24 hour
canal.instance.tsdb.snapshot.interval = 24
# purge snapshot expire , default 360 hour(15 days)
canal.instance.tsdb.snapshot.expire = 360
#################################################
######### destinations #############
#################################################
canal.destinations =
# conf root dir
canal.conf.dir = ../conf
# auto scan instance dir add/remove and start/stop instance
canal.auto.scan = true
canal.auto.scan.interval = 5
# set this value to 'true' means that when binlog pos not found, skip to latest.
# WARN: pls keep 'false' in production env, or if you know what you want.
canal.auto.reset.latest.pos.mode = false
canal.instance.tsdb.spring.xml = classpath:spring/tsdb/h2-tsdb.xml
#canal.instance.tsdb.spring.xml = classpath:spring/tsdb/mysql-tsdb.xml
canal.instance.global.mode = manager
canal.instance.global.lazy = false
canal.instance.global.manager.address = ${canal.admin.manager}
#canal.instance.global.spring.xml = classpath:spring/memory-instance.xml
canal.instance.global.spring.xml = classpath:spring/file-instance.xml
#canal.instance.global.spring.xml = classpath:spring/default-instance.xml
##################################################
######### MQ Properties #############
##################################################
# aliyun ak/sk , support rds/mq
canal.aliyun.accessKey =
canal.aliyun.secretKey =
canal.aliyun.uid=
canal.mq.flatMessage = true
canal.mq.canalBatchSize = 50
canal.mq.canalGetTimeout = 100
# Set this value to "cloud", if you want open message trace feature in aliyun.
canal.mq.accessChannel = local
canal.mq.database.hash = true
canal.mq.send.thread.size = 30
canal.mq.build.thread.size = 8
##################################################
######### Kafka #############
##################################################
kafka.bootstrap.servers = 127.0.0.1:6667
kafka.acks = all
kafka.compression.type = none
kafka.batch.size = 16384
kafka.linger.ms = 1
kafka.max.request.size = 1048576
kafka.buffer.memory = 33554432
kafka.max.in.flight.requests.per.connection = 1
kafka.retries = 0
kafka.kerberos.enable = false
kafka.kerberos.krb5.file = "../conf/kerberos/krb5.conf"
kafka.kerberos.jaas.file = "../conf/kerberos/jaas.conf"
##################################################
######### RocketMQ #############
##################################################
rocketmq.producer.group = test
rocketmq.enable.message.trace = false
rocketmq.customized.trace.topic =
rocketmq.namespace =
rocketmq.namesrv.addr = 127.0.0.1:9876
rocketmq.retry.times.when.send.failed = 0
rocketmq.vip.channel.enabled = false
rocketmq.tag =
##################################################
######### RabbitMQ #############
##################################################
# MQ地址及端口
rabbitmq.host = 127.0.0.1:5672
# 虚拟主机
rabbitmq.virtual.host = canal
# 交换机名称
rabbitmq.exchange = ex_canal
# mq账号
rabbitmq.username = canal
# mq 密码
rabbitmq.password = canal
# 以上几个值是在MQ中自建的值 如下图示例
rabbitmq.deliveryMode =
修改配置二:
vim conf/canal_local.properties
# register ip
canal.register.ip =
# canal admin config
canal.admin.manager = 127.0.0.1:8089
#Canal-admin 访问IP及端口
canal.admin.port = 11110
# Canal 管理端口,用于管理和配置 Canal 实例。
canal.admin.user = admin
# 对应上面application.yml 配置
canal.admin.passwd = 4ACFE3202A5FF5CF467898FC58AAB1D615029441
# 4ACF~441 则是admin 密文
#canal.admin.passwd = 6bb4837eb74329105ee4568dda7dc67ed2ca2ad9
# admin auto register
canal.admin.register.auto = true
canal.admin.register.cluster =
canal.admin.register.name =
修改配置三:
vim conf/example/instance.properties
#################################################
## mysql serverId , v1.0.26+ will autoGen
# canal.instance.mysql.slaveId=0
# enable gtid use true/false
canal.instance.gtidon=false
# position info
canal.instance.master.address=127.0.0.1:3306
# 监控mysql的地址,可以换成其他地址
canal.instance.master.journal.name=
canal.instance.master.position=
canal.instance.master.timestamp=
canal.instance.master.gtid=
# rds oss binlog
canal.instance.rds.accesskey=
canal.instance.rds.secretkey=
canal.instance.rds.instanceId=
# table meta tsdb info
canal.instance.tsdb.enable=true
#canal.instance.tsdb.url=jdbc:mysql://127.0.0.1:3306/canal_tsdb
#canal.instance.tsdb.dbUsername=canal
#canal.instance.tsdb.dbPassword=canal
#canal.instance.standby.address =
#canal.instance.standby.journal.name =
#canal.instance.standby.position =
#canal.instance.standby.timestamp =
#canal.instance.standby.gtid=
# username/password
canal.instance.dbUsername=canal
# mysql 账户
canal.instance.dbPassword=canal
# mysql 密码
# 如果账户密码没有,根据上面的命令创建
canal.instance.connectionCharset = UTF-8
# enable druid Decrypt database password
canal.instance.enableDruid=false
#canal.instance.pwdPublicKey=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALK4BUxdDltRRE5/zXpVEVPUgunvscYFtEip3pmLlhrWpacX7y7GCMo2/JM6LeHmiiNdH1FWgGCpUfircSwlWKUCAwEAAQ==
# table regex
# canal.instance.filter.regex=.*\\..*
# canal.instance.filter.regex=dt\\.orders,dt\\.user,dt\\.user_equity_summary,dt\\.user_statistics,dt\\.user_equity_summary,dt_logs\\..*
# 如上注释:监控dt库下的orders表,dt库下的user表..... dt_logs库下的所有表
canal.instance.filter.regex=dt_test\\..*
# 需要监控的数据库,监控dt_test库下面的所有表
# table black regex
# 排除一些不需要同步的数据库表,避免不必要的数据传输和处理,提高性能和效率。
# 注解:排除mysql库中的slave_.* 开头的表
canal.instance.filter.black.regex=mysql\\.slave_.*
# table field filter(format: schema1.tableName1:field1/field2,schema2.tableName2:field1/field2)
#canal.instance.filter.field=test1.t_product:id/subject/keywords,test2.t_company:id/name/contact/ch
# table field black filter(format: schema1.tableName1:field1/field2,schema2.tableName2:field1/field2)
#canal.instance.filter.black.field=test1.t_product:subject/product_image,test2.t_company:id/name/contact/ch
# mq config
# 配置 canal.mq.topic 属性,指定 Canal 将数据变更事件发送到消息队列时所使用的主题名称。
# canal.mq.topic=canal_crm
# dynamic topic route by schema or table regex
#canal.mq.dynamicTopic=mytest1.user,topic2:mytest2\\..*,.*\\..*
#canal.mq.dynamicTopic=dt:dt\\..*,dt:dt_.*\\..*
# Canal 将根据一定的规则动态生成主题名称,并将数据变更事件发送到对应的动态主题中。
# 将以 dt_test. 开头的表名映射到MQ名为 canal 的 topic 上,多个已逗号分割,如上
canal.mq.dynamicTopic=canal:dt_test\\..*
canal.mq.partition=0
# hash partition config
#canal.mq.enableDynamicQueuePartition=false
#canal.mq.partitionsNum=3
#canal.mq.dynamicTopicPartitionNum=test.*:4,mycanal:6
#canal.mq.partitionHash=test.table:id^name,.*\\..*
#################################################
创建rabbitMQ队列及关联关系
登录到mq管理台界面
http://IP:15672
账号:admin密码:Admin@123
创建mq账号 mq 密码 创建MQ 虚拟主机
原始界面:图一
创建mq账号 mq 密码
创建虚拟主机 虚拟主机名称要和 rabbitmq.virtual.host = canal 对应上
创建交换机名称
rabbitmq.exchange = ex_canal 交换机名称要和配置文件对应上
创建队列
绑定关联关系
点击 Bingings 创建关联关系
在队列中也能查看到(图三、图四)
启动canal
cd /usr/local/canal/
./bin/startup.sh local(加 local 启动)tail-f logs/canal/canal.log
报错:
[main] ERROR com.alibaba.otter.canal.deployer.CanalLauncher - ## Something goes wrong when starting up the canal Server:
com.alibaba.otter.canal.common.CanalException: load manager config failed.
Caused by: com.alibaba.otter.canal.common.CanalException: requestGet for canal config error: auth :admin is failed
说明:canal 配置不对
canal.properties canal_local.properties 两个配置文件中的 密文密码与 canal-admin/conf/application.yml 明文没对应上
canal.admin.passwd = 4ACFE3202A5FF5CF467898FC58AAB1D615029441
# 4ACFE3202A5FF5CF467898FC58AAB1D615029441 为 admin 密文。但是作者也不知道用的是什么加密方式 所以目前还不能修改、、、
报错:
ERROR com.alibaba.otter.canal.deployer.CanalLauncher - ## Something goes wrong when starting up the canal Server:
java.lang.NullPointerException: null 说明版本不对。
然后重启:
./bin/stop.sh
./bin/startup.sh local
注释:
如果使用canal-admin界面管理,最好是用 ./bin/startup.sh local
启动,因为这样启动之后,配置文件会生成到数据库中,界面也可以添加,删除配置如果不用 local 启动,每次修改得手动修改 /usr/local/canal/conf/下的文件;为了管理方便我们还是使用
./bin/startup.sh local 启动。
如果启动时不想加local,而且也想使用界面管理,也可以。只是在修改canal配置文件时
[root@mq conf]# cd /usr/local/canal/conf[root@mq conf]# ls
canal.properties canal_local.properties example logback.xml metrics spring
[root@mq conf]# mv canal.properties canal.properties.bak[root@mq conf]# mv canal_local.properties canal.properties[root@mq conf]# vim canal.properties (配置就是上面的修改配置二:即可)
然后只修改这个一个配置文件即可,启动时直接
[root@mq conf]# ../bin/startup.sh
查看进程
canal 端口 如下:
[root@mq canal]# ss -ntulp | grep 1111
tcp LISTEN 0 3 0.0.0.0:11112 0.0.0.0:* users:(("java",pid=33308,fd=81))
tcp LISTEN 0 50 0.0.0.0:11111 0.0.0.0:* users:(("java",pid=33308,fd=86))
tcp LISTEN 0 50 0.0.0.0:11110 0.0.0.0:* users:(("java",pid=33308,fd=90))
canal-admin 端口如下:
[root@mq canal]# ss -ntulp | grep 8089
tcp LISTEN 0 100 0.0.0.0:8089 0.0.0.0:* users:(("java",pid=23996,fd=108))
rabbitmq 端口如下:
[root@mq canal]# ss -ntulp | grep 5672
tcp LISTEN 0 1024 0.0.0.0:15672 0.0.0.0:* users:(("beam.smp",pid=21252,fd=35))
tcp LISTEN 0 128 0.0.0.0:25672 0.0.0.0:* users:(("beam.smp",pid=21252,fd=18))
tcp LISTEN 0 128 *:5672 *:* users:(("beam.smp",pid=21252,fd=36))
访问 canal-admin
http://IP:8089 admin 123456
创建 instance
显示启动,及为成功。
登录到mysql 创建数据库,表,插入数据。
CREATE DATABASE dt_test;
CREATE TABLE dt_test.users (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT
);
INSERT INTO dt_test.users (id, name, age) VALUES (19, 'Alice', 30);
登录到rabbitMQ 界面
消费MQ日志:
最后献上 看守脚本
cat /usr/local/src/canal.sh
#!/bin/bashcanal=`ps-aux|grep-Ew"canal/bin"|grep-vgrep|wc-l`if[$canal-eq0];then
/usr/local/canal/bin/stop.sh
/usr/local/canal/bin/startup.sh localifcat /usr/local/src/canal_admin.sh
#!/bin/bashcanal=`ps-aux|grep-Ew'canal-admin'|grep-vgrep|wc-l`if[$canal-eq0];then
/usr/local/canal-admin/bin/stop.sh
/usr/local/canal-admin/bin/startup.sh
fichmod755 /usr/local/src/canal_admin.sh
chmod755 /usr/local/src/canal.sh
crontab-l
* * * * * /usr/local/src/canal.sh
* * * * * /usr/local/src/canal_admin.sh
版权归原作者 花晓木 所有, 如有侵权,请联系我们删除。