mysqldude
Error codes › 4031

MySQL & MariaDB error 4031: The client was disconnected by the server because of inactivity

Same number, two different errors. 4031 means one thing in MySQL and another in MariaDB. Check which server you are connected to.
MySQLThe client was disconnected by the server because of inactivity. See wait_timeout and interactive_timeout for configuring this behavior.
MariaDBReferenced trigger '…' for the given action time and event type does not exist
Example from our test runs: 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.

MySQLMariaDB
SymbolER_CLIENT_INTERACTION_TIMEOUTER_REFERENCED_TRG_DOES_NOT_EXIST
SQLSTATEHY000HY000
TypeServer errorServer error
Versionssince 8.0.26 activesince 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_timeout to 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 HikariCP maxLifetime and keepaliveTime, SQLAlchemy pool_pre_ping=True and pool_recycle).
  • Reconnect after long pauses in scripts.
  • Or raise the timeout for that client: SET SESSION wait_timeout = 28800; (server-wide in my.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.

  1. Connect with mysql -u root -p and create a scratch database:

    CREATE DATABASE demo;
    USE demo;
  2. 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

  1. 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

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.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
✓✓✓✓✓✓✓✓✓
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
MySQL 5.6✗ 20065.7✗ 20068.0✓ 40318.4✓ 40319✓ 4031
MariaDB 10.6✗ 200610.11✗ 200611.4✗ 200611.8✗ 2006
Use a longer wait_timeout for this client (or reconnect / validate pooled connections) fix
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-25 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9
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

Related errors

  • 2006 CR_SERVER_GONE_ERRORexplained
  • 2013 CR_SERVER_LOSTexplained