/** 查询当前的binlog位置 */ private EntryPosition findStartPosition(MysqlConnection mysqlConnection) { try { ResultSetPacket packet = mysqlConnection.query("show binlog events limit 1"); List<String> fields = packet.getFieldValues(); if (CollectionUtils.isEmpty(fields)) { throw new CanalParseException( "command : 'show binlog events limit 1' has an error! pls check. you need (at least one of) the SUPER,REPLICATION CLIENT privilege(s) for this operation"); } EntryPosition endPosition = new EntryPosition(fields.get(0), Long.valueOf(fields.get(1))); return endPosition; } catch (IOException e) { throw new CanalParseException("command : 'show binlog events limit 1' has an error!", e); } }
/** 查询当前的slave视图的binlog位置 */ @SuppressWarnings("unused") private SlaveEntryPosition findSlavePosition(MysqlConnection mysqlConnection) { try { ResultSetPacket packet = mysqlConnection.query("show slave status"); List<FieldPacket> names = packet.getFieldDescriptors(); List<String> fields = packet.getFieldValues(); if (CollectionUtils.isEmpty(fields)) { return null; } int i = 0; Map<String, String> maps = new HashMap<String, String>(names.size(), 1f); for (FieldPacket name : names) { maps.put(name.getName(), fields.get(i)); i++; } String errno = maps.get("Last_Errno"); String slaveIORunning = maps.get("Slave_IO_Running"); // Slave_SQL_Running String slaveSQLRunning = maps.get("Slave_SQL_Running"); // Slave_SQL_Running if ((!"0".equals(errno)) || (!"Yes".equalsIgnoreCase(slaveIORunning)) || (!"Yes".equalsIgnoreCase(slaveSQLRunning))) { logger.warn( "Ignoring failed slave: " + mysqlConnection.getConnector().getAddress() + ", Last_Errno = " + errno + ", Slave_IO_Running = " + slaveIORunning + ", Slave_SQL_Running = " + slaveSQLRunning); return null; } String masterHost = maps.get("Master_Host"); String masterPort = maps.get("Master_Port"); String binlog = maps.get("Master_Log_File"); String position = maps.get("Exec_Master_Log_Pos"); return new SlaveEntryPosition(binlog, Long.valueOf(position), masterHost, masterPort); } catch (IOException e) { logger.error("find slave position error", e); } return null; }