How to Set Connection Timeout in MariaDB

Galaxy Glossary

How do I change the connection timeout in MariaDB?

Adjust the time the server or client waits before dropping an idle or stalled MariaDB connection.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Description

What does a MariaDB connection timeout control?

Timeout variables decide how long MariaDB waits for a client to authenticate (connect_timeout) or remain idle (wait_timeout, interactive_timeout) before closing the connection to free resources.

How do I check current timeout settings?

Run SHOW VARIABLES LIKE 'wait\_timeout'; or query INFORMATION_SCHEMA.GLOBAL_VARIABLES. This instantly shows whether defaults—28800 seconds for wait_timeout and 10 seconds for connect_timeout—are still active.

How do I change the timeout for my current session?

Set it once per connection: SET SESSION wait_timeout = 60;. The new value applies only to your session and lasts until disconnect.

How do I set a global timeout for every connection?

With SUPER or SYSTEM_VARIABLES_ADMIN, execute SET GLOBAL wait_timeout = 120;. Changes take effect immediately for new sessions but vanish after a restart unless placed in my.cnf.

How do I persist settings in my.cnf?

Add lines under [mysqld]:
wait_timeout = 120
connect_timeout = 15
. Restart the server to apply.

How can clients specify a timeout?

CLI users pass --connect-timeout=15:
mysql --connect-timeout=15 -u app_user -p ecommerce. Libraries expose the same option through connection strings or driver parameters.

What is a safe value in production?

Busy APIs often set wait_timeout to 60-300 s to stop connection leakage. Long-running BI tools may raise interactive_timeout to 3600 s. Test under load before finalizing.

How to verify changes instantly?

After altering a variable, run SHOW SESSION VARIABLES LIKE 'wait\_timeout'; or reconnect and inspect again to confirm the server accepted the new setting.

Why How to Set Connection Timeout in MariaDB is important

How to Set Connection Timeout in MariaDB Example Usage


-- Limit idle time for a long report run
SET SESSION wait_timeout = 60;

-- Run report on high-value orders
SELECT id, customer_id, total_amount
FROM Orders
WHERE total_amount > 1000
ORDER BY order_date DESC
LIMIT 5;

How to Set Connection Timeout in MariaDB Syntax


-- View current values
SHOW VARIABLES LIKE 'wait_timeout';
SHOW VARIABLES LIKE 'interactive_timeout';
SHOW VARIABLES LIKE 'connect_timeout';

-- Adjust for current session (no restart needed)
SET SESSION wait_timeout = <seconds>;
SET SESSION interactive_timeout = <seconds>;

-- Adjust globally (requires SUPER or SYSTEM_VARIABLES_ADMIN)
SET GLOBAL wait_timeout = <seconds>;
SET GLOBAL connect_timeout = <seconds>;

-- Persist across restarts in my.cnf
[mysqld]
wait_timeout = <seconds>
connect_timeout = <seconds>

-- Client-side override
mysql --connect-timeout=<seconds> -u <user> -p <database>

Common Mistakes

Frequently Asked Questions (FAQs)

Is it safe to set wait_timeout very low?

Yes, if your application uses a pool and reconnect logic. Values under 30 s can starve long-running statements, so monitor errors before deploying.

Does changing connect_timeout drop existing users?

No. connect_timeout only affects new handshakes. Existing sessions remain unaffected.

Do I need SUPER to alter my session timeout?

No. Any authenticated user can run SET SESSION wait_timeout, but GLOBAL changes need higher privileges.

Want to learn about other SQL terms?