开发者社区> 问答> 正文

查询期间失去与MySQL服务器的连接

我有一个巨大的表,我需要处理其中的所有行。我总是收到此“丢失的连接”消息,并且无法重新连接并将光标恢复到原来的位置。这基本上是我在这里的代码:

import MySQLdb

class DB: conn = None

def connect(self): self.conn = MySQLdb.connect('hostname', 'user', '*****', 'some_table', cursorclass=MySQLdb.cursors.SSCursor)

def query(self, sql): try: cursor = self.conn.cursor() cursor.execute(sql) except (AttributeError, MySQLdb.OperationalError): self.connect() cursor = self.conn.cursor() cursor.execute(sql) return cursor

db = DB() sql = "SELECT bla FROM foo" data = db.query(sql)

for row in data: do_something(row)

但是我总是这样:

Traceback (most recent call last): File "teste.py", line 124, in run() File "teste.py", line 109, in run for row in data: File "/usr/lib64/python2.5/site-packages/MySQLdb/cursors.py", line 417, in next row = self.fetchone() File "/usr/lib64/python2.5/site-packages/MySQLdb/cursors.py", line 388, in fetchone r = self._fetch_row(1) File "/usr/lib64/python2.5/site-packages/MySQLdb/cursors.py", line 285, in _fetch_row return self._result.fetch_row(size, self._fetch_type) _mysql_exceptions.OperationalError: (2013, 'Lost connection to MySQL server during query') Exception _mysql_exceptions.OperationalError: (2013, 'Lost connection to MySQL server during query') in <bound method SSCursor. del of <MySQLdb.cursors.SSCursor object at 0x7f7e3c8da410>> ignored

你有什么主意吗?

展开
收起
保持可爱mmm 2020-05-10 22:27:54 656 0
1 条回答
写回答
取消 提交回答
  • mysql文档有专门针对此错误的整页内容:http : //dev.mysql.com/doc/refman/5.0/en/gone-away.html

    值得注意的是

    如果您向服务器发送不正确或太大的查询,您也会收到这些错误。如果mysqld收到的数据包太大或顺序混乱,则认为客户端出了点问题,并关闭了连接。如果需要大型查询(例如,如果使用大型BLOB列),则可以通过设置服务器的max_allowed_pa​​cket变量来增加查询限制,该变量的默认值为1MB。您可能还需要增加客户端上的最大数据包大小。有关设置数据包大小的更多信息,请参见第B.5.2.10节“数据包太大”。

    通过使用--log-warnings = 2选项启动mysqld,可以获得有关丢失的连接的更多信息。这会将一些断开连接的错误记录在hostname.err文件中来源:stack overflow

    2020-05-10 22:28:08
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
搭建电商项目架构连接MySQL 立即下载
搭建4层电商项目架构,实战连接MySQL 立即下载
PolarDB MySQL引擎重磅功能及产品能力盛大发布 立即下载

相关镜像