int main(int argc, char** argv) { const char *base_name, *ptr; if (argc != 2) { ptr= strrchr(argv[0], '/'); base_name= (ptr == NULL? argv[0]: ptr + 1); std::cerr << "Usage: " << base_name << " <uri>" << std::endl; return 1; } Binary_log_driver *drv= create_transport(argv[1]); if (drv == NULL) return 1; Binary_log binlog(drv); if (binlog.connect() != ERR_OK) { delete drv; return 1; } Binary_log_event *event; Decoder decode; std::pair<unsigned char *, size_t> buf_and_len; while (true) { int error_number; error_number= drv->get_next_event(&buf_and_len); if (error_number == ERR_OK) { const char *error= NULL; if (!(event= decode.decode_event((char*)buf_and_len.first, buf_and_len.second, &error, 0))) { std::cerr << error << std::endl; break; } if (argv[1][0]== 'f') if ((error_number= static_cast<Binlog_file_driver*>(drv)->update_pos(event)) != ERR_OK) { const char* msg= str_error(error_number); std::cerr << msg << std::endl; break; } } else { const char* msg= str_error(error_number); std::cerr << msg << std::endl; break; } std::cout << "Found event of type " << event->get_event_type() << std::endl; delete event; } delete drv; return 0; }
int MyBinlog::get_next_event(MyEvent *my_event) { Binary_log_event *event; int result; std::pair<unsigned char *, size_t> buffer_buflen; result = m_drv->get_next_event(&buffer_buflen); if (result == ERR_OK) { if (!(event = m_decode.decode_event((char *) buffer_buflen.first, buffer_buflen.second, NULL, 1))) { return ERR_FAIL; } } else { return ERR_FAIL; } my_event->set_type(event->get_event_type()); my_event->set_position(event->header()->log_pos); if (event->header()->type_code == binary_log::TABLE_MAP_EVENT) { m_handler.handle_event(&event); return ERR_OK; } m_handler.handle_event(&event); if(my_event->is_rows_event()) { for(Row_map::iterator row_it = contentHandler.rows_val.begin(); row_it != contentHandler.rows_val.end(); ++ row_it) { for(Row_Fields_map::iterator filed_it = row_it->begin(); filed_it != row_it->end(); ++ filed_it) { std::cout << filed_it->second << "\t"; } std::cout << std::endl; } } if (event->header()->type_code == binary_log::QUERY_EVENT) { binary_log::Query_event *qev = dynamic_cast<binary_log::Query_event *>(event); std::cout << "Query = " << qev->query << " DB = " << qev->db << std::endl; } delete event; return ERR_OK; }
int main(int argc, char **argv) { std::string connectionStr("mysql://*****:*****@localhost:13000"); if (argc > 1) { if (argv[1] == strstr(argv[1], "mysql://")) { connectionStr.assign(argv[1]); } } std::cout << "Trying to connect to <" << connectionStr << ">\n"; std::unique_ptr<system::Binary_log_driver> drv(mysql::system::create_transport(connectionStr.c_str())); mysql::Binary_log binlog(drv.get()); /* Attach a custom event parser which produces user defined events */ mysql::Basic_transaction_parser transaction_parser; Incident_handler incident_handler; Replay_binlog replay_handler; binlog.content_handler_pipeline()->push_back(&transaction_parser); binlog.content_handler_pipeline()->push_back(&incident_handler); binlog.content_handler_pipeline()->push_back(&replay_handler); int ret = binlog.connect(); if (0 != ret) { std::cerr << "Can't connect to the master.\n"; return -1; } if (binlog.set_position(4) != ERR_OK) { std::cerr << "Can't reposition the binary log reader.\n"; return -1; } Binary_log_event *event; bool quit = false; while (!quit) { /* Pull events from the master. This is the heart beat of the event listener. */ if (binlog.wait_for_next_event(&event)) { quit = true; continue; } /* Print the event */ std::cout << "MainLoop: Event type: [" << mysql::system::get_event_type_str(event->get_event_type()) << "] length: " << event->header()->event_length << " next pos: " << event->header()->next_position << std::endl; /* Perform a special action based on event type */ switch (event->get_event_type()) { case mysql::QUERY_EVENT: { const mysql::Query_event *qev = static_cast<const mysql::Query_event *>(event); std::cout << "query= " << qev->query << " db= " << qev->db_name << std::endl << std::endl; if (qev->query.find("DROP TABLE REPLICATION_LISTENER") != std::string::npos || qev->query.find("DROP TABLE `REPLICATION_LISTENER`") != std::string::npos) { quit = true; } } break; case mysql::ROTATE_EVENT: { mysql::Rotate_event *rot = static_cast<mysql::Rotate_event *>(event); std::cout << "filename= " << rot->binlog_file.c_str() << " pos= " << rot->binlog_pos << std::endl << std::endl; } break; } delete event; } return 0; }
int main(int argc, char** argv) { string uri; int number_of_args= argc; char cwd[PATH_MAX]; getcwd(cwd, PATH_MAX); parse_args(&argc, argv); while (argc != number_of_args) { uri= argv[argc++]; if ( strncmp("mysql://", uri.c_str(), 8) != 0) { uri.insert(0, "file://"); if (uri[7] == '.') { uri.insert(7, cwd); uri.insert((7 + strlen(cwd)), "/"); } cout << uri << endl; } Binary_log binlog(create_transport(uri.c_str())); int error_number= binlog.connect(); if (const char* msg= str_error(error_number)) cerr << msg << endl; if (error_number != ERR_OK) { cerr << "Unable to setup connection" << endl; exit(2); } if (binlog.set_position(opt_start_pos) != ERR_OK) { cerr << "The specified position " << opt_start_pos << " cannot be set" << endl; exit(2); } if (opt_format_flag) { map<string, int>::iterator it; for (it= opt_format.begin(); it != opt_format.end(); ++it) { if (it->second > 10 && it->second != 15) { cout << setw(17) << left << it->first; } } cout << endl; } else { cout << setw(17) << left << "Start Position" << setw(15) << left << "End Position" << setw(15) << left << "Event Length" << setw(25) << left << "Event Type" << endl; } while (true) { Binary_log_event *event; long int event_start_pos; string event_type; string database_dot_table; error_number= binlog.wait_for_next_event(&event); if (const char* msg= str_error(error_number)) cerr << msg << endl; if (error_number != ERR_OK) exit(2); if (event->get_event_type() == mysql::INCIDENT_EVENT || (event->get_event_type() == mysql::ROTATE_EVENT && event->header()->next_position == 0 || event->header()->next_position == 0)) { /* If the event type is an Incident event or a Rotate event which occurs when a server starts on a fresh binlog file, the event will not be written in the binlog file. */ event_start_pos= 0; } else { event_start_pos= binlog.get_position() - (event->header())->event_length; } if (event_start_pos >= opt_stop_pos) break; if ((opt_offset > 0 && opt_offset--) || (opt_db_flag && check_event_db(&event)) || (opt_event_flag && check_event_type(&event))) continue; /* Filtering events if --include or --exclude option is specified */ if ((opt_exclude_flag || opt_include_flag) && (event->get_event_type() == mysql::TABLE_MAP_EVENT || event->get_event_type() == mysql::PRE_GA_WRITE_ROWS_EVENT || event->get_event_type() == mysql::PRE_GA_UPDATE_ROWS_EVENT || event->get_event_type() == mysql::PRE_GA_DELETE_ROWS_EVENT || event->get_event_type() == mysql::WRITE_ROWS_EVENT || event->get_event_type() == mysql::WRITE_ROWS_EVENT_V1 || event->get_event_type() == mysql::UPDATE_ROWS_EVENT || event->get_event_type() == mysql::UPDATE_ROWS_EVENT_V1 || event->get_event_type() == mysql::DELETE_ROWS_EVENT || event->get_event_type() == mysql::DELETE_ROWS_EVENT_V1)) { if (event->get_event_type() == mysql::TABLE_MAP_EVENT) { Table_map_event *table_map_event= static_cast<Table_map_event*>(event); database_dot_table= table_map_event->db_name; database_dot_table.append("."); database_dot_table.append(table_map_event->table_name); tid2tname[table_map_event->table_id]= database_dot_table; } else { // It is a row event Row_event *row_event= static_cast<Row_event*>(event); tb_it= tid2tname.begin(); tb_it= tid2tname.find(row_event->table_id); if (tb_it != tid2tname.end()) { database_dot_table= tb_it->second; if (row_event->flags == Row_event::STMT_END_F) tid2tname.erase(tb_it); } } bool match; if (opt_include_flag) { match= filter_table_by_regex(database_dot_table, 'i'); if (!match) continue; } if (opt_exclude_flag) { match= filter_table_by_regex(database_dot_table, 'e'); if (match) continue; } } if (opt_format_flag) { int info_flag= 0; map<string, int>::iterator it= opt_format.begin(); for (it= opt_format.begin(); it != opt_format.end(); ++it) { cout << setw(17) << left; if (it->second == 11) cout << event_start_pos; if (it->second == 12) cout << (event->header())->next_position; if (it->second == 13) cout << (event->header())->event_length; if (it->second == 14) cout << get_event_type_str(event->get_event_type()) << "[" << event->get_event_type() << "]"; if (it->second == 15) info_flag= 1; } cout << endl; if (info_flag) { cout << "Event info: "; opt_para_flag ? event->print_long_info(cout) : event->print_event_info(cout); cout << endl; } } else { cout << setw(17) << left << event_start_pos << setw(15) << left << (event->header())->next_position << setw(15) << left << (event->header())->event_length << get_event_type_str(event->get_event_type()) << "[" << event->get_event_type() << "]" << endl; cout << "Event Info: "; opt_para_flag ? event->print_long_info(cout) : event->print_event_info(cout); cout << endl; } } }//end of outer while loop }
int main(int argc, char** argv) { if (argc < 2) { fprintf(stderr,"Usage:\n\treplaybinlog URL\n\nExample:\n\treplaybinlog mysql://root:[email protected]:3306\n\n"); return (EXIT_FAILURE); } mysql::Binary_log binlog(mysql::system::create_transport(argv[1])); if (argc > 2) { // Set ssl ca certificate binlog.set_ssl_ca(std::string(argv[2])); // Set ssl cipher list //binlog.set_ssl_cipher("AES256-SHA"); //RDS ssl_cipher list //binlog.set_ssl_cipher("EXP1024-RC4-SHA:EXP1024-DES-CBC-SHA:AES256-SHA:AES128-SHA:DES-CBC3-SHA:DES-CBC-SHA:EXP-DES-CBC-SHA:EXP-RC2-CBC-MD5:RC4-SHA:RC4-MD5:EXP-RC4-MD5:NULL-SHA:NULL-MD5:DES-CBC3-MD5:DES-CBC-MD5:EXP-RC2-CBC-MD5:RC2-CBC-MD5:EXP-RC4-MD5:RC4-MD5:KRB5-DES-CBC3-MD5:KRB5-DES-CBC3-SHA:ADH-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:EDH-DSS-DES-CBC3-SHA:ADH-AES256-SHA:DHE-RSA-AES256-SHA:DHE-DSS-AES256-SHA:ADH-AES128-SHA:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA:EXP-KRB5-RC4-MD5:EXP-KRB5-RC2-CBC-MD5:EXP-KRB5-DES-CBC-MD5:KRB5-RC4-MD5:KRB5-DES-CBC-MD5:ADH-RC4-MD5:EXP-ADH-RC4-MD5:DHE-DSS-RC4-SHA:EXP1024-DHE-DSS-RC4-SHA:EXP1024-DHE-DSS-DES-CBC-SHA:EXP-KRB5-RC4-SHA:EXP-KRB5-RC2-CBC-SHA:EXP-KRB5-DES-CBC-SHA:KRB5-RC4-SHA:KRB5-DES-CBC-SHA:ADH-DES-CBC-SHA:EXP-ADH-DES-CBC-SHA:EDH-RSA-DES-CBC-SHA:EXP-EDH-RSA-DES-CBC-SHA:EDH-DSS-DES-CBC-SHA:EXP-EDH-DSS-DES-CBC-SHA"); } /* Attach a custom event parser which produces user defined events */ mysql::Basic_transaction_parser transaction_parser; Incident_handler incident_hndlr; Replay_binlog replay_hndlr; binlog.content_handler_pipeline()->push_back(&transaction_parser); binlog.content_handler_pipeline()->push_back(&incident_hndlr); binlog.content_handler_pipeline()->push_back(&replay_hndlr); if (binlog.connect()) { fprintf(stderr,"Can't connect to the master.\n"); return (EXIT_FAILURE); } if (binlog.set_position(4) != ERR_OK) { fprintf(stderr,"Can't reposition the binary log reader.\n"); return (EXIT_FAILURE); } Binary_log_event *event; bool quit= false; while(!quit) { /* Pull events from the master. This is the heart beat of the event listener. */ if (binlog.wait_for_next_event(&event)) { quit= true; continue; } /* Print the event */ std::cout << "Event type: " << mysql::system::get_event_type_str(event->get_event_type()) << " length: " << event->header()->event_length << " next pos: " << event->header()->next_position << std::endl; /* Perform a special action based on event type */ switch(event->header()->type_code) { case mysql::QUERY_EVENT: { const mysql::Query_event *qev= static_cast<const mysql::Query_event *>(event); std::cout << "query= " << qev->query << " db= " << qev->db_name << std::endl << std::endl; if (qev->query.find("DROP TABLE REPLICATION_LISTENER") != std::string::npos || qev->query.find("DROP TABLE `REPLICATION_LISTENER`") != std::string::npos) { quit= true; } } break; case mysql::ROTATE_EVENT: { mysql::Rotate_event *rot= static_cast<mysql::Rotate_event *>(event); std::cout << "filename= " << rot->binlog_file.c_str() << " pos= " << rot->binlog_pos << std::endl << std::endl; } break; } // end switch delete event; } // end loop return (EXIT_SUCCESS); }
int main(int argc, char** argv) { connLocal[0].carregaConf(); connLocal[0].conectar(); connLocal[0].pesquisaServidores(); if (!connLocal[0].pesquisaServidores()) { std::cout << "Erro ao pesquisar servidores" << std::endl; logErro("Erro ao pesquisar servidores"); exit(2); } while (row = mysql_fetch_row(result)) { indiceServidores++; banco[indiceServidores].setDados(row[0], row[1], row[2], row[3], row[4]); banco[indiceServidores].conectar(); } char url[128]; sprintf(url, "mysql://%s@%s:%s", connLocal[0].getUsuario().c_str(), //connLocal[0].getSenha().c_str(), connLocal[0].getHost().c_str(), connLocal[0].getPorta().c_str()); std::cout << url << std::endl; Binary_log binlog(create_transport(url)); binlog.connect(); //std::cout << url << std::endl; // Binary_log_event *event; while (true) { Binary_log_event *event; int result = binlog.wait_for_next_event(&event); if (result == ERR_EOF) break; std::cout << "Found event of type " << event->get_event_type() << std::endl; //switch (event->header()->type_code) { switch (event->get_event_type()) { case QUERY_EVENT: { const mysql::Query_event *qev = static_cast<const mysql::Query_event *>(event); //std::cout << "Banco comando: " << qev->db_name << std::endl; if (strcmp(qev->db_name.c_str(), "DefaultBD") == 0) { // FILTRO PELO BANCO CONFIGURADO //std::cout << static_cast<Query_event*>(event)->query << std::endl; //std::cout << "Evento tipo: " << event->get_event_type() << std::endl; /* * Remove BEGIN antes de todas as querys */ std::stringstream query; query << qev->query << ";"; size_t found; found = query.str().find("BEGIN"); if (found) { for (int i = 0; i <= indiceServidores; i++) { std::cout << query.str() << std::endl; banco[i].checaConexao(); banco[i].query("use DefaultBD;"); banco[i].query(query.str()); std::cout << "Executando em servidor: " << banco[i].getHost() << std::endl; } log(query.str()); } } } break; } } }