繁体   English   中英

Python mysql:查询期间失去与MySQL服务器的连接

[英]Python mysql: Lost connection to MySQL server during query

我在使用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

是否可以修复此错误? 我无权访问更改权限,也无权在服务器上安装MySQLdb。

您用于应用程序的用户没有此权限,或者通过root用户连接到服务器并登录mysql并运行此命令...或将所有权限授予该用户(不建议用于应用程序级数据库用户)使用

grant all on database.* to user@'%';
flush privileges

您不需要包括全局参数。

游标= self.db.cursor()
cursor.execute(“ SET max_allowed_pa​​cket = 1073741824”)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM