#include "stdlib.h" #include "stdio.h" #include "string.h" #include "mysql.h" #include "mariadb_rpl.h" int binlog_reader(char* pszServer, char* pszBinlogFile, char* pszUser, char* pszPassword) { MYSQL* hMySql = NULL; MYSQL* hResult; int rc = 0; MARIADB_RPL* hMariadbRpl; // MariaDB binlog_api handle MARIADB_RPL_EVENT RplEvent = { '\0' }; // MariaDB binlog_api last event MARIADB_RPL_EVENT* pRplEvent = &RplEvent; hMySql = mmysql_init(NULL); hResult = mysql_real_connect(hMySql, pszServer, pszUser, pszPassword, "", 3306, 0, 0); if (hResult == NULL) { printf("Connect error\n"); return 1; } mmysql_query(hMySql, "SET @master_binlog_checksum=@@global.binlog_checksum"); hMariadbRpl = mariadb_rpl_init(hMySql); mariadb_rpl_optionsv(hMariadbRpl, MARIADB_RPL_FILENAME, pszBinlogFile, strlen(pszBinlogFile)+1); mariadb_rpl_optionsv(hMariadbRpl, MARIADB_RPL_START, 4 ); mariadb_rpl_optionsv(hMariadbRpl, MARIADB_RPL_SERVER_ID, (uint32_t)1); mariadb_rpl_optionsv(hMariadbRpl, MARIADB_RPL_FLAGS, (uint32_t)0); rc = mariadb_rpl_open(hMariadbRpl); if (rc != 0) { printf("mariadb_rpl_open failed: %s\n", hMySql->net.last_error); return 1; } pRplEvent = mariadb_rpl_fetch(hMariadbRpl, &RplEvent); while (pRplEvent != NULL) { printf("Event %d len %d, next_pos %d\n", RplEvent.event_type, RplEvent.event_length, RplEvent.next_event_pos); pRplEvent = mariadb_rpl_fetch(hMariadbRpl, &RplEvent); } mmysql_close(hMySql); return 0; } // Usage: // > mysql_binlog_reader.exe server binlog-file user password int main(int argc, char* argv[]) { int rc; rc = binlog_reader(argv[1], argv[2], argv[3], argv[4], ); return rc; }