import os script_file = "./skip_file.sql" def write_script(script_content): file_handle = open(script_file, 'a+') file_handle.writelines(script_content + " ") file_handle.close() def delete_script_file(): if os.path.exists(script_file): os.remove(script_file) def get_skip_script_list(master_uuid, start_tran_id, end_tran_id): script_list = [] current_tran_id = start_tran_id while current_tran_id <= end_tran_id: current_script = """ SET @@SESSION.GTID_NEXT= '{master_uuid}:{tran_id}'; BEGIN; COMMIT; """.format( master_uuid=master_uuid, tran_id=current_tran_id ) current_tran_id = current_tran_id + 1 script_list.append(current_script) script_list.append("SET SESSION GTID_NEXT = AUTOMATIC;") return script_list def main(): master_uuid = "0275e614-b4b1-11e7-8f24-b8ca3a6fcb25" start_tran_id = 1 end_tran_id = 177 script_list = get_skip_script_list(master_uuid, start_tran_id, end_tran_id) write_script(" ".join(script_list)) if __name__ == '__main__': main()
当从库误执行一些SQL时或其他原因导致主从上的gtid集合不一致时,可以通过这种方式生成一些空事务,避免主从切换后发生从库无法获取新主库已purge掉的binlog而中断。