当前位置: 首页 > news >正文

canal安装使用

简介

canal [kə'næl],译意为水道/管道/沟渠,主要用途是基于 MySQL 数据库增量日志解析,提供增量数据订阅和消费

工作原理

canal 模拟 MySQL slave 的交互协议,伪装自己为 MySQL slave ,向 MySQL master 发送 dump 协议
MySQL master 收到 dump 请求,开始推送 binary log 给 slave (即 canal )
canal 解析 binary log 对象(原始为 byte 流)

QuickStart

SHOW VARIABLES LIKE 'log_bin%'

MySQL , 需要先开启 Binlog 写入功能,配置 binlog-format 为 ROW 模式

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

授权 canal  账号具有slave 的权限

CREATE USER canal IDENTIFIED BY 'canal';  
GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'canal'@'%';
-- GRANT ALL PRIVILEGES ON *.* TO 'canal'@'%' ;
FLUSH PRIVILEGES;

下载

Releases · alibaba/canal · GitHub

# 客户端
canal.adapter-1.1.8-SNAPSHOT.tar.gz
# 管理端
canal.admin-1.1.8-SNAPSHOT.tar.gz
# 服务端
canal.deployer-1.1.8-SNAPSHOT.tar.gz

配置修改

canal.deployer-1.1.8-SNAPSHOT\conf\example\instance.properties

启动startup.bat

Unrecognized VM option 'PermSize=128m'

在jdk8(含)以后,永久代被移除了,所以虚拟机的启动参数MaxPermSize(最大永久代大小)不可用。

去掉 -XX:PermSize=128m,重新启动

ClientSample

创建项目

org.apache.maven.archetypes:maven-archetype-quickstart

依赖配置

    <dependency><groupId>com.alibaba.otter</groupId><artifactId>canal.client</artifactId><version>1.1.7</version></dependency><dependency><groupId>com.alibaba.otter</groupId><artifactId>canal.protocol</artifactId><version>1.1.7</version></dependency>

ClientSample.java

package org.example;import com.alibaba.otter.canal.client.CanalConnector;
import com.alibaba.otter.canal.client.CanalConnectors;
import com.alibaba.otter.canal.common.utils.AddressUtils;
import com.alibaba.otter.canal.protocol.CanalEntry.*;
import com.alibaba.otter.canal.protocol.Message;import java.net.InetSocketAddress;
import java.util.List;public class SimpleCanalClientExample {public static void main(String args[]) {// 创建链接CanalConnector connector = CanalConnectors.newSingleConnector(new InetSocketAddress(AddressUtils.getHostIp(),11111), "example", "", "");int batchSize = 1000;int emptyCount = 0;try {connector.connect();connector.subscribe(".*\\..*");connector.rollback();int totalEmptyCount = 120;while (emptyCount < totalEmptyCount) {Message message = connector.getWithoutAck(batchSize); // 获取指定数量的数据long batchId = message.getId();int size = message.getEntries().size();if (batchId == -1 || size == 0) {emptyCount++;System.out.println("empty count : " + emptyCount);try {Thread.sleep(1000);} catch (InterruptedException e) {}} else {emptyCount = 0;// System.out.printf("message[batchId=%s,size=%s] \n", batchId, size);printEntry(message.getEntries());}connector.ack(batchId); // 提交确认// connector.rollback(batchId); // 处理失败, 回滚数据}System.out.println("empty too many times, exit");} finally {connector.disconnect();}}private static void printEntry(List<Entry> entrys) {for (Entry entry : entrys) {if (entry.getEntryType() == EntryType.TRANSACTIONBEGIN || entry.getEntryType() == EntryType.TRANSACTIONEND) {continue;}RowChange rowChage = null;try {rowChage = RowChange.parseFrom(entry.getStoreValue());} catch (Exception e) {throw new RuntimeException("ERROR ## parser of eromanga-event has an error , data:" + entry.toString(),e);}EventType eventType = rowChage.getEventType();System.out.println(String.format("================&gt; binlog[%s:%s] , name[%s,%s] , eventType : %s",entry.getHeader().getLogfileName(), entry.getHeader().getLogfileOffset(),entry.getHeader().getSchemaName(), entry.getHeader().getTableName(),eventType));for (RowData rowData : rowChage.getRowDatasList()) {if (eventType == EventType.DELETE) {printColumn(rowData.getBeforeColumnsList());} else if (eventType == EventType.INSERT) {printColumn(rowData.getAfterColumnsList());} else {System.out.println("-------&gt; before");printColumn(rowData.getBeforeColumnsList());System.out.println("-------&gt; after");printColumn(rowData.getAfterColumnsList());}}}}private static void printColumn(List<Column> columns) {for (Column column : columns) {System.out.println(column.getName() + " : " + column.getValue() + "    update=" + column.getUpdated());}}}

Canal Admin QuickStart

配置修改

canal.admin-1.1.8-SNAPSHOT\conf\application.yml

导入数据

source canal_manager.sql

启动startup.bat

http://127.0.0.1:8089/ 访问,默认密码:admin/123456

canal_local.properties

# register ip
canal.register.ip =# canal admin config
canal.admin.manager = 127.0.0.1:8089
canal.admin.port = 11110
canal.admin.user = admin
canal.admin.passwd = 4ACFE3202A5FF5CF467898FC58AAB1D615029441
# admin auto register
canal.admin.register.auto = true
canal.admin.register.cluster = 

select password('admin')

+-------------------------------------------+
| password('admin')                         |
+-------------------------------------------+
| *4ACFE3202A5FF5CF467898FC58AAB1D615029441 |
+-------------------------------------------+

# 如果遇到mysql8.0,可以使用select upper(sha1(unhex(sha1('admin'))))

bin/startup.bat local


http://www.mrgr.cn/news/80140.html

相关文章:

  • elementUI修改table样式
  • 大模型之prompt
  • MySQL执行计划分析
  • Unity屏幕截图、区域截图、读取图片、WebGL长截屏并下载到本地jpg
  • Spring Cloud微服务架构
  • ️️️ 避坑指南:如何修复国密gmssl 库填充问题并提炼优秀加密实践20241212
  • [C++]多态
  • ORB-SLAM3源码学习:G2oTypes.cc: void EdgeInertial::computeError 计算预积分残差
  • 代码随想录算法训练营第一天 | 数组理论基础,977.有序数组平方结果再排序
  • 玛哈特精密矫平机:制造业的隐秘
  • C语言数组和字符串笔记
  • 51c大模型~合集89
  • 考研数学【线性代数基础box(数二)】
  • ubuntu20.04+ROS Noetic 安装PX4+Mavros
  • Vue基础记录
  • 海康萤石摄像机接入EasyNVR流程:开启RTSP-》萤石视频添加到EasyNVR-》未来支持海康SDK协议添加到EasyNVR
  • 代码随想录算法训练营第三天 | 链表理论基础 | 707.设计链表
  • 从整个SecurityFilterChain的角度,一个请求经过Spring Security的流程
  • L23.【LeetCode笔记】验证回文串(剖析几种解法)
  • Spring 如何解决循环依赖?
  • Linux下socket广播通讯的实现
  • 图像融合算法笔记2024 CDTNet
  • pytorch_fid 安装笔记
  • 【AIGC】ChatGPT保护指令:高效提升GPTs提示词与知识库文件的安全性
  • 【PyCharm调试】显示一个对象值时会调用的方法
  • 长短期记忆神经网络(LSTM)介绍