MySQL error 4031: Disconnected by the server for inactivity
The connection sat idle longer than wait_timeout (or interactive_timeout) and the server closed it. Newer servers tell the client why.
- Error number
4031- Name
ER_CLIENT_INTERACTION_TIMEOUT- SQLSTATE
HY000- Category
- Connection and client
What it means
Since MySQL 8.0.24 the server sends this error to a client whose idle connection it is closing, instead of just dropping it. The client sees it on its next command. It's the same situation older versions report as 2006 (server has gone away).
Common causes
- A connection left idle in a pool or a forgotten interactive session for longer than
wait_timeout(8 hours by default). - A low
wait_timeoutset by a hosting provider.
How to fix it
-
Configure the connection pool to retire or test idle connections before
wait_timeoutruns out. -
Raise the timeout if long idle periods are expected.
SET PERSIST wait_timeout = 28800;
Related errors
More connection and client errors
2002Can't connect through socket2003Can't connect to MySQL server on host2005Unknown MySQL server host2013Lost connection to MySQL server during query2055Lost connection to MySQL server at stage2026SSL connection error2059Authentication plugin cannot be loaded2061Authentication plugin reported error
The code and message text are from the MySQL 8.4 Error Message Reference. Browse the full MySQL error list or search every database error.
Work on MySQL in SQLly
A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for MySQL and the other databases you use.