一、官网地址:https://github.com/alibaba/canal
二、安装 canal server
1. 下载canal
直接下载
访问:https://github.com/alibaba/canal/releases
下载:https://github.com/alibaba/canal/releases/download/canal-1.0.24/canal.deployer-1.0.24.tar.gz
2. 解压缩
mkdir canal tar zxvf canal.deployer-$version.tar.gz
解压完成后,进入canal目录,可以看到如下结构:
drwxr-xr-x 2 jianghang jianghang 136 2013-02-05 21:51 bin drwxr-xr-x 4 jianghang jianghang 160 2013-02-05 21:51 conf drwxr-xr-x 2 jianghang jianghang 1.3K 2013-02-05 21:51 lib drwxr-xr-x 2 jianghang jianghang 48 2013-02-05 21:29 logs
3. 配置修改
应用参数:
vi conf/example/instance.properties
################################################# ## mysql serverId canal.instance.mysql.slaveId = 1234
position info,需要改成自己的数据库信息
canal.instance.master.address = 127.0.0.1:3306 canal.instance.master.journal.name = canal.instance.master.position = canal.instance.master.timestamp =
#canal.instance.standby.address = #canal.instance.standby.journal.name = #canal.instance.standby.position = #canal.instance.standby.timestamp =
username/password,需要改成自己的数据库信息
canal.instance.dbUsername = canal canal.instance.dbPassword = canal canal.instance.defaultDatabaseName = canal_test canal.instance.connectionCharset = UTF-8
table regex
canal.instance.filter.regex = .\..
#################################################
说明:
- canal.instance.connectionCharset 代表数据库的编码方式对应到java中的编码类型,比如UTF-8,GBK , ISO-8859-1
4. 准备启动
sh bin/startup.sh
5. 查看日志
vi logs/canal/canal.log
2013-02-05 22:45:27.967 [main] INFO com.alibaba.otter.canal.deployer.CanalLauncher - ## start the canal server. 2013-02-05 22:45:28.113 [main] INFO com.alibaba.otter.canal.deployer.CanalController - ## start the canal server[10.1.29.120:11111] 2013-02-05 22:45:28.210 [main] INFO com.alibaba.otter.canal.deployer.CanalLauncher - ## the canal server is running now ......
具体instance的日志:
vi logs/example/example.log
2013-02-05 22:50:45.636 [main] INFO c.a.o.c.i.spring.support.PropertyPlaceholderConfigurer - Loading properties file from class path resource [canal.properties] 2013-02-05 22:50:45.641 [main] INFO c.a.o.c.i.spring.support.PropertyPlaceholderConfigurer - Loading properties file from class path resource [example/instance.properties] 2013-02-05 22:50:45.803 [main] INFO c.a.otter.canal.instance.spring.CanalInstanceWithSpring - start CannalInstance for 1-example 2013-02-05 22:50:45.810 [main] INFO c.a.otter.canal.instance.spring.CanalInstanceWithSpring - start successful....
6. 关闭
sh bin/stop.sh
三、配置mysql
a. canal的原理是基于mysql binlog技术,所以这里一定需要开启mysql的binlog写入功能,建议配置binlog模式为row.
修改my.cnf配置文件,如果找不到,参考:https://blog.csdn.net/bahaidong/article/details/80475684
[mysqld] log-bin=mysql-bin #添加这一行就ok binlog-format=ROW #选择row模式 server_id=1 #配置mysql replaction需要定义,不能和canal的slaveId重复
b. canal的原理是模拟自己为mysql slave,所以这里一定需要做为mysql slave的相关权限.
用root账户登录mysql添加
CREATE USER canal IDENTIFIED BY 'canal'; GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'canal'@'%'; -- GRANT ALL PRIVILEGES ON *.* TO 'canal'@'%' ; FLUSH PRIVILEGES;
针对已有的账户可直接通过grant
四、测试数据库脚本
create database canal_test;
use canal_test;
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) DEFAULT NULL,
`weight` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
五、下载示例代码
https://github.com/alibaba/canal/releases/download/canal-1.0.24/canal.example-1.0.24.tar.gz
导入idea或eclipse
六、单机模式运行
启动canal,找到类,运行即可,修改表数据即可
SimpleCanalClientTest
七、集群模式
1、安装zookeeper
通过docker安装一台测试用即可
docker用法参考:https://blog.csdn.net/bahaidong/article/details/80467584
启动zookeeper
$ docker run -d -p 80:80 --name zookeeper zookeeper
ֹͣ $ docker container stop zookeeper
删除 $ docker container rm zookeeper
重启 $ docker container start zookeeper
2、运行,修改表数据即可
ClusterCanalClientTest