一、检测是否开启binlog
1.正常开启状态
show variables like 'log_bin';
+
| Variable_name | Value |
+
| log_bin | ON |
+
1.2
show binary logs;
+
| Log_name | File_size |
+
| mysql-bin.000001 | 154 |
+
2.权限不足情况
show binary logs;
1227 - Access denied; you need (at least one of) the SUPER, REPLICATION CLIENT privilege(s) for this operation
3.未开启状态(默认情况下是不开启的)
show binary logs;
ERROR 1381 - You are not using binary logging
二、当正常开启
核心依赖
<dependency>
<groupId>com.github.shyiko</groupId>
<artifactId>mysql-binlog-connector-java</artifactId>
<version>0.17.0</version>
</dependency>
DemoMain
package org.example;
import com.github.shyiko.mysql.binlog.BinaryLogClient;
import com.github.shyiko.mysql.binlog.event.Event;
import java.io.IOException;
public class BinlogListener {
public static void main(String[] args) throws Exception {
BinaryLogClient client = new BinaryLogClient("localhost", 3306, "root", "root");
client.registerEventListener(new BinaryLogClient.EventListener() {
@Override
public void onEvent(Event event) {
System.out.println(event);
}
});
try {
client.connect();
} catch (IOException e) {
e.printStackTrace();
}
}
}