由于同步是基于binlog实现的,所以要先在mysql中开启binlog
修改my.cnf配置信息
server-id = 1
log-bin=mysql-bin # 开启 binlog
binlog-format=ROW # 选择 ROW 模式
重启mysql服务,然后连接到mysql服务,使用指令查看是否已经开启
show variables like '%log_bin%';
源数据库创建一个canal账号,并且设置slave,dump权限
CREATE USER canal IDENTIFIED BY 'canal';
GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'canal'@'%';
-- GRANT ALL PRIVILEGES ON *.* TO 'canal'@'%' ;
FLUSH PRIVILEGES;
因为mysql8.0.3后身份检验方式为caching_sha2_password,但canal使用的是mysql_native_password,因此需要设置检验方式(如果该版本之前的可跳过),否则会报错IOException: caching_sha2_password Auth failed
ALTER USER 'canal'@'%' IDENTIFIED WITH mysql_native_password BY 'canal';
select host,user,plugin from mysql.user ;
为了方便对canal-server以及后期集群的管理,我们先安装canal-admin
docker pull canal/canal-admin
/*
Navicat MySQL Data Transfer
Source Server : local_docker_3307
Source Server Version : 80027
Source Host : 192.168.0.104:3307
Source Database : canal_manager
Target Server Type : MYSQL
Target Server Version : 80027
File Encoding : 65001
Date: 2023-04-05 16:23:13
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for canal_adapter_config
-- ----------------------------
DROP TABLE IF EXISTS `canal_adapter_config`;
CREATE TABLE `canal_adapter_config` (
`id` bigint NOT NULL AUTO_INCREMENT,
`category` varchar(45) NOT NULL,
`name` varchar(45) NOT NULL,
`status` varchar(45) DEFAULT NULL,
`content` text NOT NULL,
`modified_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
-- ----------------------------
-- Records of canal_adapter_config
-- ----------------------------
-- ----------------------------
-- Table structure for canal_cluster
-- ----------------------------
DROP TABLE IF EXISTS `canal_cluster`;
CREATE TABLE `canal_cluster` (
`id` bigint NOT NULL AUTO_INCREMENT,
`name` varchar(63) NOT NULL,
`zk_hosts` varchar(255) NOT NULL,
`modified_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
-- ----------------------------
-- Records of canal_cluster
-- ----------------------------
-- ----------------------------
-- Table structure for canal_config
-- ----------------------------
DROP TABLE IF EXISTS `canal_config`;
CREATE TABLE `canal_config` (
`id` bigint NOT NULL AUTO_INCREMENT,
`cluster_id` bigint DEFAULT NULL,
`server_id` bigint DEFAULT NULL,
`name` varchar(45) NOT NULL,
`status` varchar(45) DEFAULT NULL,
`content` text NOT NULL,
`content_md5` varchar(128) NOT NULL,
`modified_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `sid_UNIQUE` (`server_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
-- ----------------------------
-- Records of canal_config
-- ----------------------------
-- ----------------------------
-- Table structure for canal_instance_config
-- ----------------------------
DROP TABLE IF EXISTS `canal_instance_config`;
CREATE TABLE `canal_instance_config` (
`id` bigint NOT NULL AUTO_INCREMENT,
`cluster_id` bigint DEFAULT NULL,
`server_id` bigint DEFAULT NULL,
`name` varchar(45) NOT NULL,
`status` varchar(45) DEFAULT NULL,
`content` text NOT NULL,
`content_md5` varchar(128) DEFAULT NULL,
`modified_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `name_UNIQUE` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
-- ----------------------------
-- Records of canal_instance_config
-- ----------------------------
-- ----------------------------
-- Table structure for canal_node_server
-- ----------------------------
DROP TABLE IF EXISTS `canal_node_server`;
CREATE TABLE `canal_node_server` (
`id` bigint NOT NULL AUTO_INCREMENT,
`cluster_id` bigint DEFAULT NULL,
`name` varchar(63) NOT NULL,
`ip` varchar(63) NOT NULL,
`admin_port` int DEFAULT NULL,
`tcp_port` int DEFAULT NULL,
`metric_port` int DEFAULT NULL,
`status` varchar(45) DEFAULT NULL,
`modified_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
-- ----------------------------
-- Records of canal_node_server
-- ----------------------------
-- ----------------------------
-- Table structure for canal_user
-- ----------------------------
DROP TABLE IF EXISTS `canal_user`;
CREATE TABLE `canal_user` (
`id` bigint NOT NULL AUTO_INCREMENT,
`username` varchar(31) NOT NULL,
`password` varchar(128) NOT NULL,
`name` varchar(31) NOT NULL,
`roles` varchar(31) NOT NULL,
`introduction` varchar(255) DEFAULT NULL,
`avatar` varchar(255) DEFAULT NULL,
`creation_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb3;
-- ----------------------------
-- Records of canal_user
-- ----------------------------
INSERT INTO `canal_user` VALUES ('1', 'admin', '6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9', 'Canal Manager', 'admin', null, null, '2019-07-14 00:05:28');
docker run -d --name canal-admin -e spring.datasource.address=192.168.0.104:3307 -e spring.datasource.database=canal_manager -e spring.datasource.username=root -e spring.datasource.password=root -p 8089:8089 canal/canal-admin:latest
自己按照自己的环境对数据的地址、端口、用户名、密码进行修改
启动之后,访问:http://localhost:8089,默认账号密码为admin/123456
docker pull canal/canal-server:latest
docker run -d --name canal-server -e canal.instance.master.address=192.168.0.104:3307 -e canal.instance.dbUsername=root -e canal.instance.dbPassword=root -e canal.admin.manager=192.168.0.104:8089 -e canal.admin.port=11110 -e canal.admin.user=admin -e canal.admin.passwd=4ACFE3202A5FF5CF467898FC58AAB1D615029441 -p 11111:11111 canal/canal-server:latest
初次启动后如果我们需要修改instance.properties,建议复制到宿主机进行修改
docker cp canal-server:/home/admin/canal-server/conf/example/instance.properties /home/ninesun/mydata/canal/conf/
PS:/home/ninesun/mydata/canal/conf/是我宿主机的目录,自己选择自己的复制地址
具体需要修改的内容大致如下图所示,自己按照需求修改
instance.properties配置描述
同时我们还需要设置全量同步
查询源mysql服务器的binlog位置
# 源mysql服务器中登陆mysql执行
show binary logs;
然后将你修改好的配置文件复制回容器内
docker cp /home/ninesun/mydata/canal/conf/instance.properties canal-server:/home/admin/canal-server/conf/example/instance.properties
重启镜像即可
docker restart canal
docker exec -it canal bash
cd ./canal-server/logs/example/
cat ./example.log
若出现以下内容则表示配置成功
刷新admin管理页面的列表就可以看到我们的server已经注册进来了
至此docker安装canal就结束了
下载地址:https://github.com/alibaba/canal/releases
同时Canal是基于Java开发的,所以要保证你的电脑上有jdk或jre
创建一个canal_manager数据库,编码格式utf8mb4(如果未安装admin管理服务则不需要该数据库),该数据库用于远程统一配置管理
导入脚本canal_manager.sql,初始化数据库结构数据,该脚本文件在canal.admin下的conf目录中
查询源mysql服务器的binlog位置
# 源mysql服务器中登陆mysql执行
show binary logs;
具体修改内容和我们docker里配置的一样