一、检测是否开启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
查看所有二进制日志文件
SHOW BINARY LOGS;
查看当前使用的二进制日志文件
SHOW MASTER STATUS;
刷新二进制日志
FLUSH LOGS;
show variables like 'binlog_format';
查看当前使用的二进制日志文件格式
设置二进制日志的格式为STATEMENT、ROW或MIXED
SET GLOBAL binlog_format = 'ROW';
二、当正常开启
核心依赖
<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();
}
}
}
监听器实际使用
–乐游监听器
跳转链接
注意 weci打包时不会将配置的jar打入需要单独设置
我们最后使用的方案是这样的。
<dependency>
<groupId>com.sansec</groupId>
<artifactId>crypto</artifactId>
<version>5.3.3.17</version>
</dependency>
<dependency>
<groupId>com.sansec</groupId>
<artifactId>swxajce</artifactId>
<version>5.3.3.17</version>
</dependency>