[mysql]binlog监听

一、检测是否开启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();
        }
    }
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值