python MySQL : Lost connection to MySQL server during query

标签 python mysql

我在使用 MySQLdb 连接器的 python 中有脚本,但在服务器上未安装 MySQLdb,因此我需要更改连接器。我有示例类:

class Foo:

    def __init__(self, config):
        self.logger = logging.getLogger("Foo")
        self.db = MySQLdb.connect(host=config["host"],user=config["user"], passwd=config["passwd"], db=config["db"])

    def get_something(self):
        cursor=self.db.cursor()
        cursor.execute("Select ...")
        for row in cursor:
            self.logger.debug(row)
            yield(row)

f = Foo(config)
f.get_something()

当我使用 MySQLdb 连接器时,它可以正常工作。在这种情况下,python 读取所有记录并存储在内存中。 但是当我将连接器更改为 mysql.connector 脚本时会打印一些记录并引发错误:

mysql.connector.errors.InterfaceError: 2013: Lost connection to MySQL server during query

这是修改后的类:

class Foo:

    def __init__(self, config):
        self.logger = logging.getLogger("Foo")
        self.db = mysql.connector.connect(host=config["host"],user=config["user"], passwd=config["passwd"], db=config["db"])

    def get_something(self):
        cursor=self.db.cursor()
        cursor.execute("Select ...")
        for row in cursor:
            self.logger.debug(row)
            yield(row)

f = Foo(config)
f.get_something()

我尝试运行查询:

self.db.cursor().execute("SET GLOBAL max_allowed_packet=1073741824")

但是我有错误:

mysql.connector.errors.ProgrammingError: 1227 (42000): Access denied; you need (at least one of) the SUPER privilege(s) for this operation

是否可以修复此错误?我没有 root 权限来更改权限,也没有权限在服务器上安装 MySQLdb。

最佳答案

您不需要包含global参数。

cursor = self.db.cursor()
cursor.execute("SET max_allowed_packet=1073741824")

直到您关闭光标,最大允许数据包系统变量将是您分配的值。

关于 python MySQL : Lost connection to MySQL server during query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27486112/

相关文章:

python - 使用 Biopython Entrez 从 fasta 记录访问序列元素

python - 如何使用 itertools 计算具有重复元素的所有组合?

python - Arnold 自定义 AOV

python - 我需要获取数据帧最密集点的坐标(x,y)

mysql - Magento:带锁的自定义 MySQL 查询不起作用

mysql - 我的查询需要稍微具体一些

php - 结合 2 个 MySQL 查询

mysql - 删除多余记录

python - Django: "How often"我需要@transaction.atomic

mysql - 更新一个日期字段小于另一日期字段的字段