热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

这篇文章主要介绍了Python中MySQLdb和torndb模块对MySQL的断连问题处理,torndb使用起来相对更加简洁一些,需要的朋友可以参考下

在使用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 
  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)


推荐阅读
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的详细步骤
    本文详细介绍了搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的步骤,包括环境说明、相关软件下载的地址以及所需的插件下载地址。 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 本文由编程笔记小编整理,介绍了PHP中的MySQL函数库及其常用函数,包括mysql_connect、mysql_error、mysql_select_db、mysql_query、mysql_affected_row、mysql_close等。希望对读者有一定的参考价值。 ... [详细]
  • Oracle分析函数first_value()和last_value()的用法及原理
    本文介绍了Oracle分析函数first_value()和last_value()的用法和原理,以及在查询销售记录日期和部门中的应用。通过示例和解释,详细说明了first_value()和last_value()的功能和不同之处。同时,对于last_value()的结果出现不一样的情况进行了解释,并提供了理解last_value()默认统计范围的方法。该文对于使用Oracle分析函数的开发人员和数据库管理员具有参考价值。 ... [详细]
  • 本文介绍了RPC框架Thrift的安装环境变量配置与第一个实例,讲解了RPC的概念以及如何解决跨语言、c++客户端、web服务端、远程调用等需求。Thrift开发方便上手快,性能和稳定性也不错,适合初学者学习和使用。 ... [详细]
  • 一、Hadoop来历Hadoop的思想来源于Google在做搜索引擎的时候出现一个很大的问题就是这么多网页我如何才能以最快的速度来搜索到,由于这个问题Google发明 ... [详细]
  • 本文详细介绍了SQL日志收缩的方法,包括截断日志和删除不需要的旧日志记录。通过备份日志和使用DBCC SHRINKFILE命令可以实现日志的收缩。同时,还介绍了截断日志的原理和注意事项,包括不能截断事务日志的活动部分和MinLSN的确定方法。通过本文的方法,可以有效减小逻辑日志的大小,提高数据库的性能。 ... [详细]
  • 推荐一个ASP的内容管理框架(ASP Nuke)的优势和适用场景
    本文推荐了一个ASP的内容管理框架ASP Nuke,并介绍了其主要功能和特点。ASP Nuke支持文章新闻管理、投票、论坛等主要内容,并可以自定义模块。最新版本为0.8,虽然目前仍处于Alpha状态,但作者表示会继续更新完善。文章还分析了使用ASP的原因,包括ASP相对较小、易于部署和较简单等优势,适用于建立门户、网站的组织和小公司等场景。 ... [详细]
  • 本文介绍了如何在MySQL中将零值替换为先前的非零值的方法,包括使用内联查询和更新查询。同时还提供了选择正确值的方法。 ... [详细]
  • mysql-cluster集群sql节点高可用keepalived的故障处理过程
    本文描述了mysql-cluster集群sql节点高可用keepalived的故障处理过程,包括故障发生时间、故障描述、故障分析等内容。根据keepalived的日志分析,发现bogus VRRP packet received on eth0 !!!等错误信息,进而导致vip地址失效,使得mysql-cluster的api无法访问。针对这个问题,本文提供了相应的解决方案。 ... [详细]
  • 众筹商城与传统商城的区别及php众筹网站的程序源码
    本文介绍了众筹商城与传统商城的区别,包括所售产品和玩法不同以及运营方式不同。同时还提到了php众筹网站的程序源码和方维众筹的安装和环境问题。 ... [详细]
  • ubuntu用sqoop将数据从hive导入mysql时,命令: ... [详细]
author-avatar
xiashi
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有