Java监听mysql的binlog 报错解决办法

报错:com.github.shyiko.mysql.binlog.network.AuthenticationException: Client does not support authentication protocol requested by server; consider upgrading MySQL client

解决方案:在mysql中执行以下命令

alter user 'root'@'localhost' identified with mysql_native_password by '密码';  修改认证规则

刷新权限

flush privileges;  

报错:The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone.

my.ini配置文件增加配置

[mysqld]

default-time-zone='+08:00'

查看区时 如下图:

show variables like'%time_zone';  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用 Java 代码监听 MySQL binlog,可以使用 MySQL Connector/J 驱动程序提供的 API。以下是一个简单的示例代码: ```java import java.io.IOException; import java.io.Serializable; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.mysql.cj.jdbc.MysqlDataSource; import com.mysql.cj.jdbc.exceptions.CommunicationsException; import com.mysql.cj.jdbc.exceptions.MySQLTimeoutException; import com.mysql.cj.jdbc.exceptions.PacketTooBigException; import com.mysql.cj.jdbc.exceptions.WrongArgumentException; import com.mysql.cj.jdbc.exceptions.WrongUsageException; import com.mysql.cj.protocol.ResultsetRow; import com.mysql.cj.protocol.a.BinaryRowDecoder; import com.mysql.cj.protocol.a.MysqlBinaryLogClient; import com.mysql.cj.result.Row; import com.mysql.cj.result.RowList; public class MySQLBinlogListener { private final String host; private final int port; private final String username; private final String password; private final String schemaName; private final long serverId; private final long binlogPosition; private final String binlogFilename; public MySQLBinlogListener(String host, int port, String username, String password, String schemaName, long serverId, long binlogPosition, String binlogFilename) { this.host = host; this.port = port; this.username = username; this.password = password; this.schemaName = schemaName; this.serverId = serverId; this.binlogPosition = binlogPosition; this.binlogFilename = binlogFilename; } public void start() throws SQLException, IOException { MysqlDataSource dataSource = new MysqlDataSource(); dataSource.setServerName(host); dataSource.setPort(port); dataSource.setUser(username); dataSource.setPassword(password); dataSource.setDatabaseName(schemaName); Connection connection = DriverManager.getConnection(dataSource.getUrl(), dataSource.getUser(), dataSource.getPassword()); connection.setAutoCommit(false); MysqlBinaryLogClient client = new MysqlBinaryLogClient(host, port, username, password); client.setServerId(serverId); client.setBinlogFilename(binlogFilename); client.setBinlogPosition(binlogPosition); client.registerEventListener(event -> { if (event.getData() instanceof RowList) { RowList rows = (RowList) event.getData(); List<Serializable[]> rowDataList = new ArrayList<>(); for (Row row : rows) { BinaryRowDecoder decoder = new BinaryRowDecoder(rows.getColumnTypes(), row); Serializable[] rowData = decoder.decode(); rowDataList.add(rowData); } // 处理 rowDataList 中的数据 System.out.println("Received " + rowDataList.size() + " rows"); } else if (event.getData() instanceof ResultsetRow) { ResultsetRow row = (ResultsetRow) event.getData(); BinaryRowDecoder decoder = new BinaryRowDecoder(event.getColumnTypes(), row); Serializable[] rowData = decoder.decode(); // 处理 rowData 中的数据 System.out.println("Received 1 row"); } else { // 处理其他类型的事件(例如DDL语句) System.out.println("Received other event"); } }); while (true) { try { client.connect(); } catch (WrongArgumentException | WrongUsageException | PacketTooBigException e) { // 处理异常 } catch (MySQLTimeoutException | CommunicationsException e) { // 处理异常 } Thread.sleep(1000); } } } ``` 在这个示例代码中,我们使用 `MysqlBinaryLogClient` 类来连接 MySQL 服务器,并使用 `registerEventListener` 方法注册一个事件监听器。当有新的 binlog 事件产生时,事件监听器会被触发,我们可以在事件监听器中处理事件的数据。 要启动监听程序,只需要创建一个 `MySQLBinlogListener` 对象,并调用 `start` 方法即可。在 `start` 方法中,我们创建了一个 `MysqlDataSource` 对象来连接 MySQL 数据库,并创建了一个 `MysqlBinaryLogClient` 对象来连接 binlog 服务。然后,我们使用一个无限循环来不断连接 binlog 服务,如果连接失败,则等待一段时间再进行重连。在事件监听器中,我们可以处理不同类型的事件,例如插入、更新、删除语句等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值