PyMysql 错误:数据包序列号错误 - 预期为 1 0

问题描述 投票:0回答:2

我在 stackoverflow 和另一个编码示例网站中看到了本主题讨论的内容。 但我找不到任何成熟的解决方案,有人修复这个错误吗?

错误信息:

ERROR:root:Can't connect to to MySQL server with error: (pymysql.err.InternalError) Packet sequence number wrong - got 1 expected 0
ERROR:root:Can't connect to to MySQL server with error: (pymysql.err.InternalError) Packet sequence number wrong - got 1 expected 0
ERROR:root:Can't connect to to MySQL server with error: (pymysql.err.InternalError) Packet sequence number wrong - got 1 expected 0
ERROR:root:Can't connect to to MySQL server with error: (pymysql.err.InternalError) Packet sequence number wrong - got 1 expected 0
ERROR:root:Can't connect to to MySQL server with error: (pymysql.err.InternalError) Packet sequence number wrong - got 1 expected 0
ERROR:root:Can't connect to to MySQL server with error: (pymysql.err.InternalError) Packet sequence number wrong - got 1 expected 0

连接设置如下:

   "mysql+pymysql://{u}:{p}@{s}/{d}?charset=utf8".format(
                u=creds['username'],
                p=creds['password'],
                s=creds['host'],
                d=creds['dbname']
            ),
            pool_recycle=3500,
            echo=False,
            pool_pre_ping=True,
            pool_size=2,
            max_overflow=5
        )
python mysql error-handling pymysql
2个回答
4
投票

按照这个链接我得到:

在使用pymysql配合python多线程时,一般我们会面临 问题:

它不能与所有线程共享主线程创建的连接 子线程。它将导致以下错误: pymysql.err.InternalError:数据包序列号错误 - 为 0 预期 1 如果我们让每个子线程都创建一个连接并且 当这个子线程结束时关闭它,这是可行的,但显然会导致 与 MySQL 建立连接的成本很高。

他们似乎开发了其他库来解决这个问题:PyMySQL 连接池(相同链接)


0
投票

1。安装 pymysql-pool

pip3 install pymysql-pool

2。在代码中导入

import pymysql

import pymysqlpool

3.在代码启动时配置它

config={'host':'localhost', 'user':'{dbuser}', 'password':'{dbpass}', 'database':'{dbname}', 'autocommit':True}


pool1 = pymysqlpool.ConnectionPool(size=2, maxsize=3, pre_create_num=2, name='pool1', **config)

4。对于每个查询

con1 = pool1.get_connection()

gcu = con1.cursor()

gcu.execute("SELECT ....")

myresult = gcu.fetchall()

con1.close()
    

5。每次提交时:

con1 = pool1.get_connection()

gcu = con1.cursor()

sql = "update ..."

gcu.execute(sql)

con1.commit()

con1.close()
© www.soinside.com 2019 - 2024. All rights reserved.