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