MySQL Timeout Variables Explained: wait_timeout vs interactive_timeout


2 views

In MySQL server configuration, both wait_timeout and interactive_timeout control connection durations, but they apply to different types of client connections:

-- Default values in my.cnf/my.ini
wait_timeout = 28800      -- 8 hours
interactive_timeout = 28800 -- 8 hours

wait_timeout applies to non-interactive connections (typically from applications), while interactive_timeout governs connections initiated from command-line clients or GUIs where users directly interact with MySQL.

Consider this scenario with PHP applications:

// Persistent connection example
$db = new PDO(
    'mysql:host=localhost;dbname=test',
    'user',
    'password',
    [PDO::ATTR_PERSISTENT => true]
);

This connection would be subject to wait_timeout, whereas a MySQL shell session would use interactive_timeout.

For web applications:

# Recommended settings for LAMP stack
[mysqld]
wait_timeout = 300        -- 5 minutes
interactive_timeout = 3600 -- 1 hour

Check current timeouts with:

SHOW VARIABLES LIKE '%timeout%';
SHOW PROCESSLIST;  -- View active connections

Common symptoms include "MySQL server has gone away" errors. Adjust timeouts based on your application's connection patterns and server resources.


In MySQL server configuration, both wait_timeout and interactive_timeout control how long a connection can remain idle before being terminated. While they serve similar purposes, their application differs based on connection type:

-- Default values (in seconds)
SHOW VARIABLES LIKE 'wait_timeout';
SHOW VARIABLES LIKE 'interactive_timeout';

wait_timeout applies to non-interactive connections (typically from applications), while interactive_timeout governs interactive clients (like MySQL command-line or GUI tools). Both are measured in seconds of idle time before termination.

Consider these scenarios:

-- Setting different values for each timeout
SET GLOBAL wait_timeout = 300;       -- 5 minutes for application connections
SET GLOBAL interactive_timeout = 600; -- 10 minutes for CLI sessions

Application connection pools might need shorter timeouts to free resources, while DBAs might prefer longer interactive sessions.

When configuring these values:

  • Higher values consume more server resources
  • Lower values may cause "MySQL server has gone away" errors
  • Changes require server restart or session-specific SET commands
-- Checking current session timeouts
SELECT @@session.wait_timeout, @@session.interactive_timeout;

The timeout behavior follows these rules:

  1. New connections inherit global timeout values
  2. Interactive clients use interactive_timeout
  3. Non-interactive clients use wait_timeout
  4. SET commands can override per-session

When debugging timeout problems:

-- Check active connections and their idle time
SHOW PROCESSLIST;
-- Verify timeout values for specific connections
SELECT * FROM performance_schema.session_variables 
WHERE VARIABLE_NAME IN ('wait_timeout', 'interactive_timeout');