binlog监听事件 没有日志打印,也不报错 ,求教怎么调试源码定位错误
来源:9-3 【先来个栗子】使用开源工具监听 Binlog 的演示

moc_hq
2019-03-26
老师 您好!如题
控制台打印
Connected to the target VM, address: '127.0.0.1:54687', transport: 'socket'
三月 25, 2019 11:46:32 下午 com.github.shyiko.mysql.binlog.BinaryLogClient connect
信息: Connected to 127.0.0.1:3306 at mysql-bin.000027/1020 (sid:65535, cid:71)
以下是代码:
package com.imooc.ad.service;
import com.github.shyiko.mysql.binlog.BinaryLogClient;
import com.github.shyiko.mysql.binlog.event.*;
import lombok.extern.slf4j.Slf4j;
/**
* @Author Alex
* @Desc
* <p>
*
* 测试监听binlog的开源组件
*
* </p>
* @Date 2019/3/25 22:22
*/
@Slf4j
public class BinlogServiceTest {
public static void main(String[] args) throws Exception{
BinaryLogClient client = new BinaryLogClient(
"127.0.0.1",
3306,
"root",
"123456"
);
client.registerEventListener(event->{
EventHeader header = event.getHeader();
log.info("header:{}",header);
EventData data = event.getData();
if (data instanceof UpdateRowsEventData) {
log.info("update info:{}", data);
} else if (data instanceof WriteRowsEventData) {
log.info("insert info:{}", data);
} else if (data instanceof DeleteRowsEventData) {
log.info("delete info:{}", data);
}
});
client.connect();
}
}
然后我本地win10安装的MySQL Server 5.5
mysql 客户端控制台显示:
| mysql-bin.000027 | 1020 |
当我监听mysql时候,不能同步信息,操作如下:
mysql> update ad_unit_keyword set keyword=‘张qw’ where id=1;
Query OK, 1 row affected
Rows matched: 1 Changed: 1 Warnings: 0
控制台,没有变化;还是这样
三月 25, 2019 11:55:32 下午 com.github.shyiko.mysql.binlog.BinaryLogClient$5 run
信息: Trying to restore lost connection to 127.0.0.1:3306
三月 25, 2019 11:55:32 下午 com.github.shyiko.mysql.binlog.BinaryLogClient connect
信息: Connected to 127.0.0.1:3306 at mysql-bin.000027/1020 (sid:65535, cid:80)
mysql 客户端控制台显示,说明日志有增加
mysql> show master logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000027 | 1250 |
+------------------+-----------+
27 rows in set
网上查找,看见同类型错误,见这篇文章:
监听binlog日志不报错
内容有点杂,先是出现binlog不能监听问题,后来网上找资料,发现有人调试代码,然后我发现我不会debug,也找不到问题
谢谢老师~
1回答
-
张勤一
2019-03-26
同学你好:
从你这里给出的控制台信息可以看到,connector 并没有成功连接到 MySQL:
信息: Trying to restore lost connection to 127.0.0.1:3306
这里最可能的原因是你本地 MySQL 的配置,例如:MySQL Binlog 的功能是否开启,用户名密码等等配置。我建议可以尝试直接使用 MySQL 8.0 以上的版本,8.0 以上的 MySQL 是默认开启 Binlog 功能的,不需要再做额外的配置。
这里不需要去调试工具代码,首先,工具代码并没有问题,只是连接 MySQL 出错,所以,一定是在 MySQL 的配置上有问题。
欢迎来 QQ 群随时交流、讨论,也非常感谢同学的支持!
122019-03-27
相似问题