Python 检查主从数据库的数据是否一致

import pymysql
connection_master=pymysql.connect(host='10.3.XXXX',
                           user='XXXX',
                           password='XXXX',
                           db='XXXX',
                           port=3306,
                           charset='utf8')
connection_slave=pymysql.connect(host='XXXX',
                           user='XXXX',
                           password='XXXX',
                           db='XXX',
                           port=3306,
                           charset='utf8')
schema='XXX' #要导出的数据库的名字
cursor=connection_master.cursor()
cursor_slave=connection_slave.cursor()
sql="select table_name from information_schema.tables where TABLE_SCHEMA = '"+str(schema)+"'"
cursor.execute(sql)
tableNameList=cursor.fetchall()
for tableName in tableNameList:
    try:
        compileTableSql="select count(1) from "+str(tableName[0])
        cursor.execute(compileTableSql)
        tableDataNumber=cursor.fetchone()
        cursor_slave.execute(compileTableSql)
        tableSlaveDataNumber=cursor_slave.fetchone()
        if tableDataNumber==tableSlaveDataNumber:
            print("table_name: "+str(tableName[0])+"  is same")
        else:
            print("table_name: "+str(tableName[0])+"  is diff")
    except:
        pass
    continue
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MonkeyKing.sun

对你有帮助的话,可以打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值