为啥我得到 MySQLdb._exceptions.OperationalError: (2013, 'Lost connection to MySQL server during query')

Posted

技术标签:

【中文标题】为啥我得到 MySQLdb._exceptions.OperationalError: (2013, \'Lost connection to MySQL server during query\')【英文标题】:Why am I getting MySQLdb._exceptions.OperationalError: (2013, 'Lost connection to MySQL server during query')为什么我得到 MySQLdb._exceptions.OperationalError: (2013, 'Lost connection to MySQL server during query') 【发布时间】:2021-10-13 05:59:33 【问题描述】:

我向 mysqlsd 发出多个请求,但对于特定用户,我收到此错误

MySQLdb._exceptions.OperationalError: (2013, 'Lost connection to MySQL server during query')

在线出现此错误

cursor.execute('Select * from process WHERE email=%s ORDER BY timestamp DESC LIMIT 20', ("tommy345a@outlook.com",))

但是当我对不同的用户执行相同的查询时,没有问题。

cursor.execute('Select * from process WHERE email=%s ORDER BY timestamp DESC LIMIT 20', ("cafahxxxx@gmail.com",))

页面加载正确。

下面给出了关于 MySQL 的更多细节

#modules used
from flask_mysqldb import MySQL
import MySQLdb.cursors

#setup
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'myusername'
app.config['MYSQL_PASSWORD'] = 'mypassword'
app.config['MYSQL_DB'] = 'my_db'
mysql = MySQL(app)

#extract of requests made to db
@app.route('/myhome', methods=['GET', 'POST'])
def home_page():
email = "tommy345a@outlook.com"
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM mail WHERE email = %s', (email,))
completed = cursor.fetchone()
cursor.execute('SELECT sum(transaction) FROM transactions WHERE email=%s', (email,))
points = cursor.fetchone()
cursor.execute('Select * from process WHERE email=%s ORDER BY timestamp DESC LIMIT 20', (email,))
transactions = cursor.fetchall()

我将在此处发布来自 Mysql 的进程表,以便您找出问题发生在红色标记用户而不是绿色标记用户的原因。

另外,这可能是数据包大小的问题,但直到昨天我还没有遇到任何问题(用户 tommy 下有超过 11 个条目。我删除了 6 行,但仍然无法正常工作)。另外,如果您认为这是由于数据包大小造成的,请告诉我如何在不增加数据包大小的情况下解决它,因为我在共享网络上,并且托管服务提供商不允许我增加数据包大小限制。

结构

【问题讨论】:

【参考方案1】:

“服务器已消失”有多种潜在原因,其中只有一个是由于数据对于您允许的最大数据包大小而言太大。

https://dev.mysql.com/doc/refman/8.0/en/gone-away.html

如果是数据太大导致的,请记住每一行结果都是自己的数据包。它不是必须适合数据包的整个结果集。如果您有 11 行并删除了 6 行但仍然出现错误,那么导致问题的行仍然存在。

你可以:

删除过大的行。您可能想要更改表的列数据类型,以便给定行不能太大。你展示了一些数据的截图,但我不知道你使用什么数据类型。提示:使用SHOW CREATE TABLE process

max_allowed_packet 更改为会话变量,因为您无权更改全局变量。另请记住,客户端还必须更改其最大允许数据包以匹配。阅读https://dev.mysql.com/doc/refman/8.0/en/packet-too-large.html

【讨论】:

感谢您帮助我缩小问题范围。您能检查一下添加到问题中的新屏幕截图吗? 我看不出任何行可以大于 max_allowed_pa​​cket (除非该选项被故意设置为 very 低,但这不太可能)。所以连接丢失错误可能还有另一个原因。

以上是关于为啥我得到 MySQLdb._exceptions.OperationalError: (2013, 'Lost connection to MySQL server during query')的主要内容,如果未能解决你的问题,请参考以下文章

Error loading MySQLdb module: No module named 'MySQLdb'. django.core.exceptions。

FLASK python web:MySQLdb._exceptions.OperationalError:(2002,“无法通过套接字'/tmp/mysql.sock'连接到本(代码

django报错:django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module. Did you install

Django启动时找不到mysqlclient处理 django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.

解决报错:from err django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module. Did you ins

django的mysqldb的坑