Why am I getting 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')

我向 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()

我将 post 来自 Mysql 的过程 table 在这里,这样你就可以弄清楚为什么这个问题发生在红色标记的用户而不是绿色标记的用户身上。

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

结构

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

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

如果是数据过大造成的,注意每一行结果都是自己的数据包。不是整个结果集都必须适合数据包。如果您有 11 行并且删除了 6 行但仍然出现错误,则导致问题的行仍然存在。

你可以:

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

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