- 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
您可能关注的文档
最近下载
- 提高市政雨水管道接口施工一次验收合格率.docx VIP
- 食品企业管理体系建立与认证吴亨覃海元61课件.pptx
- 新智能制造产业领域的智能仓储与物流解决方案.pptx VIP
- 电子科技大学860软件工程学科基础综合2020年考研专业课初试大纲.pdf VIP
- 2026人教版小学英语四年级上册期末综合试卷(3套含答案解析 打印版).docx
- 复杂底座的三维造型及数控加工工艺设计.docx VIP
- 2024北京昌平一中高三11月月考英语试题及答案.pdf VIP
- “建筑制图基础实训作业”及“建筑制图与识图实训小作业1-4部分参考答案”.pdf VIP
- 《食品企业管理体系建立与认证》课程标准.docx VIP
- 2025年房地产经纪人养老地产风险管理与应急预案专题试卷及解析.pdf VIP
原创力文档

文档评论(0)