看到有個不錯的切換master的腳本,python編寫的;可以自動切換到新master上的腳本,拿下來分享下;
def replicate_to_position(server,pos):
server.sql("START SLAVE UNTIL MASTER_LOT_FILE=%s,MASTER_LOG_POS=%s",(pos.file,po
s.pos))
server.sql("SELECT MASTER_POS_WAIT(%s,%s)",(pos.file,pos.pos))
def switch_to_master(server,standby)
stop_slave(server)
stop_slave(standby)
server_pos = fetch_slave_position(server)
standby_pos = fetch_slave_position(standby)
if server_pos < standby_pos:
replicate_to_position(server,standby_pos)
elif server_pos > standby_pos:
replicate_to_position(standby,server_pos)
master_pos = fetch_master_positino(standby)
change_master(server,standby,master_pos)
start_slave(standby)
start_slave(server)