Python中MySQLdb和torndb模块对MySQL的断连问题处理


Posted in Python onNovember 09, 2015

在使用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相关的变量

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)
Python 相关文章推荐
python实现将pvr格式转换成pvr.ccz的方法
Apr 28 Python
django实现同一个ip十分钟内只能注册一次的实例
Nov 03 Python
django定期执行任务(实例讲解)
Nov 03 Python
python使用xslt提取网页数据的方法
Feb 23 Python
python微信公众号开发简单流程
Mar 23 Python
python实现浪漫的烟花秀
Jan 30 Python
Flask模板引擎之Jinja2语法介绍
Jun 26 Python
Django框架创建mysql连接与使用示例
Jul 29 Python
python opencv 简单阈值算法的实现
Aug 04 Python
python读取文件指定行内容实例讲解
Mar 02 Python
Python自动化xpath实现自动抢票抢货
Sep 19 Python
Python环境配置实现pip加速过程解析
Nov 27 Python
使用Python对IP进行转换的一些操作技巧小结
Nov 09 #Python
Python实现模拟时钟代码推荐
Nov 08 #Python
用Python的Flask框架结合MySQL写一个内存监控程序
Nov 07 #Python
Python的Flask框架中SQLAlchemy使用时的乱码问题解决
Nov 07 #Python
举例讲解Linux系统下Python调用系统Shell的方法
Nov 07 #Python
使用Python导出Excel图表以及导出为图片的方法
Nov 07 #Python
Windows下为Python安装Matplotlib模块
Nov 06 #Python
You might like
PHP程序级守护进程的实现与优化的使用概述
2013/05/02 PHP
php发送post请求的三种方法
2014/02/11 PHP
递归删除一个节点以及该节点下的所有节点示例
2014/03/19 PHP
PHP模拟QQ登录的方法
2015/07/29 PHP
PHP基于socket实现的简单客户端和服务端通讯功能示例
2017/07/10 PHP
自己动手制作jquery插件之自动添加删除行功能介绍
2011/10/14 Javascript
在jquery中combobox多选的不兼容问题总结
2013/12/24 Javascript
Javascript基础教程之for循环
2015/01/18 Javascript
js实现网页右上角滑出会自动消失大幅广告的方法
2015/02/27 Javascript
javascript结合CSS实现苹果开关按钮特效
2015/04/07 Javascript
PageSwitch插件实现100种不同图片切换效果
2015/07/28 Javascript
js调用百度地图及调用百度地图的搜索功能
2015/09/07 Javascript
javascript数组遍历的方法实例分析
2016/09/13 Javascript
用js实现博客打赏功能
2016/10/24 Javascript
微信小程序 五星评分(包括半颗星评分)实例代码
2016/12/14 Javascript
AngularJS框架中的双向数据绑定机制详解【减少需要重复的开发代码量】
2017/01/19 Javascript
ES6中的箭头函数实例详解
2017/04/06 Javascript
Vue from-validate 表单验证的示例代码
2017/09/26 Javascript
微信小程序学习笔记之函数定义、页面渲染图文详解
2019/03/28 Javascript
Vue.js中该如何自己维护路由跳转记录
2019/05/19 Javascript
[02:14]完美“圣”典2016风云人物:xiao8专访
2016/12/01 DOTA
Python中使用gzip模块压缩文件的简单教程
2015/04/08 Python
python实现简单聊天应用 python群聊和点对点均实现
2017/09/14 Python
python中format()函数的简单使用教程
2018/03/14 Python
Python使用cx_Oracle模块操作Oracle数据库详解
2018/05/07 Python
使用keras实现densenet和Xception的模型融合
2020/05/23 Python
为2021年的第一场雪锦上添花:用matplotlib绘制雪花和雪景
2021/01/05 Python
CSS3教程(5):网页背景图片
2009/04/02 HTML / CSS
HTML5中原生的右键菜单创建方法
2016/06/28 HTML / CSS
德国柯吉澳趣味家居:Koziol
2017/08/24 全球购物
"引用"与多态的关系
2013/02/01 面试题
外语学院毕业生的自我鉴定
2013/11/28 职场文书
优秀员工演讲稿
2014/05/19 职场文书
公司授权委托书样本
2014/09/15 职场文书
人事任命书范本
2015/09/21 职场文书
熟背这些句子,让您的英语口语突飞猛进(135句)
2019/09/06 职场文书