亚豪平台-邕宁区人民医院什么走

首页

AD联系:507867812

亚豪平台

时间:2019-11-17 12:42:58 作者:太阳城申博 浏览量:21110

亚豪平台

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linuxMySQL数据库主从正常切换IP脚本

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linuxMySQL数据库主从正常切换IP脚本,见下图

MySQL数据库主从正常切换IP脚本

MySQL数据库主从正常切换IP脚本MySQL数据库主从正常切换IP脚本,见下图

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linuxMySQL数据库主从正常切换IP脚本MySQL数据库主从正常切换IP脚本MySQL数据库主从正常切换IP脚本,如下图

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

MySQL数据库主从正常切换IP脚本

如下图

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux,如下图

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux,见图

亚豪平台MySQL数据库主从正常切换IP脚本

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

MySQL数据库主从正常切换IP脚本

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linuxMySQL数据库主从正常切换IP脚本

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

MySQL数据库主从正常切换IP脚本

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

MySQL数据库主从正常切换IP脚本

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linuxMySQL数据库主从正常切换IP脚本MySQL数据库主从正常切换IP脚本

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linuxMySQL数据库主从正常切换IP脚本

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linuxMySQL数据库主从正常切换IP脚本MySQL数据库主从正常切换IP脚本

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

亚豪平台MySQL数据库主从正常切换IP脚本

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linuxMySQL数据库主从正常切换IP脚本

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linuxMySQL数据库主从正常切换IP脚本MySQL数据库主从正常切换IP脚本

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

MySQL数据库主从正常切换IP脚本

1.MySQL数据库主从正常切换IP脚本

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linuxMySQL数据库主从正常切换IP脚本MySQL数据库主从正常切换IP脚本MySQL数据库主从正常切换IP脚本

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linuxMySQL数据库主从正常切换IP脚本

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linuxMySQL数据库主从正常切换IP脚本MySQL数据库主从正常切换IP脚本

2.MySQL数据库主从正常切换IP脚本。

MySQL数据库主从正常切换IP脚本

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linuxMySQL数据库主从正常切换IP脚本

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

3.MySQL数据库主从正常切换IP脚本。

MySQL数据库主从正常切换IP脚本MySQL数据库主从正常切换IP脚本MySQL数据库主从正常切换IP脚本MySQL数据库主从正常切换IP脚本

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linuxMySQL数据库主从正常切换IP脚本

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

4.

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

MySQL数据库主从正常切换IP脚本MySQL数据库主从正常切换IP脚本MySQL数据库主从正常切换IP脚本

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linuxMySQL数据库主从正常切换IP脚本MySQL数据库主从正常切换IP脚本MySQL数据库主从正常切换IP脚本

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linuxMySQL数据库主从正常切换IP脚本。亚豪平台

展开全文
相关文章
网上手机真人赌博平台

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux

菠菜公社论坛

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux....

银河娱乐网址

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux....

澳门金沙网址大全

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux....

金鼎娱乐

此脚本主要用于mysql数据库主从在正常情况下,因某种原因需要切换IP的情况下使用,主要就是切换IP,主要是通过内网切换公网IP,脚本代码如下:

#!/usr/bin/envpython #-*-coding:utf-8-*- #scriptusedinSlaveServerforchangeserverip importos,sys,time,socket,paramiko,MySQLdb defmysql_connect(ip,sql):#数据库连接 try: conn=MySQLdb.connect(host=ip,user='repl',passwd='VQrtetr8',connect_timeout=5) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) alldata=cursor.fetchall() returnalldata exceptMySQLdb.Error,e: returne.args[0] defssh_conm(conm):#连接主库服务器并执行命令 host=mysql_connect('127.0.0.1',"showslavestatus")[0]["Master_Host"] #stat=mysql_chcek(host) username='root' password='3141315' port=63008 s=paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,port,username,password) stdin,stdout,stderr=s.exec_command(conm) result=stdout.read() s.close() returnresult deflog_pos():#获取当前主从的同步位置 result=mysql_connect('127.0.0.1',"showslavestatus") host=result[0]["Master_Host"] s_log=result[0]["Relay_Master_Log_File"].split('.')[1] s_pos=result[0]["Exec_master_log_pos"] result=mysql_connect(host,"showmasterstatus") ifresult==2003:#这里是应对mysql数据库无法连接,可以选择据继续和终止脚本 choose=raw_input('CannotconnectMaster,Doyougoon? (yorn):') ifchoose=='y': return0,0,0,0 else: sys.exit(1) m_log=result[0]["File"].split('.')[1] m_pos=result[0]["Position"] returns_log,m_log,s_pos,m_pos defshow():#循环5次判断主从同步是否一致 print"CheckMaster-Slavenow..." foriinrange(5): s_log,m_log,s_pos,m_pos=log_pos() ifs_log==m_logands_pos==m_pos: print"SlaveandMasterisOK." break else: print"SlaveandMasternotOK,pleasewaitmoment." ifi==4: #print"MustbeshowReplcationError,Quitthisscriptnow!" choose=raw_input('Master-slavenotsynchronous,Doyougoon? (yorn):') ifchoose=='y': break else: sys.exit(1) i=i+1 time.sleep(5) defreplace(): #显示当前主从的IP,并检测主从3306是否存在 print"BeforeswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip print #1:停用主的网卡 conm="ifdowneth0" ssh_conm(conm) #2:判断主从同步是否一致,如果一致则切换,不一致则手动查看问题所在(不一致的情况重复检测5次,间隔5s,检测五次依然不同步可以选择停止脚本的执行或继续执行) show() #3:修改从IP为主的IP os.popen("sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(s_ip,m_ip)) print"SlavechangeIPisOK" #4:修改主IP为从的IP conm="sed-i's/%s/%s/'/etc/sysconfig/network-scripts/ifcfg-eth0"%(m_ip,s_ip) ssh_conm(conm) print"MasterchangeIPisOK" #5:显示替换之后主从的IP print print"AfterswitchingIP:" s_ip=os.popen("cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'").read().split()[:1][0] print"s_ip:"+s_ip conm="cat/etc/sysconfig/network-scripts/ifcfg-eth0|grepIPADDR|awk-F'=''{print}'" m_ip=ssh_conm(conm).split()[:1][0] print"m_ip:"+m_ip #6:停止主从同步 mysql_connect("127.0.0.1","slavestop") print"SlavestopOK" #7:重启用主从的网卡 print print"Networkservicerestartnow,pleasewait......" conm="ifupeth0" ssh_conm(conm) os.system("ifdowneth0") os.system("ifupeth0") print"NetworkreatrtOK!"if__name__=="__main__": replace() linux....

相关资讯
热门资讯