GurbaniX
GurbaniX

Reputation: 317

PyMysql error : Packet Sequence Number Wrong - got 1 expected 0

I saw in the stackoverflow and another coding example sites which this topics discussed. But I cannot find any mature solution about it, is anyone fix this error ?

Error Message:

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

Connection settings is like below:

   "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
        )

Upvotes: 2

Views: 12168

Answers (3)

Paul Allsopp
Paul Allsopp

Reputation: 741

I am using SQLAlchemy which I think uses this library so tough if you don't want to use it. However, I realized I had this library already installed, so I wondered if there was one bundled with SA, as well as the one I installed. So, I removed the one I installed, and the errors stopped immediately.

Have no idea if that was the real cause but might be worth checking out. Yes, afterwards SA still functioned just fine.

Upvotes: 0

Eyni Kave
Eyni Kave

Reputation: 1530

1. Install pymysql-pool

pip3 install pymysql-pool

2. Import it on code

import pymysql

import pymysqlpool

3. Config it on code startup

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. On each query

con1 = pool1.get_connection()

gcu = con1.cursor()

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

myresult = gcu.fetchall()

con1.close()
    

5. On each commit:

con1 = pool1.get_connection()

gcu = con1.cursor()

sql = "update ..."

gcu.execute(sql)

con1.commit()

con1.close()

So, this code can work with no interrupt forever with the minimum system resources...

Upvotes: 2

Ewerton Xavier
Ewerton Xavier

Reputation: 151

Following this link I got:

While using pymysql with python multithreading, generally we will face the questions:

It can't share a connection created by main thread with all sub-threads. It will result in the following error: pymysql.err.InternalError: Packet sequence number wrong - got 0 expected 1 If we make every sub-thread to create a connection and close it when this sub-thread ends that's workable but obviously lead to high cost on establishing connections with MySQL.

It seems they have developed other library to solve this:PyMySQL Connection Pool (same link)

Upvotes: 6

Related Questions