MySQL数据库主从切换脚本自动化

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import MySQLdb

import time

import sys

class m_s:

def __init__(self,host,user,password,port):

self.host=host

self.user=user

self.passowrd=password

self.port=port

def getConn(self,db="mysql"):

try:

conn=MySQLdb.connect(host=self.host, user=self.user, passwd=self.passowrd, db=db, port=self.port, charset="utf8")

cur = conn.cursor()

return cur

except Exception as e:

return e

def execSQLlock(self,*args):

flush_sql="FLUSH TABLES WITH READ LOCK"

cur.execute(flush_sql)

def execIo(self,cur,command):

cur.execute(command)

db_pos = cur.fetchall()

for value in db_pos:

value=value

return value

def exeStop(self,cur,command):

cur.execute(command)

db_pos = cur.fetchall()

return db_pos

def execSQLstatus(self,*args):

n=0

self.execSQLlock(cur)

flush_m ="flush logs"

cur.execute(flush_m)

while True:

data=[]

slave_pos=[]

n=n+1

exe_sql ="select Command,State,Info,Id from information_schema.processlist"

cur.execute(exe_sql)

plist = cur.fetchall()

for li in range(len(plist)):

if plist[li][0]=="Query" and plist[li][1]=="Waiting for global read lock":

lock_id ="kill " + str(plist[li][3])

print plist[li][2]

cur.execute(lock_id)

slave_pos.append(self.execIo(cur1, "show master status")[1])

data.append(self.execIo(cur1,"show slave status")[6]) ##从库的游标

time.sleep(1)

slave_pos.append(self.execIo(cur1, "show master status")[1])

data.append(self.execIo(cur,"show master status")[1])##从库的游标

print ".......",data,slave_pos

if data[0]==data[1] and slave_pos[0]==slave_pos[1]:

try:

print "第%s次判断数据已经同步....."%n

if n==c_time:

print "开始主从切换工作........"

self.exeStop(cur1,"stop slave") ##停止从库同步

new_pos=self.exeStop(cur1,"show master status")#获取新主库的FILE和POS的值,游标为还没切换前的从库

#print "获取新主库的FILE和POS的值,游标为还没切换前的从库",new_pos

self.exeStop(cur,"reset master;")##主库释放从库主从信息......

##在从库执行new_change 指向新的主库

self.exeStop(cur1,"reset slave all")

new_change="change master to master_host=‘"+str(args[1])+"‘"+",master_user=‘"+args[2]+"‘"+",master_password=‘"+args[3]+"‘,master_port="+str(args[4])+",MASTER_LOG_FILE=‘"+str(new_pos[0][0])+"‘"+",MASTER_LOG_POS="+str(new_pos[0][1])

print new_change

self.exeStop(cur,new_change)##在原来主库上执行change master to.....

#print "在原来主库上执行change master to...."

self.exeStop(cur,"start slave")  ##在原来主库上执行change master to.....

time.sleep(5)

s_pos=self.exeStop(cur,"show slave status;")

#print s_pos[0][10],s_pos[0][11]

if s_pos[0][10]=="Yes" and s_pos[0][11]=="Yes":

self.exeStop(cur1,"reset slave all")

print  "主从切换成功!"

print "\n"

while True:

print "等待其他操作完成,即将unlock tables主库......"

try:

stop =raw_input("输入终止命令q即完成此次操作:\n")

if stop=="q":

sys.exit()

# break

except Exception as e:

print "good bye"

else:

print   s_pos[0][19]

break

except Exception as e:

return e

else:

print "主从数据未达到一致性..........",n

n=0

data=[]

if __name__=="__main__":

c_time=int(raw_input("多少秒后进行主从切换..>>"))

print "****************************************************\n"

print "请根据提示输入指定信息:"

m_host =raw_input("目前主库的地址:")

m_user =raw_input("目前主库的登陆用户名:")

m_password =raw_input("目前主库的密码:")

m_port =int(raw_input("目前主库的端口:"))

print "****************************************************\n"

s_host =raw_input("目前从库的地址:")

s_user =raw_input("目前从库的登陆用户名:")

s_password =raw_input("目前从库的密码:")

s_port =int(raw_input("目前从库的端口:"))

M = m_s(m_host, m_user, m_password, m_port)

S = m_s(s_host,s_user,s_password,s_port)

cur = M.getConn()  ##获取主库游标

cur1 = S.getConn()  ##获取从库游标

print "*****************************************************\n"

print "主从同步用户信息.........\n"

s_user1 =raw_input("输入主从同步的用户>>:")

s_password1 =raw_input("输入主从同步的密码:")

s_port1 =int(raw_input("输入主从同步的端口:"))

M.execSQLstatus(cur,s_host,s_user1,s_password1,s_port1)

原文地址:https://www.cnblogs.com/mynale/p/11018500.html

时间: 2024-08-05 08:38:47

MySQL数据库主从切换脚本自动化的相关文章

