问题描述
当我运行大型查询(查询返回许多行)时,出现Lost connection to MySQL server during query
错误,并且看不到我做错了什么.我使用来自mysql.com的新" mysql驱动程序(而不是旧" MySQLdb)和与MAMP捆绑在一起的mysql版本. Python 2.7.表未损坏,analyze table nrk2013b_tbl;
返回状态为ok.这是一个中断的示例:
When I run large queries (queries returning many rows), I get the Lost connection to MySQL server during query
error, and I cannot see what I do wrong. I use the "new" mysql driver from mysql.com (not the "old" MySQLdb), and the mysql version that is bundled with MAMP. Python 2.7. Table is not corrupted, analyze table nrk2013b_tbl;
returns status ok. Here's an example that breaks:
#!/usr/bin/python2.7
# coding: utf-8
import sys
import mysql.connector # version 2.0.1
connection = mysql.connector.connect(
unix_socket="/Applications/MAMP/tmp/mysql/mysql.sock",
user="dbUsernam",
passwd="dbUserPassword",
db="nrk",
charset = "utf8",
use_unicode = True)
cur = connection.cursor()
cur.execute("USE nrk;")
sql = """SELECT id FROM nrk2013b_tbl WHERE main_news_category = 'Sport'"""
cur.execute(sql)
rows = cur.fetchall()
print rows
sys.exit(0)
这导致我大部分时间都遇到错误:
This results in the error I get most of the time:
Traceback (most recent call last):
File "train_trainer_test.py", line 20, in <module>
remaining_rows = cur.fetchall()
File "/Library/Python/2.7/site-packages/mysql/connector/cursor.py", line 823, in fetchall
(rows, eof) = self._connection.get_rows()
File "/Library/Python/2.7/site-packages/mysql/connector/connection.py", line 669, in get_rows
rows = self._protocol.read_text_result(self._socket, count)
File "/Library/Python/2.7/site-packages/mysql/connector/protocol.py", line 309, in read_text_result
packet = sock.recv()
File "/Library/Python/2.7/site-packages/mysql/connector/network.py", line 226, in recv_plain
raise errors.InterfaceError(errno=2013)
mysql.connector.errors.InterfaceError: 2013: Lost connection to MySQL server during query
第20行是rows = cur.fetchall()
如果我将查询限制为导致更少的结果,则SELECT id FROM nrk2013b_tbl WHERE main_news_category = 'Sport' LIMIT 10
一切都很好.但是我确实想使用更大的结果集.对于某些临时问题的解决,我已经移动了限制,并将所需的数据分解为较小的批处理,但这一直是一个问题.
If I limit the query to result fewer result SELECT id FROM nrk2013b_tbl WHERE main_news_category = 'Sport' LIMIT 10
all is well. But I do want to work with larger result sets. For some ad-hoc problem solving I have moved the limit and broken down the data I wanted into smaller batches, but this keeps popping up as a problem.
为了考虑连接超时和max_allowed_packet等,我有以下my.cnf文件:File: /Applications/MAMP/conf/my.cnf
In order to take connect-timeout, and max_allowed_packet, etc into account, I have this my.cnf-file: File: /Applications/MAMP/conf/my.cnf
[mysqld]
max_allowed_packet = 64M
wait_timeout = 28800
interactive_timeout = 28800
connect-timeout=31536000
这似乎没有什么区别(我什至不确定mysql是否能识别这些设置).当我从终端机或Sequel Pro运行查询时,它运行良好.只有通过python mysql.connector我才能得到这些错误.
This does not seem to make any difference (I'm not even sure if mysql recognises these settings). When I run queries from the terminal or from Sequel Pro, it works fine. It is only through the python mysql.connector I get these errors.
有什么想法吗?
PS:我暂时放弃了这一点,改用PyMySQL代替了Oracle mysql.connector.通过更改为此,问题似乎消失了(我自己得出结论,问题出在oracle mysql连接器中).
PS: I've temporarily given this up, and changed to PyMySQL instead of of the Oracle mysql.connector. By changing to this, the problems seems to disappear (and I conclude for myself that the problem is in the oracle mysql connector).
import pymysql
conn = pymysql.connect(
unix_socket="/Applications/MAMP/tmp/mysql/mysql.sock",
user="dbUsernam",
passwd="dbUserPassword",
db="nrk",
charset = "utf8",
use_unicode = True)
conn.autocommit(True)
cur = conn.cursor()
推荐答案
我还不得不切换到PyMySQL.我正在运行pip 1.5.6,Python 2.7.8,并尝试了mysql-connector 2.0.1
I also had to switch to PyMySQL. I am running pip 1.5.6, Python 2.7.8, and tried mysql-connector 2.0.1
我能够从Sequel Pro中运行查询而没有任何问题,但是我的Python查询将仅返回一部分结果,但由于问题中所述的错误而失败.
I was able to run the query from within Sequel Pro with no problems, but my Python query would fail with the error described in the question after returning just a subset of results.
切换到PyMySQL,一切正常.
Switched to PyMySQL and things work as expected.
https://github.com/PyMySQL/PyMySQL
在virtualenv中:
In the virtualenv:
pip install pymysql
在代码中:
import pymysql
connection = pymysql.connect(user='x', passwd='x',
host='x',
database='x')
cursor = connection.cursor()
query = ("MYQUERY")
cursor.execute(query)
for item in cursor:
print item
绝对是mysql-connector-python中的错误.
Definitely a bug in mysql-connector-python.
这篇关于为什么mysql连接器断开(“查询期间到MySQL服务器的连接丢失"错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!