译意: 水獭,数据搬运工
语言: 纯java开发
定位: 基于数据库增量日志解析,准实时同步到本机房或异地机房的mysql/oracle数据库.
工作原理
一、环境准备
操作系统配置:
关闭防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
关闭SElinux
sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config
setenforce 0
二、安装jdk(manager、node都安装)
rpm -e `rpm -qa|grep openjdk` --nodeps //卸载openjdk
上传jdk-8u202-linux-x64.tar.gz文件到/usr/local目录,并运行如下命令进行解压
tar xzvf jdk-8u202-linux-x64.tar.gz
在/etc/profile文件最后面,添加jdk环境变量
export JAVA_HOME=/usr/local/jdk1.8.0_202
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
保存并退出编辑
source /etc/profile //使环境变量生效
java -version //确定jdk版本
[root@node102 node]# java -version
java version "1.8.0_202"
Java(TM) SE Runtime Environment (build 1.8.0_202-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.202-b08, mixed mode)
三、部署zookeeper
wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz
上传文件zookeeper-3.4.14.tar.gz 到/usr/local/目录下,并进行解压
tar xzvf zookeeper-3.4.14.tar.gz
创建对应的数据目录
mkdir -p /usr/local/zookeeper-3.4.14
mkdir -p /data/zookeeper
编辑zookeeper配置文件
cd /usr/local/zookeeper-3.4.14/conf
cp zoo_sample.cfg zoo.cfg
vim zoo.cfg 编辑文件,修改成如下的参数
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/data/zookeeper #只需要修改这个目录,其它的默认不变
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
保存并退出编辑
单节点的zk不用修改配置,可以直接启动。
当你看到Starting zookeeper ... STARTED,表示你的zookeeper服务已经启动成功了。我们可以用 jps 命令查询zookeeper启动是否正常(也可以用 ps -ef|grep zookeeper 命令查看),QuorumPeerMain这个进程就是zookeeper的进程。至此,zookeeper的单节点安装就成功了。
启动Zookeeper:
[root@node102 bin]# pwd
/usr/local/zookeeper-3.4.14/bin
[root@node102 bin]# /usr/local/zookeeper-3.4.14/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.14/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
查看监听的端口:
[root@node102 bin]# netstat -lntup | grep java
tcp6 0 0 :::42296 :::* LISTEN 8496/java
tcp6 0 0 :::2181 :::* LISTEN 8496/java
[root@node102 bin]# lsof -i:2181
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 8496 root 28u IPv6 46469 0t0 TCP *:eforward (LISTEN)
检查服务状态:
[root@node102 bin]# /usr/local/zookeeper-3.4.14/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.14/bin/../conf/zoo.cfg
Mode: standalone
四、安装并配置otter数据库
1.安装MySQL(略)
配置文件如下:
cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
# disable_ssl
skip_ssl
character_set_server = utf8mb4
log-bin=mysql-bin
binlog-format=ROW
server_id=102
gtid_mode=ON
enforce_gtid_consistency=ON
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[mysql]
user=root
password=123456
default-character-set=utf8mb4
2.执行otter数据库配置脚本(创建otter数据库和表)
wget https://github.com/alibaba/otter/blob/master/manager/deployer/src/main/resources/sql/otter-manager-schema.sql
mysql -uroot -p
#输入mysql密码
source /root/otter-manager-schema.sql
五、部署otter manager
1.安装软件
上传manager.deployer-4.2.18.tar.gz 文件到/usr/local/目录
mkdir /usr/local/otter-manager
tar -xzvf manager.deployer-4.2.18.tar.gz -C /usr/local/otter-manager
2.修改配置文件
修改数据库连接及zookeeper相关参数
vim /usr/local/otter-manager/conf/otter.properties
## otter manager database config #修改为正确数据库信息
otter.database.driver.class.name = com.mysql.jdbc.Driver
otter.database.driver.url = jdbc:mysql://172.16.1.102:3306/otter?useSSL=false #禁用SSL检查
otter.database.driver.username = otter
otter.database.driver.password = Otter_123456
## otter communication port
otter.communication.manager.port = 1099
## otter communication payload size (default = 8388608)
otter.communication.payload = 8388608
## otter communication pool size
otter.communication.pool.size = 10
## default zookeeper address
otter.zookeeper.cluster.default = 127.0.0.1:2181 #修改为正确的地址,手动选择一个地域就近的zookeeper集群列表,zookeeper默认端口 2181
## default zookeeper sesstion timeout = 60s
otter.zookeeper.sessionTimeout = 60000
## otter arbitrate connect manager config
otter.manager.address = ${otter.domainName}:${otter.communication.manager.port}
## should run in product mode , true/false
otter.manager.productionMode = true
## self-monitor enable or disable
otter.manager.monitor.self.enable = true
## self-montir interval , default 120s
otter.manager.monitor.self.interval = 120
## auto-recovery paused enable or disable
otter.manager.monitor.recovery.paused = true
# manager email user config
otter.manager.monitor.email.host = smtp.gmail.com
otter.manager.monitor.email.username =
otter.manager.monitor.email.password =
otter.manager.monitor.email.stmp.port = 465
3.启动otter manager
/usr/local/otter-manager/bin/startup.sh
查看监听的端口(8080):
[root@node102 bin]# netstat -lntup | grep java
tcp 0 0 0.0.0.0:1099 0.0.0.0:* LISTEN 8412/java
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 8412/java
tcp6 0 0 :::40183 :::* LISTEN 8306/java
tcp6 0 0 :::2181 :::* LISTEN 8306/java
4.访问页面
http://172.16.1.102:8080/channelList.htm
出现otter的页面,用户名和密码都是admin。
六、部署otter node
1.otter manager中配置otter node
http://172.16.1.102:8080/channelList.htm
默认管理员账户/密码为: admin/admin
添加zookeeper
如果有zookeeper集群,按下面的格式填写,每个用;号分隔
添加node
2.安装otter-node软件
上传node.deployer-4.2.18.tar.gz文件到node节点机的/usr/local/目录,并进行解压
mkdir /usr/local/otter-node
tar -xzvf node.deployer-4.2.18.tar.gz -C /usr/local/otter-node
安装配置aria2
我们在介绍otter的特性里面有提及到aria2,它是一个文件通道来保证需要同步的数据通过极快的速度同步到需要同步的服务器上
cd /usr/local/
wget --no-check-certificate http://sourceforge.net/projects/aria2/files/stable/aria2-1.18.10/aria2-1.18.10.tar.gz
tar zxvf aria2-1.18.1.tar.gz
mv aria2-1.18.1 aria2
cd aria2
./configure
make
make install
#验证安装是否成功
aria2c -v
#会输出
aria2 version 1.18.1
Copyright © 2006, 2013 Tatsuhiro Tsujikawa
2.2 安装配置node
接下来我们来配置node,node主要负责接受manage下发任务的处理
mkdir /usr/local/otter-node
tar xf node.deployer-4.2.18.tar.gz
cd /usr/local/otter-node
#nid配置node的ID多个node协同工作时不能重复
echo 1 > /usr/local/otter-node/conf/nid
#修改配置文件
vim /usr/local/otter-node/conf/otter.properties
# otter node root dir
otter.nodeHome = ${user.dir}/../
## otter node dir
otter.htdocs.dir = ${otter.nodeHome}/htdocs
otter.download.dir = ${otter.nodeHome}/download
otter.extend.dir= ${otter.nodeHome}/extend
## default zookeeper sesstion timeout = 60s
otter.zookeeper.sessionTimeout = 60000
## otter communication payload size (default = 8388608)
otter.communication.payload = 8388608
## otter communication pool size
otter.communication.pool.size = 10
## otter arbitrate & node connect manager config
otter.manager.address = 127.0.0.1:1099 #主要是确认连接manager地址是否正确(这里使用服务器内网地址进行配置)
配置完成之后我们不要着急启动node,因为启动了node要是manager没有配置是没法建立连接的,顺序是先配置好manager再开启node.
4、启动otter-node
[root@node102 bin]# /usr/local/otter-node/bin/startup.sh
查看监听的端口号(2088):
[root@node102 bin]# netstat -lntup | grep java
tcp 0 0 0.0.0.0:9090 0.0.0.0:* LISTEN 8643/java
tcp 0 0 0.0.0.0:2088 0.0.0.0:* LISTEN 8643/java
tcp 0 0 0.0.0.0:2090 0.0.0.0:* LISTEN 8643/java
tcp 0 0 0.0.0.0:1099 0.0.0.0:* LISTEN 8412/java
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 8412/java
tcp6 0 0 :::40183 :::* LISTEN 8306/java
tcp6 0 0 :::2181 :::* LISTEN 8306/java
5.登录网页检查:
七、数据库参数配置,建立用户,建立测试表
1.参数配置(主备库添加)
主库:(172.16.1.100)
要求开启二进制日志记录:
[mysqld]
character_set_server = utf8mb4
log-bin=mysql-bin #添加这一行就ok
binlog-format=ROW #选择row模式
server_id=100 #配置mysql replaction需要定义,不能和canal的slaveId重复
gtid_mode=ON
enforce_gtid_consistency=ON
备库:(172.16.1.101)
[mysqld]
character_set_server = utf8mb4
log-bin=mysql-bin #添加这一行就ok
binlog-format=ROW #选择row模式
server_id=101 #配置mysql replaction需要定义,不能和canal的slaveId重复
gtid_mode=ON
enforce_gtid_consistency=ON
2.建立用户
源端数据库:
CREATE USER canal@'%' IDENTIFIED BY 'Canal123.';
GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'canal'@'%';
flush privileges;
目标端数据库:
CREATE USER canal@'%' IDENTIFIED BY 'Canal123.';
grant all on *.* to 'canal'@'%';
flush privileges;
3.建立测试表(主备都需要创建表结构)
CREATE DATABASE test; //创建test数据库
CREATE TABLE `test`.`example` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(32) COLLATE utf8_bin DEFAULT NULL ,PRIMARY KEY (`ID`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
八、添加数据源
配置源端数据库,注意,数据库连接要加useSSL=false
jdbc:mysql://172.16.1.101:3306?useSSL=false
jdbc:mysql://172.16.1.100:3306?useSSL=false
九、添加canal
定义源端数据库
十、添加数据表
添加源端数据库表
添加目标端数据库表
十一、添加channel
十二、添加Pipeline
十四、开始同步
在172.16.1.100 数据库上进行DDL,DML操作
到172.16.1.101数据库上查看数据是否同步