Python切换到新master的脚本
今天看书上有个不错的切换master的脚本,python编写的;可以自动切换到新master上的脚本,拿下来分享下;
[html]
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)
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)
补充:Web开发 , Python ,