检查mysql主从数据一致性,检查mysql主从重要表数据一致性

mysql数据库主从做起来不难,但是主从数据的一致性很重要,本脚本用于粗略检查mysql数据库主从重要表的数据一致性,主要是在主从正常的情况下,连接数据库统计表的条数是否一致,脚本分为两部分,一个是py脚本,一个是ini配置文件,py脚本源码如下:

#!/usr/bin/env python

#This script is used check mysql replcation some table

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

import sys,time,MySQLdb,threading

import ConfigParser

class Check:

def __init__(self):

self.w =[]

self.table = table

def conf(self):

fp = ConfigParser.ConfigParser()

fp.readfp(open('config.ini'))

iplist = fp.get("global", "iplist")

return iplist

def wd(self,table):

iplist = self.conf()

hostlist = []

self.w = []

threads = []

for i in iplist.split(";"):

hostlist.append(i.split(","))

for i in range(len(hostlist)):

host = hostlist[i][0]

hostname = hostlist[i][1]

t = threading.Thread(target=self.mysql_check,args=(host,hostname,i))

threads.append(t)

t.start()

time.sleep(0.05)

for i in range(len(hostlist)):

threads[i].join()

self.w.sort()

for r in range(len(self.w)):

print self.w[r]

def mysql_check(self,host,hostname,i):

try:

conn = MySQLdb.connect(host = host,user = 'user',passwd = 'passwd',connect_timeout=5)

cursor = conn.cursor()

sql = "SELECT COUNT(*) FROM %s" % self.table

cursor.execute(sql)

alldata = cursor.fetchall()

count = alldata[0][0]

value = hostname + "\t" + str(count)

self.w.append(value)

except:

print "Can not Connect to " + host + " mysql server"

return 0

if __name__ == "__main__":

table_list = ['aa.aa','bb.bb','cc.cc']

for i in range(len(table_list)):

table = table_list[i]

print "Table Count: " + table

print

boss = Check()

boss.wd(table)

print

config.ini 文件格式如下

[global]

iplist = 192.168.50.1,vvv(主);192.168.50.2,vvv(从);192.168.50.3,ttt(主);192.168.50.4,ttt(从)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值