Python中MySQLdb和torndb模块对MySQL的断连问题处理
在使用python 对wordpress tag 进行细化代码处理时,遇到了调用MySQLdb模块时的出错,由于错误提示和问题原因相差甚远,查看了N久代码也未发现代码有问题。后来问了下师傅,被告知MySQLdb里有一个断接的坑 ,需要进行数据库重连解决。
一、报错代码及提示
运行出错的代码如下:
import MySQLdb
def getTerm(db,tag):
cursor = db.cursor()
query = "SELECT term_id FROM wp_terms where name=%s "
count = cursor.execute(query,tag)
rows = cursor.fetchall()
db.commit()
#db.close()
if count:
term_id = [int(rows[id][0]) for id in range(count)]
return term_id
else:return None
def addTerm(db,tag):
cursor = db.cursor()
query = "INSERT into wp_terms (name,slug,term_group) values (%s,%s,0)"
data = (tag,tag)
cursor.execute(query,data)
db.commit()
term_id = cursor.lastrowid
sql = "INSERT into wp_term_taxonomy (term_id,taxonomy,description) values (%s,'post_tag',%s) "
value = (term_id,tag)
cursor.execute(sql,value)
db.commit()
db.close()
return int(term_id)
dbconn = MySQLdb.connect(host='localhost', user='root', passwd='123456', db='361way', port=3306, charset='utf8', init_command='set names utf8')
tags = ['mysql','1111','aaaa','bbbb','ccccc','php','abc','python','java']
tagids = []
for tag in tags:
termid = getTerm(dbconn,tag)
if termid:
print tag, 'tag id is ',termid
tagids.extend(termid)
else:
termid = addTerm(dbconn,tag)
print 'add tag',tag,'id is ' ,termid
tagids.append(termid)
print 'tag id is ',tagids
直接可以执行,在第for循环里第二次调用getTerm函数时,报错如下:
Traceback (most recent call last):
File "a.py", line 40, in <module>
termid = getTerm(dbconn,tag)
File "a.py", line 11, in getTerm
count = cursor.execute(query,tag)
File "/usr/lib64/python2.6/site-packages/MySQLdb/cursors.py", line 154, in execute
charset = db.character_set_name()
_mysql_exceptions.InterfaceError: (0, '')
二、解决方法
初始时以为是编码问题了,又细核对了几遍未发现编码有问题,在python代码里也未发现异常。后来问过师傅后,师傅来了句提示:
只看代码有啥用,mysql 的超时时间调长点或捕获异常从连,原因是
cursor. connection 没有关闭
但是socket已经断了
cursor 这个行为不会再建立一次socket的
重新执行一次MysqlDB.connect()
看的有点懵懂,先从mysql 里查看了所有timeout相关的变量
?
1
mysql> show GLOBAL VARIABLES like "%timeout%";
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| connect_timeout | 10 |
| delayed_insert_timeout | 300 |
| innodb_lock_wait_timeout | 50 |
| innodb_rollback_on_timeout | OFF |
| interactive_timeout | 28800 |
| net_read_timeout | 30 |
| net_write_timeout | 60 |
| slave_net_timeout | 3600 |
| table_lock_wait_timeout | 50 |
| wait_timeout | 28800 |
+----------------------------+-------+
10 rows in set (0.00 sec)
发现最小的超时时间是10s ,而我的程序执行起来显然就不了10s 。因为之前查过相关的报错,这里估计这个很可能是另外一个报错:2006,MySQL server has gone away 。即然和这个超时时间应该没关系,那就尝试通过MySQLdb ping测试,如果捕获异常,就再进行重连,修改后的代码为:
#!/usr/bin/python
#coding=utf-8
import MySQLdb
def getTerm(db,tag):
cursor = db.cursor()
query = "SELECT term_id FROM wp_terms where name=%s "
count = cursor.execute(query,tag)
rows = cursor.fetchall()
db.commit()
#db.close()
if count:
term_id = [int(rows[id][0]) for id in range(count)]
print term_id
return term_id
else:return None
def addTerm(db,tag):
cursor = db.cursor()
query = "INSERT into wp_terms (name,slug,term_group) values (%s,%s,0)"
data = (tag,tag)
cursor.execute(query,data)
db.commit()
term_id = cursor.lastrowid
sql = "INSERT into wp_term_taxonomy (term_id,taxonomy,description) values (%s,'post_tag',%s) "
value = (term_id,tag)
cursor.execute(sql,value)
db.commit()
db.close()
return int(term_id)
dbconn = MySQLdb.connect(host='localhost', user='root', passwd='123456', db='361way', port=3306, charset='utf8', init_command='set names utf8')
tags = ['mysql','1111','aaaa','bbbb','ccccc','php','abc','python','java']
if __name__ == "__main__":
tagids = []
for tag in tags:
try:
dbconn.ping()
except:
print 'mysql connect have been close'
dbconn = MySQLdb.connect(host='localhost', user='root', passwd='123456', db='361way', port=3306, charset='utf8', init_command='set names utf8')
termid = getTerm(dbconn,tag)
if termid:
print tag, 'tag id is ',termid
tagids.extend(termid)
else:
termid = addTerm(dbconn,tag)
print 'add tag',tag,'id is ' ,termid
tagids.append(termid)
print 'All tags id is ',tagids
再执行发现竟然OK了,而细看下结果,发现基本上每1-2次getTerm或addTerm函数调用就会打印一次'mysql connect have been close' 。
三、使用torndb模块解决mysql断连问题
1.MySQLdb和torndb的代码样例对比
torndb是facebook开源的一个基于MySQLdb二次封装的一个mysql模块,新封装的这个模块比较小,是一个只有2百多行代码的py文件。虽然代码短,功能确相较MySQLdb简便不少,并且该模块由于增加了reconnect方法和max_idel_time参数,解决了mysql的断连问题。比较下使用原生MySQLdb模块和使用torndb模块的代码:
使用MySQLdb模块的代码
import MySQLdb
def getTerm(db,tag):
cursor = db.cursor()
query = "SELECT term_id FROM wp_terms where name=%s "
count = cursor.execute(query,tag)
rows = cursor.fetchall()
db.commit()
#db.close()
if count:
term_id = [int(rows[id][0]) for id in range(count)]
return term_id
else:return None
def addTerm(db,tag):
cursor = db.cursor()
query = "INSERT into wp_terms (name,slug,term_group) values (%s,%s,0)"
data = (tag,tag)
cursor.execute(query,data)
db.commit()
term_id = cursor.lastrowid
sql = "INSERT into wp_term_taxonomy (term_id,taxonomy,description) values (%s,'post_tag',%s) "
value = (term_id,tag)
cursor.execute(sql,value)
db.commit()
db.close()
return int(term_id)
def addCTag(db,data):
cursor = db.cursor()
query = '''INSERT INTO `wp_term_relationships` (
`object_id` ,
`term_taxonomy_id`
)
VALUES (
%s, %s) '''
cursor.executemany(query,data)
db.commit()
db.close()
dbconn = MySQLdb.connect(host='localhost', user='root', passwd='123456', db='361way', port=3306, charset='utf8', init_command='set names utf8')
tags = ['mysql','1111','aaaa','bbbb','ccccc','php','abc','python','java']
tagids = []
for tag in tags:
if termid:
try:
dbconn.ping()
except:
dbconn = MySQLdb.connect(host='localhost', user='root', passwd='123456', db='361way', port=3306, charset='utf8', init_command='set names utf8')
print tag, 'tag id is ',termid
termid = getTerm(dbconn,tag)
tagids.extend(termid)
else:
try:
dbconn.ping()
except:
dbconn = MySQLdb.connect(host='localhost', user='root', passwd='123456', db='361way', port=3306, charset='utf8', init_command='set names utf8')
termid = addTerm(dbconn,tag)
print 'add tag',tag,'id is ' ,termid
tagids.append(termid)
print 'tag id is ',tagids
postid = '35'
tagids = list(set(tagids))
ctagdata = []
for tagid in tagids:
ctagdata.append((postid,tagid))
try:
dbconn.ping()
except:
dbconn = MySQLdb.connect(host='localhost', user='root', passwd='123456', db='361way', port=3306, charset='utf8', init_command='set names utf8')
addCTag(dbconn,ctagdata)
使用torndb的代码
#!/usr/bin/python
#coding=utf-8
import torndb
def getTerm(db,tag):
query = "SELECT term_id FROM wp_terms where name=%s "
rows = db.query(query,tag)
termid = []
for row in rows:
termid.extend(row.values())
return termid
def addTerm(db,tag):
query = "INSERT into wp_terms (name,slug,term_group) values (%s,%s,0)"
term_id = db.execute_lastrowid(query,tag,tag)
sql = "INSERT into wp_term_taxonomy (term_id,taxonomy,description) values (%s,'post_tag',%s) "
db.execute(sql,term_id,tag)
return term_id
def addCTag(db,data):
query = "INSERT INTO wp_term_relationships (object_id,term_taxonomy_id) VALUES (%s, %s) "
db.executemany(query,data)
dbconn = torndb.Connection('localhost:3306','361way',user='root',password='123456')
tags = ['mysql','1111','aaaa','bbbb','ccccc','php','abc','python','java']
tagids = []
for tag in tags:
termid = getTerm(dbconn,tag)
if termid:
print tag, 'tag id is ',termid
tagids.extend(termid)
else:
termid = addTerm(dbconn,tag)
print 'add tag',tag,'id is ' ,termid
tagids.append(termid)
print 'All tags id is ',tagids
postid = '35'
tagids = list(set(tagids))
ctagdata = []
for tagid in tagids:
ctagdata.append((postid,tagid))
addCTag(dbconn,ctagdata)
从两者的代码上来看,使用torndb模块和原生相比,发现可以省略如下两部分:
torndb模块不需要db.cursor进行处理,无不需要db.comment提交,torndb是自动提交的;
torndb不需要在每次调用时,进行db.ping()判断数据库socket连接是否断开,因为torndb增加了reconnect方法,支持自动重连。
2.torndb的方法
torndb提供的参数和方法有:
execute 执行语句不需要返回值的操作。
execute_lastrowid 执行后获得表id,一般用于插入后获取返回值。
executemany 可以执行批量插入。返回值为第一次请求的表id。
executemany_rowcount 批量执行。返回值为第一次请求的表id。
get 执行后获取一行数据,返回dict。
iter 执行查询后,返回迭代的字段和数据。
query 执行后获取多行数据,返回是List。
close 关闭
max_idle_time 最大连接时间
reconnect 关闭后再连接
使用示例:
mysql> CREATE TABLE `ceshi` (`id` int(1) NULL AUTO_INCREMENT ,`num` int(1) NULL ,PRIMARY KEY (`id`));
>>> import torndb
>>> db = torndb.Connection("127.0.0.1","数据库名","用户名", "密码", 24*3600) # 24*3600为超时时间
>>> get_id1 = db.execute_lastrowid("insert ceshi(num) values('1')")
>>> print get_id1
1
>>> args1 = [('2'),('3'),('4')]
>>> get1 = db.executemany("insert ceshi(num) values(%s)", args1)
>>> print get1
2
>>> rows = db.iter("select * from ceshi")
>>> for i in rows:
… print i
3.报错
在使用过程中可能遇到的错误:
File "/home/361way/database.py", line 145, in execute_lastrowid
self._execute(cursor, query, parameters)
File "/home/361way/database.py", line 207, in _execute
return cursor.execute(query, parameters)
File "/usr/lib/pymodules/python2.7/MySQLdb/cursors.py", line 159, in execute
query = query % db.literal(args)
TypeError: not enough arguments for format string
写上面的代码时,我刚开始还是试着使用MySQLdb模块的方式引用数据,结果发现报参数的错误 ,经查看代码发现 ,torndb在使用几个sql方法时较MySQLdb精简过了。具体各个方法的传参方法如下(注意参数个数):
close()
reconnect()
iter(query, *parameters, **kwparameters)
query(query, *parameters, **kwparameters)
get(query, *parameters, **kwparameters)
execute(query, *parameters, **kwparameters)
execute_lastrowid(query, *parameters, **kwparameters)
execute_rowcount(query, *parameters, **kwparameters)
executemany(query, parameters)
executemany_lastrowid(query, parameters)
executemany_rowcount(query, parameters)
update(query, *parameters, **kwparameters)
updatemany(query, parameters)
insert(query, *parameters, **kwparameters)
insertmany(query, parameters)
数据分析咨询请扫描二维码
若不方便扫码,搜微信号:CDAshujufenxi
你是否被统计学复杂的理论和晦涩的公式劝退过?别担心,“山有木兮:统计学极简入门(Python)” 将为你一一化解这些难题。课程 ...
2025-03-31在电商、零售、甚至内容付费业务中,你真的了解你的客户吗? 有些客户下了一两次单就消失了,有些人每个月都回购,有些人曾经是 ...
2025-03-31在数字化浪潮中,数据驱动决策已成为企业发展的核心竞争力,数据分析人才的需求持续飙升。世界经济论坛发布的《未来就业报告》, ...
2025-03-28你有没有遇到过这样的情况?流量进来了,转化率却不高,辛辛苦苦拉来的用户,最后大部分都悄无声息地离开了,这时候漏斗分析就非 ...
2025-03-27TensorFlow Datasets(TFDS)是一个用于下载、管理和预处理机器学习数据集的库。它提供了易于使用的API,允许用户从现有集合中 ...
2025-03-26"不谋全局者,不足谋一域。"在数据驱动的商业时代,战略级数据分析能力已成为职场核心竞争力。《CDA二级教材:商业策略数据分析 ...
2025-03-26当你在某宝刷到【猜你喜欢】时,当抖音精准推来你的梦中情猫时,当美团外卖弹窗刚好是你想吃的火锅店…… 恭喜你,你正在被用户 ...
2025-03-26当面试官问起随机森林时,他到底在考察什么? ""请解释随机森林的原理""——这是数据分析岗位面试中的经典问题。但你可能不知道 ...
2025-03-25在数字化浪潮席卷的当下,数据俨然成为企业的命脉,贯穿于业务运作的各个环节。从线上到线下,从平台的交易数据,到门店的运营 ...
2025-03-25在互联网和移动应用领域,DAU(日活跃用户数)是一个耳熟能详的指标。无论是产品经理、运营,还是数据分析师,DAU都是衡量产品 ...
2025-03-24ABtest做的好,产品优化效果差不了!可见ABtest在评估优化策略的效果方面地位还是很高的,那么如何在业务中应用ABtest? 结合企业 ...
2025-03-21在企业数据分析中,指标体系是至关重要的工具。不仅帮助企业统一数据标准、提升数据质量,还能为业务决策提供有力支持。本文将围 ...
2025-03-20解锁数据分析师高薪密码,CDA 脱产就业班助你逆袭! 在数字化浪潮中,数据驱动决策已成为企业发展的核心竞争力,数据分析人才的 ...
2025-03-19在 MySQL 数据库中,查询一张表但是不包含某个字段可以通过以下两种方法实现:使用 SELECT 子句以明确指定想要的字段,或者使 ...
2025-03-17在当今数字化时代,数据成为企业发展的关键驱动力,而用户画像作为数据分析的重要成果,改变了企业理解用户、开展业务的方式。无 ...
2025-03-172025年是智能体(AI Agent)的元年,大模型和智能体的发展比较迅猛。感觉年初的deepseek刚火没多久,这几天Manus又成为媒体头条 ...
2025-03-14以下的文章内容来源于柯家媛老师的专栏,如果您想阅读专栏《小白必备的数据思维课》,点击下方链接 https://edu.cda.cn/goods/sh ...
2025-03-13以下的文章内容来源于刘静老师的专栏,如果您想阅读专栏《10大业务分析模型突破业务瓶颈》,点击下方链接 https://edu.cda.cn/go ...
2025-03-12以下的文章内容来源于柯家媛老师的专栏,如果您想阅读专栏《小白必备的数据思维课》,点击下方链接 https://edu.cda.cn/goods/sh ...
2025-03-11随着数字化转型的加速,企业积累了海量数据,如何从这些数据中挖掘有价值的信息,成为企业提升竞争力的关键。CDA认证考试体系应 ...
2025-03-10