本文主要是介绍[mysql]binlog监听main方法简化版,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、检测是否开启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() {@Overridepublic void onEvent(Event event) {System.out.println(event);}});try {client.connect();} catch (IOException e) {e.printStackTrace();}}
}
这篇关于[mysql]binlog监听main方法简化版的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!