Connection reset by peer: Lost connection to MySQL server during query ([Errno 54]


I have a python application that is querying a MySQL Database (v 8.0.28) hosted on AWS RDS. The code in the application looks something like this:

import pymysql
from sqlalchemy import create_engine
user = 'usr1'
pwd = 'pwd1'
host =  ''
port = 3306
database = 'db1'
engine = create_engine("mysql+pymysql://{}:{}@{}/{}".format(user,pwd,host,database))

con = engine.connect()

query = '''
        select *
        from db1.tbl1

df = pd.read_sql(query, con)

The application triggers a query upon loading and query returns successful results. However, after a few minutes (2-3) of idle time, I get an error when it tries to query the DB again / establish a connection.

Error traceback:

File "/Applications/Anaconda/anaconda3/lib/python3.9/site-packages/pymysql/", line 692, in _read_packet
    packet_header = self._read_bytes(4)
  File "/Applications/Anaconda/anaconda3/lib/python3.9/site-packages/pymysql/", line 738, in _read_bytes
    raise err.OperationalError(
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2013, 'Lost connection to MySQL server during query ([Errno 54] Connection reset by peer)')
[SQL: SELECT AS users_id, users.fullname AS users_fullname, AS users_email, users.password AS users_password, AS users_name
FROM users 
WHERE = %(pk_1)s]
[parameters: {'pk_1': 1}]
(Background on this error at:

We're trying to isolate the issue to whether this in on the Application side or DB host side.

1 Antwort

Error traceback : 'Lost connection to MySQL server during query ([Errno 54] Connection reset by peer)'

Peer connection resets are sudden break in connections from the server (peer). Connection Resets could occur due to a number of reasons with problems originating either at the client application, DB server (RDS instance) or the network connectivity between them.

From the description provided, since the application throws the error after an idle time (2-3 minutes). It is likely the application is not gracefully terminating the older connection while one of the following scenarios being true:

  1. The MySQL timeout parameters of the RDS database is set lower than the Client connection timeout making RDS to terminate/reset the connection.

Timeout parameters in RDS MySQL -

  1. The ‘max_allowed_packet’ parameter of your RDS database value is too small or queries require more memory than you have allocated for mysqld. You must increase this value if you are using large BLOB columns or long strings.

max_allowed_packet parameter -

  1. Your network firewall, intermediatory proxy (RDS Proxy - IdleClientTimeout) or network load balancers (AWS ELB – IdleTimeout) has a timeout established that terminate “idle” TCP connections when there is no activity on them for a certain period of time.

IdleClientTimeout in RDS Proxy -

IdleTimeout in AWS EBS -

If the issue is DB induced, you may find more information in the RDS Error logs. Connection reset may also happen due to abrupt unavailability of the DB instance or an RDS network incident. You can check the NetworkTransmitThroughput and NetworkReceiveThroughput CloudWatch metrics for a sudden drop indicative of RDS network problems, and check RDS Events and Error logs indicative of a database reboot or crash.

However, based on the the described pattern of error occurrence, it appears the problem is either at the Intermediatory Network (firewall, proxy or load balancer) or at the Application level.

beantwortet vor 2 Jahren

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen