How to Set Connection Timeout in MySQL

Galaxy Glossary

How can I change the MySQL connection timeout?

Change server-side and client-side timeout variables so MySQL closes idle or stalled connections after a defined period.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

Why change the MySQL connection timeout?

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.

How do I check the current timeout values?

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.

How do I set 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;.

How do I change the client’s connect timeout?

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.

Can I edit my.cnf instead?

Yes. Add timeout keys under [mysqld], restart the server, and the values apply to every new connection.

What are safe values for production?

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.

How do I verify changes took effect?

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.

Best practices for ecommerce workloads

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.

Why How to Set Connection Timeout in MySQL is important

How to Set Connection Timeout in MySQL Example Usage


-- Reduce idle timeout during a flash sale
SET GLOBAL wait_timeout = 30;

-- Verify change
SHOW VARIABLES LIKE 'wait_timeout';

-- Run critical query
SELECT o.id, o.total_amount
FROM Orders o
JOIN Customers c ON c.id = o.customer_id
WHERE o.order_date >= CURRENT_DATE - INTERVAL 1 DAY;

How to Set Connection Timeout in MySQL Syntax


-- Check current timeouts
SHOW VARIABLES LIKE 'wait_timeout';
SHOW VARIABLES LIKE 'interactive_timeout';

-- Session-level change (only this session)
SET SESSION wait_timeout = 60;

-- Global change (persists until restart)
SET GLOBAL interactive_timeout = 300;

-- my.cnf permanent settings
[mysqld]
wait_timeout          = 120
interactive_timeout   = 120
net_read_timeout      = 30
net_write_timeout     = 60

-- Client CLI example
mysql --connect-timeout=10 -u admin -p ecommerce

-- Application DSN example (Node.js)
mysql://user:pass@db.prod?connectTimeout=10000

Common Mistakes

Frequently Asked Questions (FAQs)

Does lowering wait_timeout harm performance?

No. It frees resources by killing idle sessions. Just make sure your app reconnects gracefully.

What’s the difference between wait_timeout and interactive_timeout?

interactive_timeout applies to sessions flagged as interactive (e.g., CLI). All other sessions use wait_timeout.

Will changing timeouts restart my server?

SET GLOBAL takes effect instantly without a restart. Editing my.cnf requires a service restart to load new defaults.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.