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

猜你喜欢

转载自blog.csdn.net/sunyuhua_keyboard/article/details/80610262