作者简介:赵志勇, oracle 、 mysql DBA。
1.简介
基于 OceanBase 的 CDC 功能实现 OceanBase 数据库数据实时同步到 MySQL 数据库中。
数据链路:
ob_cluster -> oblogreader ->oblogmsg -> canal_server -> canal_client -> mysql
2. 环境准备
2.1. OceanBase
已准备好一个3节点的 OceanBase 集群(开源3.1.1版本),并建立了业务租户 obcp_t1 。搭建过程及业务租户操作等可参考官方文档或官方入门教程,地址:https://open.oceanbase.com。
如下图,红色方框部分均有相关的搭建过程介绍。
2.2. MySQL
版本8.0.27,搭建过程省略,具体可参见MySQL官方文档,详见:https://dev.mysql.com/doc/。
3. 配置 oblogproxy
3.1. 安装 oblogproxy
# 配置OB yum源
yum install -y yum-utils
yum-config-manager --add-repo https://mirrors.aliyun.com/oceanbase/OceanBase.repo
# 从 https://github.com/oceanbase/oblogproxy/releases/tag/v1.0.0 下载rpm包
执行安装:yum install oblogproxy-1.0.0-1.el7.x86_64.rpm
# 安装后的信息
rpm -ql oblogproxy
/usr/local/oblogproxy/bin
/usr/local/oblogproxy/bin/logproxy
/usr/local/oblogproxy/conf
/usr/local/oblogproxy/conf/conf.json
/usr/local/oblogproxy/run.sh
# 配置OB yum源
yum install -y yum-utils
yum-config-manager --add-repo https://mirrors.aliyun.com/oceanbase/OceanBase.repo
# 从 https://github.com/oceanbase/oblogproxy/releases/tag/v1.0.0 下载rpm包
执行安装:yum install oblogproxy-1.0.0-1.el7.x86_64.rpm
# 安装后的信息
rpm -ql oblogproxy
/usr/local/oblogproxy/bin
/usr/local/oblogproxy/bin/logproxy
/usr/local/oblogproxy/conf
/usr/local/oblogproxy/conf/conf.json
/usr/local/oblogproxy/run.sh
3.2. 配置系统租户信息
oblogproxy需要ob sys租户的账户和密码密文信息,可通过如下方式获取:
# /usr/local/oblogproxy/bin/logproxy -x root@sys
EA87898018FD1EDDC2AA11CE1556E917
min]# /usr/local/oblogproxy/bin/logproxy -x oracle_4U
B91378831462F9C3E93465FA2C81E60E
# 修改 /usr/local/oblogproxy/conf/conf.json 文件,将上面的密文添加进去。
"ob_sys_username": "EA87898018FD1EDDC2AA11CE1556E917",
"ob_sys_password": "B91378831462F9C3E93465FA2C81E60E",
3.3. 启动 oblogproxy
#1 使用绝对路径启动:(无法启动成功)-- 已在github提交issue
[root@52ff51c2a702 conf]# /usr/local/oblogproxy/run.sh startwork path : /usr/local/oblogproxy
is_running : (6815)/usr/local/oblogproxy logproxy is running !
Terminated
#2 使用相对路径启动:(成功启动)
[root@52ff51c2a702 oblogproxy]# cd /usr/local/oblogproxy/
[root@52ff51c2a702 oblogproxy]# ls
bin conf run.sh
[root@52ff51c2a702 oblogproxy]# bash ./run.sh startwork path : /usr/local/oblogproxy
logproxy started!
#3 查看状态
# bash ./run.sh status
work path : /usr/local/oblogproxy
is_running : (6850)/usr/local/oblogproxy logproxy is running !
status : 1
#1 使用绝对路径启动:(无法启动成功)-- 已在github提交issue
[root@52ff51c2a702 conf]# /usr/local/oblogproxy/run.sh startwork path : /usr/local/oblogproxy
is_running : (6815)/usr/local/oblogproxy logproxy is running !
Terminated
#2 使用相对路径启动:(成功启动)
[root@52ff51c2a702 oblogproxy]# cd /usr/local/oblogproxy/
[root@52ff51c2a702 oblogproxy]# ls
bin conf run.sh
[root@52ff51c2a702 oblogproxy]# bash ./run.sh startwork path : /usr/local/oblogproxy
logproxy started!
#3 查看状态
# bash ./run.sh status
work path : /usr/local/oblogproxy
is_running : (6850)/usr/local/oblogproxy logproxy is running !
status : 1
4. 安装 zookeeper
# 测试,只配置了单节点。
wget https://dlcdn.apache.org/zookeeper/zookeeper-3.6.3/apache-zookeeper-3.6.3-bin.tar.gz
tar -xf apache-zookeeper-3.6.3-bin.tar.gz
cd apache-zookeeper-3.6.3-bin
cp zoo_sample.cfg zoo.cfg
# 修改配置文件
more zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/tmp/zookeeper
clientPort=2181
# 启动zookeeper
bin/zkServer.sh start
5. 配置 canal
5.1. 安装 canal
从 https://github.com/oceanbase/canal/releases 下载canal的ob版本。
本次下载文件为:
canal.deployer-for-ob-rc2.tar.gz
mkdir canal-ob canal-adapter-ob -p
tar xf canal.deployer-for-ob-rc2.tar.gz -C canal-ob/
5.2. 配置 canal 参数
# 修改 /root/canal-ob/conf/canal.properties,主要修改内容如下:
canal.zkServers = 172.20.0.30:2181
canal.destinations = example # 为方便直接使用example
canal.instance.global.spring.xml = classpath:spring/ob-file-instance.xml
canal.serverMode = tcp # 默认提供的是kafka,需要修改。
cd /root/canal-ob/conf/example/
mv ob-instance.properties instance.properties
# 修改集群配置参数,位置 /root/canal-ob/conf/example/instance.properties,主要修改内容如下:
canal.instance.oceanbase.rsList=172.20.0.10:2882:2881,172.20.0.11:2882:2881,172.20.0.12:2882:2881
canal.instance.oceanbase.username=root@obcp_t1
canal.instance.oceanbase.password=oracle_4U
## 其中 172.20.0.10:2882,172.20.0.11:2882,172.20.0.12:2882 ,可以通过登录sys租户执行:show parameters like '%rootservice_list%' 获取到。
# 修改 oblogproxy 参数,位置 /root/canal-ob/conf/example/instance.properties,主要修改内容如下:
canal.instance.oceanbase.logproxy.address=172.20.0.30:2983
# 修改 租户信息,位置 /root/canal-ob/conf/example/instance.properties,主要修改内容如下:
canal.instance.oceanbase.tenant=obcp_t1
# table regex, format: [tenant].[database].[table]
canal.instance.filter.regex=obcp_t1.*.*
5.3. 启动 canal
# /root/canal-ob/bin/startup.sh
# tail -500f /root/canal-ob/logs/canal/canal.log
# tail -500f /root/canal-ob/logs/example/example.log
6. 配置 canal-adapter
6.1. 安装 canal-adapter
从 https://github.com/oceanbase/canal/releases 下载canal的ob版本。
本次下载文件为:
canal.adapter-for-ob-rc2.tar.gz
mkdir canal-ob canal-adapter-ob -p
tar xf canal.adapter-for-ob-rc2.tar.gz -C canal-adapter-ob/
6.2. 配置 canal-adapter 参数
修改后的配置文件如下:
more /root/canal-adapter-ob/conf/application.yml
server:
port: 8081
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
default-property-inclusion: non_null
canal.conf:
mode: tcp #tcp kafka rocketMQ rabbitMQ
flatMessage: true
zookeeperHosts:
syncBatchSize: 1000
retries: 0
timeout:
accessKey:
secretKey:
consumerProperties:
# canal tcp consumer
canal.tcp.server.host: 172.20.0.30:11111
canal.tcp.zookeeper.hosts: 172.20.0.30:2181
canal.tcp.batch.size: 500
canal.tcp.username:
canal.tcp.password:
canalAdapters:
- instance: example # canal instance Name or mq topic name
groups:
- groupId: g1
outerAdapters:
- name: logger
- name: rdb
key: mysql1
properties:
jdbc.driverClassName: com.mysql.jdbc.Driver
jdbc.url: jdbc:mysql://172.20.0.32:3306/mytest2?useUnicode=true&useSSl=false
jdbc.username: root
jdbc.password: password
# 测试,只配置了单节点。
wget https://dlcdn.apache.org/zookeeper/zookeeper-3.6.3/apache-zookeeper-3.6.3-bin.tar.gz
tar -xf apache-zookeeper-3.6.3-bin.tar.gz
cd apache-zookeeper-3.6.3-bin
cp zoo_sample.cfg zoo.cfg
# 修改配置文件
more zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/tmp/zookeeper
clientPort=2181
# 启动zookeeper
bin/zkServer.sh start
5.1. 安装 canal
从 https://github.com/oceanbase/canal/releases 下载canal的ob版本。
本次下载文件为:
canal.deployer-for-ob-rc2.tar.gz
mkdir canal-ob canal-adapter-ob -p
tar xf canal.deployer-for-ob-rc2.tar.gz -C canal-ob/
5.2. 配置 canal 参数
# 修改 /root/canal-ob/conf/canal.properties,主要修改内容如下:
canal.zkServers = 172.20.0.30:2181
canal.destinations = example # 为方便直接使用example
canal.instance.global.spring.xml = classpath:spring/ob-file-instance.xml
canal.serverMode = tcp # 默认提供的是kafka,需要修改。
cd /root/canal-ob/conf/example/
mv ob-instance.properties instance.properties
# 修改集群配置参数,位置 /root/canal-ob/conf/example/instance.properties,主要修改内容如下:
canal.instance.oceanbase.rsList=172.20.0.10:2882:2881,172.20.0.11:2882:2881,172.20.0.12:2882:2881
canal.instance.oceanbase.username=root@obcp_t1
canal.instance.oceanbase.password=oracle_4U
## 其中 172.20.0.10:2882,172.20.0.11:2882,172.20.0.12:2882 ,可以通过登录sys租户执行:show parameters like '%rootservice_list%' 获取到。
# 修改 oblogproxy 参数,位置 /root/canal-ob/conf/example/instance.properties,主要修改内容如下:
canal.instance.oceanbase.logproxy.address=172.20.0.30:2983
# 修改 租户信息,位置 /root/canal-ob/conf/example/instance.properties,主要修改内容如下:
canal.instance.oceanbase.tenant=obcp_t1
# table regex, format: [tenant].[database].[table]
canal.instance.filter.regex=obcp_t1.*.*
5.3. 启动 canal
# /root/canal-ob/bin/startup.sh
# tail -500f /root/canal-ob/logs/canal/canal.log
# tail -500f /root/canal-ob/logs/example/example.log
6. 配置 canal-adapter
6.1. 安装 canal-adapter
从 https://github.com/oceanbase/canal/releases 下载canal的ob版本。
本次下载文件为:
canal.adapter-for-ob-rc2.tar.gz
mkdir canal-ob canal-adapter-ob -p
tar xf canal.adapter-for-ob-rc2.tar.gz -C canal-adapter-ob/
6.2. 配置 canal-adapter 参数
修改后的配置文件如下:
more /root/canal-adapter-ob/conf/application.yml
server:
port: 8081
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
default-property-inclusion: non_null
canal.conf:
mode: tcp #tcp kafka rocketMQ rabbitMQ
flatMessage: true
zookeeperHosts:
syncBatchSize: 1000
retries: 0
timeout:
accessKey:
secretKey:
consumerProperties:
# canal tcp consumer
canal.tcp.server.host: 172.20.0.30:11111
canal.tcp.zookeeper.hosts: 172.20.0.30:2181
canal.tcp.batch.size: 500
canal.tcp.username:
canal.tcp.password:
canalAdapters:
- instance: example # canal instance Name or mq topic name
groups:
- groupId: g1
outerAdapters:
- name: logger
- name: rdb
key: mysql1
properties:
jdbc.driverClassName: com.mysql.jdbc.Driver
jdbc.url: jdbc:mysql://172.20.0.32:3306/mytest2?useUnicode=true&useSSl=false
jdbc.username: root
jdbc.password: password
从 https://github.com/oceanbase/canal/releases 下载canal的ob版本。
本次下载文件为:
canal.deployer-for-ob-rc2.tar.gz
mkdir canal-ob canal-adapter-ob -p
tar xf canal.deployer-for-ob-rc2.tar.gz -C canal-ob/
# 修改 /root/canal-ob/conf/canal.properties,主要修改内容如下:
canal.zkServers = 172.20.0.30:2181
canal.destinations = example # 为方便直接使用example
canal.instance.global.spring.xml = classpath:spring/ob-file-instance.xml
canal.serverMode = tcp # 默认提供的是kafka,需要修改。
cd /root/canal-ob/conf/example/
mv ob-instance.properties instance.properties
# 修改集群配置参数,位置 /root/canal-ob/conf/example/instance.properties,主要修改内容如下:
canal.instance.oceanbase.rsList=172.20.0.10:2882:2881,172.20.0.11:2882:2881,172.20.0.12:2882:2881
canal.instance.oceanbase.username=root@obcp_t1
canal.instance.oceanbase.password=oracle_4U
## 其中 172.20.0.10:2882,172.20.0.11:2882,172.20.0.12:2882 ,可以通过登录sys租户执行:show parameters like '%rootservice_list%' 获取到。
# 修改 oblogproxy 参数,位置 /root/canal-ob/conf/example/instance.properties,主要修改内容如下:
canal.instance.oceanbase.logproxy.address=172.20.0.30:2983
# 修改 租户信息,位置 /root/canal-ob/conf/example/instance.properties,主要修改内容如下:
canal.instance.oceanbase.tenant=obcp_t1
# table regex, format: [tenant].[database].[table]
canal.instance.filter.regex=obcp_t1.*.*
5.3. 启动 canal
# /root/canal-ob/bin/startup.sh
# tail -500f /root/canal-ob/logs/canal/canal.log
# tail -500f /root/canal-ob/logs/example/example.log
6. 配置 canal-adapter
6.1. 安装 canal-adapter
从 https://github.com/oceanbase/canal/releases 下载canal的ob版本。
本次下载文件为:
canal.adapter-for-ob-rc2.tar.gz
mkdir canal-ob canal-adapter-ob -p
tar xf canal.adapter-for-ob-rc2.tar.gz -C canal-adapter-ob/
6.2. 配置 canal-adapter 参数
修改后的配置文件如下:
more /root/canal-adapter-ob/conf/application.yml
server:
port: 8081
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
default-property-inclusion: non_null
canal.conf:
mode: tcp #tcp kafka rocketMQ rabbitMQ
flatMessage: true
zookeeperHosts:
syncBatchSize: 1000
retries: 0
timeout:
accessKey:
secretKey:
consumerProperties:
# canal tcp consumer
canal.tcp.server.host: 172.20.0.30:11111
canal.tcp.zookeeper.hosts: 172.20.0.30:2181
canal.tcp.batch.size: 500
canal.tcp.username:
canal.tcp.password:
canalAdapters:
- instance: example # canal instance Name or mq topic name
groups:
- groupId: g1
outerAdapters:
- name: logger
- name: rdb
key: mysql1
properties:
jdbc.driverClassName: com.mysql.jdbc.Driver
jdbc.url: jdbc:mysql://172.20.0.32:3306/mytest2?useUnicode=true&useSSl=false
jdbc.username: root
jdbc.password: password
# /root/canal-ob/bin/startup.sh
# tail -500f /root/canal-ob/logs/canal/canal.log
# tail -500f /root/canal-ob/logs/example/example.log
6.1. 安装 canal-adapter
从 https://github.com/oceanbase/canal/releases 下载canal的ob版本。
本次下载文件为:
canal.adapter-for-ob-rc2.tar.gz
mkdir canal-ob canal-adapter-ob -p
tar xf canal.adapter-for-ob-rc2.tar.gz -C canal-adapter-ob/
6.2. 配置 canal-adapter 参数
修改后的配置文件如下:
more /root/canal-adapter-ob/conf/application.yml
server:
port: 8081
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
default-property-inclusion: non_null
canal.conf:
mode: tcp #tcp kafka rocketMQ rabbitMQ
flatMessage: true
zookeeperHosts:
syncBatchSize: 1000
retries: 0
timeout:
accessKey:
secretKey:
consumerProperties:
# canal tcp consumer
canal.tcp.server.host: 172.20.0.30:11111
canal.tcp.zookeeper.hosts: 172.20.0.30:2181
canal.tcp.batch.size: 500
canal.tcp.username:
canal.tcp.password:
canalAdapters:
- instance: example # canal instance Name or mq topic name
groups:
- groupId: g1
outerAdapters:
- name: logger
- name: rdb
key: mysql1
properties:
jdbc.driverClassName: com.mysql.jdbc.Driver
jdbc.url: jdbc:mysql://172.20.0.32:3306/mytest2?useUnicode=true&useSSl=false
jdbc.username: root
jdbc.password: password
从 https://github.com/oceanbase/canal/releases 下载canal的ob版本。
本次下载文件为:
canal.adapter-for-ob-rc2.tar.gz
mkdir canal-ob canal-adapter-ob -p
tar xf canal.adapter-for-ob-rc2.tar.gz -C canal-adapter-ob/
修改后的配置文件如下:
more /root/canal-adapter-ob/conf/application.yml
server:
port: 8081
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
default-property-inclusion: non_null
canal.conf:
mode: tcp #tcp kafka rocketMQ rabbitMQ
flatMessage: true
zookeeperHosts:
syncBatchSize: 1000
retries: 0
timeout:
accessKey:
secretKey:
consumerProperties:
# canal tcp consumer
canal.tcp.server.host: 172.20.0.30:11111
canal.tcp.zookeeper.hosts: 172.20.0.30:2181
canal.tcp.batch.size: 500
canal.tcp.username:
canal.tcp.password:
canalAdapters:
- instance: example # canal instance Name or mq topic name
groups:
- groupId: g1
outerAdapters:
- name: logger
- name: rdb
key: mysql1
properties:
jdbc.driverClassName: com.mysql.jdbc.Driver
jdbc.url: jdbc:mysql://172.20.0.32:3306/mytest2?useUnicode=true&useSSl=false
jdbc.username: root
jdbc.password: password
6.3. 配置 rdb 映射文件(以整库同步为例)
修改后的配置文件如下:
more /root/canal-adapter-ob/conf/rdb/mytest_user.yml
## Mirror schema synchronize config
dataSourceKey: defaultDS
destination: example
groupId: g1
outerAdapterKey: mysql1
concurrent: true
dbMapping:
mirrorDb: true
database: mytest2
7. 测试
修改后的配置文件如下:
more /root/canal-adapter-ob/conf/rdb/mytest_user.yml
## Mirror schema synchronize config
dataSourceKey: defaultDS
destination: example
groupId: g1
outerAdapterKey: mysql1
concurrent: true
dbMapping:
mirrorDb: true
database: mytest2
确保 OceanBase 和 MySQL 库里均有 mytest2 库即可进行测试。
8. 感想
配置步骤较多,细节方面要注意,需要多配置几次,熟悉就好了。
OceanBase 社区版入门到实战教程直播正在进行中~
快和小伙伴一起进群交流学习吧~
加入直播群方式一:
钉钉群号 3582 5151
加入直播群方式二:
扫码下方二维码加入