京东MySQL数据库主从切换自动化

1. 产生背景 随着京东业务的高速增长,数据的重要性对于京东来说重要程度不说自明,在信息时代,数据有着比人们更大的力量,数据库的价值可见一斑,数据库的存在为人们提供了更快的查询,那么为了更好地做到数据库的高可用,保证持续提供服务,简化DBA操作,节省数据库故障切换的时间,故开发此数据库主从切换自动化系统. 2. 实现原理 此系统基于MHA做数据库切换,结合京东数据库切换的特点,定制自己的切换系统.MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的

京东DBA 王伟 | MySQL数据库主从切换自动化

http://mp.weixin.qq.com/s?__biz=MjM5ODI5Njc2MA==&mid=2655808515&idx=1&sn=069d05a3bad6f67f400475ca6ad714c2&chksm=bd7421d48a03a8c274570da116945bfd6a2eb90615d8b7fba9d78e57890604bd1c415591f81d&mpshare=1&scene=23&srcid=01101Pn9HfMWE

mysql+keepalived主从切换脚本 转

Keepalived MySQL故障自动切换脚本 MySQL架构为master-slave(主从),master故障自动切换到slave上.当然也可以设置为双master,但这里有个弊端:就是当主的压力很大时,从上延时很大,比如落后2000秒,此时主挂了,从接管(VIP漂移到从),用户刚才发表的文章,此时因为同步延时大,还没复制过来,于是用户又发表了一篇文章,当原来的master修好后,因从的IO和SQL线程还在开启状态,还会继续同步刚才没有同步复制完的数据,这时有可能把用户新发表的文章更改掉,

MySQL数据库性能优化及自动化运维实践教程!DBA日常工作

MySQL数据库性能优化及自动化运维实践教程!本文作者将站在更加全面的角度分享他在这一年多 DBA 工作中的经验,希望可以给大家带来启发和帮助. DBA 的日常工作 我觉得 DBA 真的很忙,我们来看看 DBA 的具体工作:备份和恢复.监控状态.集群搭建与扩容.数据迁移和高可用. 上面这些是我们 DBA 的功能,了解这些功能以后要对体系结构有更加深入的了解,你不知道怎么处理这些故障和投诉的事情. 所以我们要去了解缓存/线程.SQL 优化.存储引擎.SQL 审计以及锁与实务:体系结构更深一点,就去

Linux下定时备份MySQL数据库的Shell脚本

Linux下定时备份MySQL数据库的Shell脚本 对任何一个已经上线的网站站点来说,数据备份都是必须的.无论版本更新还是服务器迁移,备份数据的重要性不言而喻.人工备份数据的方式不单耗费大量时间和精力,还灰常不专业的说.于是,有了下面这段脚本的出现.参考了网上的很多教程,外加自己的测试,以下脚本经测试可用.#!/bin/bash#Shell Command For Backup MySQL Database Everyday Automatically By Crontab#Author :

mysql数据库主从同步

环境: Mater:   CentOS7.1  5.5.52-MariaDB  192.168.108.133 Slave:   CentOS7.1  5.5.52-MariaDB  192.168.108.140 1.导出主服务数据,将主备初始数据同步 master: //从master上导出需要同步的数据库信息 mysqldump -u*** -p*** --database test > test.sql //将master上的备份信息传输到slave上 scp /root/test.sq

mysql数据库统计QPS脚本

如下脚步是对mysql数据库统计QPS脚本: #!/bin/bash mysqladmin -uroot-p'xxxx' extended-status -i1|awk 'BEGIN{local_switch=0;print"QPS   Commit Rollback   TPS   Threads_con Threads_run\n------------------------------------------------------- "} $2 ~/Queries$/    

linux下mysql数据库主从同步配置

说明: 操作系统:CentOS 5.x 64位 MySQL数据库版本:mysql-5.5.35 MySQL主服务器:192.168.21.128 MySQL从服务器:192.168.21.129 准备篇: 说明:在两台MySQL服务器192.168.21.128和192.168.21.129上分别进行如下操作 备注: 作为主从服务器的MySQL版本建议使用同一版本! 或者必须保证主服务器的MySQL版本要高于从服务器的MySQL版本! 一.配置好IP.DNS .网关,确保使用远程连接工具能够连接

Linux系统中MySQL数据库“主从”配置

MySQL数据库"主从"配置 一.配置概述: 在Linux操作系统中配置MySQL数据库的主从复制是很重要的,为什么这样说呢!是因为在你搭建一个服务器以后你配置了2台数据库,一主一从.2台数据库配置好以后,当其中一台数据库被攻击以后,你被攻击的这台数据库的数据会被自动的备份到另一台数据库中去.即使被攻击了数据也不会丢失.配置主从就是为了让web网站或其他的软件可以正常运转,确保数据的存在. 二.配置步骤: <配置具备的条件> 1.需要在虚拟机上安装好两台主机,同时安装好同一