MySQL配置:
- 开启binlog日志
SHOW VARIABLES LIKE '%log_bin%'
- 将binlog监听模式改为ROW
- 创建数据查询权限的账号,建议账号密码都为canal(canal-deployer中默认账号密码都是canal)
CREATE USER canal IDENTIFIED BY 'canal'; GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'canal'@'%'; FLUSH PRIVILEGES;
下载canal
下载deployer和adapter即可
下载地址:https://github.com/alibaba/canal/releases
- deploye(监听binlog日志变化):
下载之后直接启动即可,conf中的canal.properties默认即可,进入example中instance.properties根据自己的数据库地址自行更改 - adapter(处理变更数据):
以同步es为例,进入adapter的conf目录中,修改application.yml文件,其他配置默认
然后在es中创建索库,创建对应文档,字段类型
以adapter的conf目录中es7为例
MySQL创建表:CREATE TABLE `test_demo` ( `id` bigint(20) NOT NULL COMMENT '主键', `dept_id` bigint(20) DEFAULT NULL COMMENT '部门id', `user_id` bigint(20) DEFAULT NULL COMMENT '用户id', `order_num` int(11) DEFAULT '0' COMMENT '排序号', `test_key` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'key键', `value` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '值', `version` int(11) DEFAULT '0' COMMENT '版本', `create_time` datetime DEFAULT NULL COMMENT '创建时间', `create_by` varchar(64) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '创建人', `update_time` datetime DEFAULT NULL COMMENT '更新时间', `update_by` varchar(64) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '更新人', `del_flag` int(11) DEFAULT '0' COMMENT '删除标志', PRIMARY KEY (`id`) USING BTREE ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ROW_FORMAT=DYNAMIC COMMENT='测试单表'; INSERT INTO `admin`.`test_demo` (`id`, `dept_id`, `user_id`, `order_num`, `test_key`, `value`, `version`, `create_time`, `create_by`, `update_time`, `update_by`, `del_flag`) VALUES (1, 102, 4, 1, '测试数据权限12112', '测试111', 0, '2024-02-25 15:39:12', 'admin', '2024-05-19 18:41:47', 'admin', 0);
es建立索引库,然后新建文档,插入一条数据:
http://localhost:9200/demo/test_demo/1
body:
{
"id": 1,
"deptId": 102,
"userId": 4,
"order_num": 1,
"test_key": "测试数据权限",
"value": "测试",
"version": "0",
"delFlag": "0"
}
-
在es7中创建demo.yml配置文件,建好配置文件即可在bin目录中启动startup.bat文件。启动成功之后可以直接在数据库中修改字段值,观察日志,此时es中的数据会进行同步。
index:即为你创建的索引库dataSourceKey: defaultDS destination: example groupId: g1 esMapping: _index: demo _id: id sql: "select t.id, t.dept_id, t.user_id, t.order_num, t.test_key, t.value, t.version from test_demo t" etlCondition: "where t.c_time>={}" commitBatch: 3000