MySQL & MariaDB error 4031: The client was disconnected by the server because of inactivity
The client was disconnected by the server because of inactivity. See wait_timeout and interactive_timeout for configuring this behavior. (8.0.46) The connection was idle for longer than wait_timeout (or interactive_timeout), so the server closed it. MySQL 8.0.24+ tells you this directly; older MySQL and MariaDB report the same event as 2006 or 2013.
| MySQL | MariaDB | |
|---|---|---|
| Symbol | ER_CLIENT_INTERACTION_TIMEOUT | ER_REFERENCED_TRG_DOES_NOT_EXIST |
| SQLSTATE | HY000 | HY000 |
| Type | Server error | Server error |
| Versions | since 8.0.26 active | since 10.2.44 active |
What it means
The server closes connections that do nothing for wait_timeout seconds (interactive_timeout for interactive clients like mysql). Since MySQL 8.0.24 the server sends a final message before closing, so the client can show 4031 instead of the vague “server has gone away”. The next statement on that connection fails.
Watch the number on MariaDB: MariaDB uses 4031 for a different error (a trigger reference), and reports an idle disconnect as 2006.
Why it happens
- Connection pools or long-running scripts keep a connection open but unused, longer than
wait_timeout. - Hosting providers often lower
wait_timeoutto 60–300 seconds. - A script does a slow non-database task (file processing, an HTTP call) between two queries.
How to fix it
- Configure the connection pool to validate connections before use and to retire them before
wait_timeout(for example HikariCPmaxLifetimeandkeepaliveTime, SQLAlchemypool_pre_ping=Trueandpool_recycle). - Reconnect after long pauses in scripts.
- Or raise the timeout for that client:
SET SESSION wait_timeout = 28800;(server-wide inmy.cnf, with care: idle connections hold memory).
Reproduce it yourself
These steps are the exact test our harness runs. Use a scratch server, never production.
Connect with
mysql -u root -pand create a scratch database:CREATE DATABASE demo; USE demo;Make the session time out quickly, then wait longer than that before the next statement.
SET SESSION wait_timeout = 2; DO SLEEP(0); -- now wait 4 seconds before running the next line SELECT 1;
You get: ERROR 4031 (HY000) at line 2: The client was disconnected by the server because of inactivity. See wait_timeout and interactive_timeout for configuring this behavior.(9.7.4)
Verified fixes
Use a longer wait_timeout for this client (or reconnect / validate pooled connections)
Give the session (or the pool's connections) a longer idle timeout, or reconnect/validate before use.
SET SESSION wait_timeout = 28800; -- 8 hours
Tested on
| Step | MySQL | MariaDB | |||||||
|---|---|---|---|---|---|---|---|---|---|
| 5.6 | 5.7 | 8.0 | 8.4 | 9 | 10.6 | 10.11 | 11.4 | 11.8 | |
| Leave a connection idle longer than wait_timeout, then send a query (MySQL 8.0.24+ says 4031; older servers and MariaDB say 2006) repro | ✗ 2006 | ✗ 2006 | ✓ 4031 | ✓ 4031 | ✓ 4031 | ✗ 2006 | ✗ 2006 | ✗ 2006 | ✗ 2006 |
| Use a longer wait_timeout for this client (or reconnect / validate pooled connections) fix | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
What the warnings and failures say (2)
- Leave a connection idle longer than wait_timeout, then send a query (MySQL 8.0.24+ says 4031; older servers and MariaDB say 2006) (MySQL 5.6, MySQL 5.7): error 2006: ERROR 2006 (HY000) at line 2: MySQL server has gone away
- Leave a connection idle longer than wait_timeout, then send a query (MySQL 8.0.24+ says 4031; older servers and MariaDB say 2006) (MariaDB 10.6, MariaDB 10.11, MariaDB 11.4, MariaDB 11.8): error 2006: ERROR 2006 (HY000) at line 2: Server has gone away