mysql 数据同步 代码脚本.docVIP

  • 12
  • 0
  • 约8.89千字
  • 约 7页
  • 2018-01-02 发布于河南
  • 举报
mysql 数据同步 代码脚本

#!/bin/env python # encoding:utf-8 import MySQLdb import traceback import time import winsound #报警提示声音文件 mp3=rC:/Documents and Settings/Administrator/桌面/replication/master alarm1.wav #主从机数据库信息字典 rep_master = { host:7, user : repacct-1872172, passwd:a17a3b495d9c23cedee6380aad87f25f, db: test, port:3306 }; rep_slave = { host:13, user : repacct-1872172, passwd:a17a3b495d9c23cedee6380aad87f25f, db: test, port:3307 }; #状态显示域 master_status_fields = { File:0, Position:1, } slave_status_fields = { Slave_IO_State: 0, Master_Host: 1, Master_Port: 3, Connect_Retry:4, Master_Log_File: 5, Read_Master_Log_Pos:6, Slave_IO_Running: 10, Slave_SQL_Running: 11, Last_IO_Errno: 34, Last_Errno:18#这个errno应该不能作为判断依据,因为网络中断的时候,他的number号还是0 }; #获取数据库链接 def get_conn(host_info): conn = MySQLdb.Connect(host=host_info[host], user=host_info[user], passwd=host_info[passwd], db=host_info[db], port=host_info[port]) if 0: conn.set_character_set(gb2312) cur = conn.cursor() cur.execute(set names gb2312;) #conn.ping() #print conn,conn return conn Ping: Checks whether the connection to the server is working. If the connection has gone down, an attempt to reconnect is made unless auto-reconnect is disabled. This function can be used by clients that remain idle for a long while, to check whether the server has closed the connection and reconnect if necessary. #是否ping通? def conn_ping(conn): conn.ping() #获取链接的游标? def get_cur(conn): return conn.cursor() #数据查询 def query_sql(conn, sql):#有conn情况下的查询 for i in range(1): try: cur = conn.cursor() cur.execute(sql) return cur.fetchall() except: #flog.flog(数据库连接错误: %s % traceback.format_exc()) print DB error: %s % traceback.format_exc() #conn = get_conn(True) #end of GetOneData #暂时还没用到2011年7月5日10:21:36 def exec_sql(conn, sql): for i in rang

文档评论(0)

1亿VIP精品文档

相关文档