canal github地址
https://github.com/alibaba/canal/releases
执行这个sql
show MASTER STATUS;
修改关键配置
canal.instance.master.address=127.0.0.1:3306 #mysql主机和端口
canal.instance.master.journal.name=mysql-bin.000002 #将上图的file填到这里
canal.instance.master.position=2305 #将上图的position填到这里
canal.instance.dbUsername=root #mysql用户名
canal.instance.dbPassword=123456 #mysql密码
canal.instance.enableDruid=false #密码是否加密 如果是true需要填写下面的公钥
# canal.instance.pwdPublicKey=publicKey #密码加密用的公钥
密码加密:命令行切换到druid.jar下 执行该命令,如果密码中有特殊字符需要使用英文双引号包起来
java -cp druid-1.2.13.jar com.alibaba.druid.filter.config.ConfigTools WcBnMiVd"^"MvQTR%1
执行后会生成私钥、公钥、密码
创建用户并授权,一定要授权!!!!!一定要授权!!!!!一定要授权!!!!!一定要授权!!!!!
CREATE USER 'canal'@'%' IDENTIFIED BY 'canal';
GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'canal'@'%';
FLUSH PRIVILEGES;
开启mysql binlog参考这个网址
https://blog.csdn.net/hundan_520520/article/details/123792968?spm=1001.2014.3001.5506
启动canal
windows:bin目录下双击startup.bat
linux:bin目录下执行startup.sh
maven依赖,版本要和你下载的服务端版本一致
<dependency>
<groupId>com.alibaba.otter</groupId>
<artifactId>canal.client</artifactId>
<version>1.1.4</version>
</dependency>
上代码
public void canal() {
// 创建canal连接对象
CanalConnector connector = CanalConnectors.newSingleConnector(new InetSocketAddress("canal服务主机IP地址", "cannal服务端口", "example", "canal", "canal");
try {
//连接
connector.connect();
//订阅所有数据库和表 正则表达式 库名.表名
connector.subscribe("rzt.gov_ref_sys|rzt.gov_user|rzt.gov_info_user|rzt.gov_info");
connector.rollback();
while (true) {
// 获取指定数量的数据
Message message = connector.getWithoutAck(1000);
long batchId = message.getId();
int size = message.getEntries().size();
if (batchId == -1 || size == 0) {
//没有数据,就休眠1秒
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
} else {
//有数据就打印
printEntry(message.getEntries());
}
// 提交确认
connector.ack(batchId);
}
} finally {
connector.disconnect();
}
}
private static void printEntry(List<Entry> entrys) {
for (Entry entry : entrys) {
if (entry.getEntryType() == EntryType.TRANSACTIONBEGIN ||
entry.getEntryType() == EntryType.TRANSACTIONEND) {
continue;
}
RowChange rowChage = null;
try {
rowChage = RowChange.parseFrom(entry.getStoreValue());
} catch (Exception e) {
throw new RuntimeException("ERROR parse data:" + entry.toString(), e);
}
EventType eventType = rowChage.getEventType();
System.out.println(String.format("================> binlog[%s:%s] , name[%s,%s] , eventType : %s",
entry.getHeader().getLogfileName(), entry.getHeader().getLogfileOffset(),
entry.getHeader().getSchemaName(), entry.getHeader().getTableName(),
eventType));
for (RowData rowData : rowChage.getRowDatasList()) {
//判断增删改操作
if (eventType == EventType.DELETE) {
printColumn(rowData.getBeforeColumnsList());
} else if (eventType == EventType.INSERT) {
printColumn(rowData.getAfterColumnsList());
} else {
System.out.println("-------> before");
printColumn(rowData.getBeforeColumnsList());
System.out.println("-------> after");
printColumn(rowData.getAfterColumnsList());
}
}
}
}
private static void printColumn(List<Column> columns) {
for (Column column : columns) {
System.out.println(column.getName() + " : " + column.getValue() + " update=" + column.getUpdated());
}
}