主要實現思路
- 1、在clickhouse中建立MySQL引擎表。
- 2、根據MySQL引擎表的資訊建立目標表。
- 3、實現canal實時增量同步MySQL資料到clickhouse。
MySQL 的準備
修改配置檔案開啟 Binlog
[root@hadoop100 module]$ sudo vim /etc/my.cnf
server-id=1
log-bin=mysql-bin
binlog_format=row
binlog-do-db=test
- 注意:binlog-do-db 根據自己的情況進行修改,指定具體要同步的資料庫,如果不配置則表示所有資料庫均開啟 Binlog。
MySQL中需要同步的庫表
MySQL [testck]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| innodb |
| mysql |
| performance_schema |
| sys |
| testck |
| tmp |
+--------------------+
11 rows in set (0.00 sec)
MySQL [testck]> use testck;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MySQL [testck]> show tables;
+------------------+
| Tables_in_testck |
+------------------+
| t_organization |
| t_user |
+------------------+
2 rows in set (0.00 sec)
MySQL [testck]>
clickhouse 的準備
- canal實時同步MySQL的資料需要在clickhouse中提前建好庫表
在clickhouse建MySQL引擎的庫
- 把MySQL某個庫中的所有表結構資訊對映到clickhouse中,這樣在clickhouse中就可以遠端操作MySQL。
cnnxpredn02 :) CREATE DATABASE t_tmp ENGINE = MySQL('ip:3306', 'testck', 'user', 'pass');
CREATE DATABASE t_tmp
ENGINE = MySQL('ip:3306', 'testck', 'user', 'pass')
Query id: 9f6d7179-3c97-47c2-93ea-abc0c6ca873b
Ok.
0 rows in set. Elapsed: 0.013 sec.
cnnxpredn02 :) show databases;
SHOW DATABASES
Query id: 5b5baaf5-86f5-46c1-ac34-2618c34462f1
┌─name────┐
│ default │
│ system │
│ t_tmp │
└─────────┘
3 rows in set. Elapsed: 0.009 sec.
cnnxpredn02 :)
建立clickhouse中的庫
cnnxpredn02 :) create database testck;
CREATE DATABASE testck
Query id: 397261c0-a8f0-48c6-b4f6-71d121b975b8
Ok.
0 rows in set. Elapsed: 0.004 sec.
cnnxvopredn02 :) show databases;
SHOW DATABASES
Query id: f467f4bb-99fa-4322-a3c1-e33be74b6e81
┌─name────┐
│ default │
│ system │
│ t_tmp │
│ testck │
└─────────┘
4 rows in set. Elapsed: 0.002 sec.
cnnxvpredn02 :)
根據建立的MySQL引擎表建立clickhouse目標表結構
cnnxvpredn02 :) create table testck.t_organization as t_tmp.t_organization;
:-] create table testck.t_user as t_tmp.t_user;
CREATE TABLE testck.t_organization AS t_tmp.t_organization
Query id: f942cf5d-701f-4dbd-9ffd-de2206eec851
Ok.
0 rows in set. Elapsed: 0.006 sec.
CREATE TABLE testck.t_user AS t_tmp.t_user
Query id: ef7dddd7-64b2-4dbc-88be-a105b49aff57
Ok.
0 rows in set. Elapsed: 0.004 sec.
cnnxvopredn02 :)
Canal 的下載和安
下載並解壓 Jar 包
- 下載canal:https://github.com/alibaba/canal/releases
- 注意:canal 解壓後是分散的,我們在指定解壓目錄的時候需要將 canal 指定上
mkdir /opt/module/canal
tar -zxvf canal.deployer-1.1.2.tar.gz -C /opt/module/canal
修改 canal.properties 的配置
#################################################
######### common argument #############
#################################################
# tcp bind ip
canal.ip =
# register ip to zookeeper
canal.register.ip =
canal.port = 11111
canal.metrics.pull.port = 11112
# canal instance user/passwd
# canal.user = canal
# canal.passwd = E3619321C1A937C46A0D8BD1DAC39F93B27D4458
# 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 =
#canal.admin.register.name =
canal.zkServers =
# flush data to zk
canal.zookeeper.flush.period = 1000
canal.withoutNetty = false
# tcp, kafka, rocketMQ, rabbitMQ
canal.serverMode = tcp
# 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的基本通用配置,canal埠號預設就是 11111,修改canal的輸出model,預設tcp,改為輸出到 kafka多例項配置如果建立多個例項,透過前面canal架構,我們可以知道,一個canal服務中可以有多個instance,conf/下的每一個example即是一個例項,每個例項下面都有獨立的配置檔案。預設只有一個例項example,如果需要多個例項處理不同的 MySQL 資料的話,直接複製出多個 example,並對其重新命名,命名和配置檔案中指定的名稱一致,然後修改
canal.properties 中的 canal.destinations=例項 1,例項 2,例項 3。
#################################################
######### destinations #############
#################################################
canal.destinations = example
修改 instance.properties
配置 MySQL 伺服器地址
#################################################
## mysql serverId , v1.0.26+ will autoGen
canal.instance.mysql.slaveId=20
# enable gtid use true/false
canal.instance.gtidon=false
# position info
canal.instance.master.address=hadoop100:3306
配置連線 MySQL 的使用者名稱和密碼,預設就是我們前面授權的 canal
# username/password
canal.instance.dbUsername=canal
canal.instance.dbPassword=canal
canal.instance.connectionCharset = UTF-8
canal.instance.defaultDatabaseName =test
# enable druid Decrypt database password
canal.instance.enableDruid=false
啟動服務
bin/startup.sh
[root@cnnxpredn02 canal]# cat canal.log
2022-03-07 08:47:21.349 [main] INFO com.alibaba.otter.canal.deployer.CanalLauncher - ## set default uncaught exception handler
2022-03-07 08:47:21.398 [main] INFO com.alibaba.otter.canal.deployer.CanalLauncher - ## load canal configurations
2022-03-07 08:47:21.415 [main] INFO com.alibaba.otter.canal.deployer.CanalStarter - ## start the canal server.
2022-03-07 08:47:21.475 [main] INFO com.alibaba.otter.canal.deployer.CanalController - ## start the canal server):11111]
2022-03-07 08:47:23.137 [main] INFO com.alibaba.otter.canal.deployer.CanalStarter - ## the canal server is running now ......
canal-adapter 的下載和安裝
下載並解
- 下載canal-adapter: https://github.com/alibaba/canal/releases
- 注意:canal-adapter解壓後是分散的,我們在指定解壓目錄的時候需要將 canal-adapter 指定上
mkdir canal-adapter
tar -zxvf canal.adapter-1.1.5.tar.gz -C /../canal-adapter
在application.yml中配置MySQL和clickhouse的連線資訊
...
# srcDataSources:
defaultDS:
url: jdbc:mysql://ip:3306/database?useUnicode=true
username: user
password: pass
canalAdapters:
- instance: test # canal instance Name or mq topic name
groups:
- groupId: g1
outerAdapters:
- name: logger
- name: rdb
key: mysql1
properties:
jdbc.driverClassName: ru.yandex.clickhouse.ClickHouseDriver
jdbc.url: jdbc:clickhouse://ip:port/database
jdbc.username: user
jdbc.password: pass
# - name: rdb
...
在/opt/soft/canal-adapter/conf/rdb/mytest_user.yml中配置需要同步的庫表資訊
- canal可以同步整個庫的所有表和單表的表,配置都在這裡進行配置,如果需要同步多張表的資料,就在rdb下建立多個類似的配置檔案,檔名可以用表名來命名。
Mirror schema synchronize config
dataSourceKey: defaultDS
destination: test
groupId: g1
outerAdapterKey: mysql1
concurrent: true
dbMapping:
mirrorDb: true
database: testck
啟動canal-adapter服務
bin/startup.sh
- 在/.../canal-adapter/logs/adapter下檢視日誌