canal实现mysql数据同步
canal只使用于做mysql的数据同步,其他数据库请参考别的技术.
参考博客https://www.cnblogs.com/1124li/p/11713757.html
原理:
步骤
1.mysql开启binlog模式
查询是否开启: show variables like '%log_bin%'
我的mysql8默认已经开启,若没有开启,请参考:https://blog.csdn.net/harris135/article/details/79712750
其中:
log_bin 为ON表示开启,为OFF表示未开启; log_bin_basename 表示二进制的文件保存路径
docker安装的mysql8,步骤和问题:
1.mysql开启logbin
进图mysql容器,找到 /etc/mysql/my.cnf 文件,完整内容如下
添加上述红色框中的内容即可。
mysql创建canal用户并授权:(canal默认用户和密码都是canal)
CREATE user canal@'%' IDENTIFIED BY 'canal';
GRANT SELECT,REPLICATION SLAVE,REPLICATION CLIENT,SUPER ON *.* TO 'canal'@'%';
FLUSH PRIVILEGES;
接下来就是安装canal了
linux安装canal
- 拉取镜像
docker pull docker.io/canal/canal-server
2.运行容器
docker run -p 11111:11111 --name canal -d docker.io/canal/canal-server
3.进入容器,修改核心配置canal.properties和instance.properties
前者是canal自身的配置,后者是需要同步数据的数据库连接配置
docker exec -it canal /bin/bash
cd canal-server/conf
vi canal.properties
cd example/
vi instance.properties
canal.properties:
instance.properties
reg:
.*表示所有数据库, .\\..*:表示所有表 , .*\\..*表示所有数据库中的所有表
mq config:
表示外部:自己定义微服务的监听,需要指定example名字
**
常见问题:
**
进入canal容器查看日志:发现有报错
docker exec -it canal-server bash
tail -100f canal-server/logs/example/example.log
canal日志报错:java.io.IOException: caching_sha2_password Auth failed
解决办法:
-- 解决mysql8的caching_sha2_password auth失败问题
ALTER USER 'canal'@'%' IDENTIFIED BY 'canal' PASSWORD EXPIRE NEVER;
ALTER USER 'canal'@'%' IDENTIFIED WITH mysql_native_password BY 'canal';
FLUSH PRIVILEGES;
解释:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER; #修改加密规则
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; #更新一下用户密码
FLUSH PRIVILEGES; #刷新权限
搭建完成
微服务搭建测试
pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>topgame-service</artifactId>
<groupId>com.topgame</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<description>canal无服务,实现数据库数据同步监控</description>
<artifactId>topgame-service-canal</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--需要安装-->
<dependency>
<groupId>com.xpand</groupId>
<artifactId>starter-canal</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
yml
server:
port: 18083
spring:
application:
name: topgame-service-canal
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:7001/eureka/
instance:
prefer-ip-address: true
feign:
hystrix:
enabled: true
#hystrix配置:信号量隔离
#hystrix:
# command:
# default:
# execution:
# timeout:
# enabled: true
# isolation:
# strategy: SEMAPHORE
#canal配置:连接访问阿里服务器的canal
canal:
client:
instances:
# 实例名称
example:
host: 101.200.240.33
port: 11111
监听类:CanalDataEventListener
package com.topgame.canal;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.InsertListenPoint;
import java.util.List;
/**
* @author liang
* @version 1.0
* @date 2020/4/24 19:40
* 实现mysql数据监听
*/
//启动报错,但能运行
//Caused by: java.io.IOException: 你的主机中的软件中止了一个已建立的连接。
@CanalEventListener
public class CanalDataEventListener {
/**
* 监听数据:增加
* @param entryType : 当前操作的类型
* @param rowData : 发生变更的行
* 增加前没数据,增加后有数据
* rowData.getAfterColumnsList();增加后
* rowData.getBeforeColumnsList();删除前
*/
//Caused by: java.io.IOException: 你的主机中的软件中止了一个已建立的连接。运行时异常,一直报这个错
@InsertListenPoint
public void onEventInsert(CanalEntry.EntryType entryType,CanalEntry.RowData rowData){
System.out.println("插入数据");
//插入的数据
List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
for (CanalEntry.Column column: afterColumnsList){
System.out.println(column.getName()+":"+column.getValue());
}
}
/**
* 监听数据:修改
*/
/**
* 监听数据:删除
*/
}
启动类:
package com.topgame;
import com.xpand.starter.canal.annotation.EnableCanalClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @author liang
* @version 1.0
* @date 2020/4/24 17:31
*/
//注意启动是忽略连接池,否则没有就报错
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableEurekaClient
@EnableCanalClient
public class CanalMain18083 {
public static void main(String[] args) {
SpringApplication.run(CanalMain18083.class,args) ;
}
}
测试:随便在任意表中插入一条数据
总结canal:
1.开启mysql的binlog模式
2.下载启动canal容器
3.修改canal两个配置文件(重启有问题需要进入容器,查看具体日志了解到具体问题)
4.canal微服务搭建实现数据库监听,如上