mysql-binlog-connector-java无法监听到日志的解决办法 (Trying to restore lost connection to localhost:3306)

本地代码

public class BinlogServiceTest {

    public static void  main(String[] args) throws IOException {
        BinaryLogClient client = new BinaryLogClient(
                "localhost",
                3306,
                "root",
                "123456"
        );
        client.registerEventListener(event -> {
            EventData data = event.getData();
            if (data instanceof UpdateRowsEventData){
                System.out.println("Update---------------");
                System.out.println(data.toString());
            }else if (data instanceof WriteRowsEventData){
                System.out.println("Write---------------");
                System.out.println(data.toString());
            }else if (data instanceof DeleteRowsEventData){
                System.out.println("Delete---------------");
                System.out.println(data.toString());
            }
          }
        );
        client.connect();
    }
}

运行之后,无报错,但是修改数据库也不能被监听到。
控制台显示
mysql-binlog-connector-java无法监听到日志的解决办法 (Trying to restore lost connection to localhost:3306)_第1张图片
Trying to restore lost connection to localhost:3306
可以看到这里尝试重连了mysql

后续发现在listenForEventPackets方法里,有如下异常

mysql-binlog-connector-java无法监听到日志的解决办法 (Trying to restore lost connection to localhost:3306)_第2张图片
server_id was not set 可以得出错误是因为server_id并没有设置。(坑啊,这里居然没把异常打印出来)

解决:
去到配置文件,添加:
server-id =1
重启mysql
关于修改 配置文件及windows下如何重启mysql可移步https://blog.csdn.net/qq_27037443/article/details/96099628

此时修改mysql数据库,监听成功。
mysql-binlog-connector-java无法监听到日志的解决办法 (Trying to restore lost connection to localhost:3306)_第3张图片

你可能感兴趣的:(java,mysql)