5

我目前正在运行一个脚本,使用 execute many 函数将值(元组列表)插入 MySQL 数据库。当我使用少量行(`1000)时,脚本运行良好。

当我使用大约 40,000 行时,我收到以下错误:

cursor.executemany( stmt, trans_frame)
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\IPython\core\interactiveshell.py", line 2538, in run_code
    exec code_obj in self.user_global_ns, self.user_ns
  File "<ipython-input-1-66b44e71cf5a>", line 1, in <module>
    cursor.executemany( stmt, trans_frame)
  File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 253, in executemany
    r = self._query('\n'.join([query[:p], ',\n'.join(q), query[e:]]))
  File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 346, in _query
    rowcount = self._do_query(q)
  File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 310, in _do_query
    db.query(q)
OperationalError: (2006, 'MySQL server has gone away')

有什么建议么?

4

2 回答 2

3

您可以尝试仅为一个会话max_allowed_packet设置参数:

sql ='SET SESSION max_allowed_packet=500M'
cursor.execute(sql)
sql = ...
args = ...
cursor.executemany(sql, args)

如果这可行,您可以保留代码原样,或更改您的 my.cnf 文件(知道这可以解决 executemany 问题)。

于 2012-11-08T15:37:42.520 回答
3
sql ='SET GLOBAL max_allowed_packet=500*1024*1024'
cursor.execute(sql)
于 2016-11-15T09:20:09.457 回答