Change server-side and client-side timeout variables so MySQL closes idle or stalled connections after a defined period.
Long-lived or stalled connections eat server resources and can exhaust available threads. Tuning timeout variables keeps idle sessions from blocking new ecommerce queries and protects against slow network handshakes.
Run SHOW VARIABLES LIKE 'wait_timeout';
and SHOW VARIABLES LIKE 'interactive_timeout';
. The result set shows the number of seconds MySQL waits before killing an idle session.
wait_timeout
and interactive_timeout
?Use SET GLOBAL
for a permanent change until restart, or SET SESSION
for a single connection. Example: SET GLOBAL wait_timeout = 120;
.
Pass --connect-timeout
to the mysql
CLI or include connectTimeout
=SECONDS in your application’s DSN. This controls how long the client waits for a TCP handshake.
my.cnf
instead?Yes. Add timeout keys under [mysqld]
, restart the server, and the values apply to every new connection.
Common settings are 30–300 s for wait_timeout
and interactive_timeout
, 30 s for net_read_timeout
, and 60 s for net_write_timeout
. Start high in development, then lower gradually while monitoring idle connections.
Reconnect and rerun SHOW VARIABLES
. The new numbers should appear. Simulate an idle ecommerce session by sleeping > timeout seconds and confirming MySQL closes the connection.
Close pooled connections cleanly, set shorter timeouts on APIs that burst traffic, and align application keep-alive intervals with MySQL timeout values to avoid surprise disconnects.
No. It frees resources by killing idle sessions. Just make sure your app reconnects gracefully.
interactive_timeout
applies to sessions flagged as interactive (e.g., CLI). All other sessions use wait_timeout
.
SET GLOBAL
takes effect instantly without a restart. Editing my.cnf
requires a service restart to load new defaults.