How to Change Connection Timeout in MySQL


When working with MySQL, one important aspect to consider is the management of connection timeouts. A connection timeout is the duration a client waits for a response from the server before the connection is considered unsuccessful. In some situations, the default connection timeout settings may not be optimal, which can lead to performance issues or unnecessary resource consumption. Adjusting these settings can significantly improve the overall efficiency and reliability of your MySQL-driven applications.

In this guide, we'll provide a comprehensive overview of connection timeouts in MySQL and dive into the process of changing these settings via the MySQL command line and MySQL configuration files. We'll start with the basics - what are connection timeouts, what can affect them, and how to choose the appropriate one to meet your needs. After that, we'll show an actual example of how to change the connection timeout value in MySQL. In the end. we'll give you some tips on how to monitor and optimize the connection timeout values you choose and how to solve some common problems that may occur when working with connection timeouts in MySQL.

What are Connection Timeouts in MySQL

Connection timeout in MySQL refers to the time period during which a client attempts to establish a connection with the MySQL server. If the server does not respond within the specified time frame, the client assumes the connection attempt has failed. Connection timeouts are crucial in maintaining the overall stability and performance of a MySQL-driven application.

By default, MySQL has two main timeout settings:

  • wait_timeout - the number of seconds the server waits for activity on a non-interactive connection before closing it. The default value is 28800 seconds (8 hours).
  • interactive_timeout - the number of seconds the server waits for activity on an interactive connection (such as those initiated by the MySQL command-line client) before closing it. The default value is also 28800 seconds (8 hours).

Note: These default settings are designed to cater to a wide range of use cases, but they might not be optimal for every situation.

Connection timeouts can have a significant impact on the performance and resource utilization of your MySQL server. If the timeout values are set too low, clients may experience frequent disconnections, leading to a poor user experience and increased server load due to constant re-connections. On the other hand, if the timeout values are set too high, idle connections may consume server resources unnecessarily, potentially impacting the server's ability to handle new connections and requests.

Finding the right balance in connection timeout settings is key to optimizing your MySQL deployment for your specific use case.

Checking Current Timeout Settings

Before making any changes, it's a good idea to first check the current settings for wait_timeout and interactive_timeout. This way, you'll have a baseline to compare against when making adjustments. Here's how to do that.

To retrieve the current session-level settings, you can log into MySQL from the command line and run the following queries:

SHOW VARIABLES LIKE 'wait_timeout';
SHOW VARIABLES LIKE 'interactive_timeout';

To retrieve the current global settings, use these queries instead:

SHOW GLOBAL VARIABLES LIKE 'interactive_timeout';

These commands will output the current values for wait_timeout and interactive_timeout in seconds.

Note: The session-level settings apply only to your current session, while the global settings apply to all new connections to the MySQL server.

Remember, these settings can be different for each MySQL server and can also be changed by your hosting provider or server administrator. So it's a good practice to check these values before making any adjustments.

Changing Connection Timeout in MySQL via MySQL Command Line

Now that you know how to check the current timeout settings, let's move on to changing them. The first method we'll cover involves using the MySQL command line. We'll go through how to modify the timeout settings for the current session and new global connections.

For the Current Session

Changing the timeout for the current session is straightforward. After logging into MySQL, use the SET command to change wait_timeout and interactive_timeout:

SET session wait_timeout = YOUR_VALUE;
SET session interactive_timeout = YOUR_VALUE;

Replace YOUR_VALUE with the timeout value you desire, in seconds. For example, if you want to set the timeout to 5 minutes, you would use 300 as the value (since 5 minutes * 60 seconds/minute = 300 seconds).

Globally for New Connections

If you want to change the timeout for all new connections (i.e., globally), you'd use the SET GLOBAL command:

SET GLOBAL wait_timeout = YOUR_VALUE;
SET GLOBAL interactive_timeout = YOUR_VALUE;

Once again, remember that these changes won't survive a server restart. They are temporary and only last until the server is rebooted. This method can be helpful when you need to change the timeout settings quickly or when you don't have access to the MySQL configuration file.

However, if you want a more permanent solution that does survive server restarts, you'll want to update the MySQL configuration file directly, which we'll cover in the next section.

Note: Keep in mind that you might need the SUPER or SYSTEM_VARIABLES_ADMIN privilege to change these global variables. If you encounter any issues or permissions errors, make sure to check your MySQL user privileges.

Making Permanent Changes via MySQL Configuration File

While changing timeout settings via the MySQL command line is quick and convenient, these changes are temporary and won't persist after a server restart. To make permanent changes that will endure server reboots, you'll need to update the MySQL configuration file (my.cnf on Linux or macOS, and my.ini on Windows) directly and adjust the wait_timeout and interactive_timeout variables.

First of all, you need to locate the MySQL configuration file on your system. Open the file in a text editor with admin privileges.

Note: The MySQL configuration file is typically found in:

  • /etc/my.cnf on Linux,
  • /usr/local/mysql/my.cnf on macOS,
  • C:\ProgramData\MySQL\MySQL Server X.Y\my.ini on Windows

Then, search for the wait_timeout and interactive_timeout variables in the configuration file. If they are not present, add them under the [mysqld] section. Set the desired values in seconds, like this:

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

wait_timeout = 3600
interactive_timeout = 3600

In this example, both timeout values are set to 3600 seconds (1 hour). Obviously, you can adjust the values according to your needs.

After you make the changes, you can save the configuration file and restart the MySQL server for the new settings to take effect. The process of restarting the server varies depending on your operating system:

  • Linux: sudo service mysql restart or sudo systemctl restart mysqld
  • macOS: sudo /usr/local/mysql/support-files/mysql.server restart
  • Windows: Restart the MySQL service using the Services management console.

Note: This method requires access to the MySQL configuration file and permission to modify it, which you might not have in some shared hosting environments. If you can't change the configuration file directly, you might need to ask your hosting provider or server administrator to make these changes for you.

What to Consider When Choosing a Connection Timeout Value

Several factors can influence the ideal connection timeout settings in a MySQL deployment. Taking these factors into account will help you determine the optimal timeout values for your specific use case.

The first thing that you'd want to consider when choosing a value for connection timeout is network latency. It refers to the time it takes for data to travel from the client to the server and back. High latency can cause delays in establishing connections and slow response times from the server. If your MySQL deployment has to deal with high network latency, you might need to increase the connection timeout values to prevent premature disconnections.

The overall load and performance of your MySQL server can also play a crucial role in determining appropriate connection timeout settings. If your server is under heavy load or experiencing performance issues, it might take longer for clients to establish a connection, necessitating a higher connection timeout value. Conversely, if your server has sufficient resources and performs well, you can consider using lower connection timeout values to free up resources more quickly.

Another thing to consider is the performance of the client-side application or system. If clients take longer to process and send requests, you may need to increase the timeout values to accommodate the slower response times. This can be especially relevant for applications with complex queries or large datasets.

Finally, different applications have their own requirements when it comes to connection timeouts. For instance, an application with real-time data processing might require shorter connection timeouts to maintain a rapid flow of information, while a reporting tool running long analytical queries might benefit from longer timeout settings to give enough time to generate the reports.

How to Identify the Appropriate Connection Timeout Value

Choosing the right connection timeout value depends on a careful analysis of your MySQL deployment and the factors discussed in the previous section. Here are some steps to help you identify the appropriate connection timeout values for your specific use case:

Step 1: Assess Server Performance and Load

Monitor your MySQL server's performance metrics, such as CPU usage, memory usage, and query execution times. If you notice that your server is consistently under heavy load or struggling to handle the current number of connections, consider increasing the connection timeout values to allow for more time to process requests.

Step 2: Evaluate Network Conditions

Analyze the network latency between your clients and the MySQL server. High latency can result in slow connection establishment and response times. In such cases, increasing the connection timeout values may be necessary to prevent clients from experiencing frequent disconnections.

Step 3: Analyze Application Requirements

Consider the specific requirements of your application when determining the optimal connection timeout values. For example, if your application involves real-time data processing or needs low-latency communication, a lower timeout value might be more suitable. On the other hand, if your app runs long, complex queries or deals with large datasets, a higher timeout value may be better.

Step 4: Balance Performance and Resource Utilization

Striking the right balance between performance and resource utilization is important when configuring connection timeouts. Lower timeout values can help free up server resources more quickly but may lead to a higher frequency of disconnections and reconnections. Higher timeout values, on the other hand, can result in more stable connections but may consume server resources unnecessarily. Experiment with different timeout settings and monitor the impact on both performance and resource utilization to find the optimal balance for your MySQL deployment.

How to Ensure Optimal Performance and Resource Utilization

To achieve optimal performance and resource utilization, consider implementing additional best practices alongside adjusting connection timeouts:

  • Use connection pooling - Connection pooling helps manage connections efficiently, reduces the overhead of establishing new connections, and improves overall performance.
  • Optimize queries - Regularly review and optimize SQL queries to minimize their execution time and the resources they consume.
  • Use appropriate indexing - Implementing the right indexes can significantly improve query performance and reduce server load.
  • Monitor slow queries - Track slow queries using MySQL's slow query log to identify performance bottlenecks and address them accordingly.
  • Regularly update and maintain your MySQL server - Ensure that your MySQL server is up to date and properly maintained, with the latest security patches and performance enhancements.

Troubleshooting Common Errors

Even though changing the connection timeout settings in MySQL is typically a straightforward process, you might encounter some issues along the way. Here are a few common problems and their solutions:

Permission Errors

If you're trying to change the wait_timeout and interactive_timeout settings and you receive an error message about insufficient privileges, it means your MySQL user account does not have the necessary permissions to modify these settings. You need the SUPER or SYSTEM_VARIABLES_ADMIN privilege to change these global variables. In this case, you can either use a different account that has the necessary privileges or ask your server administrator to change these settings for you.

Changes Not Persisting After Server Restart

As we mentioned earlier, changes made via the MySQL command line do not persist after a server restart. If you need to make permanent changes, you'll have to modify the MySQL configuration file directly (my.cnf or my.ini).

Cannot Locate MySQL Configuration File

If you can't find the my.cnf or my.ini file, it's possible that it's located in a different directory, or it might not exist at all. You can try searching for the file using the find command on Linux or the search function on Windows. If the file doesn't exist, you can create a new one in the default location for your operating system.

Changes Not Taking Effect After Editing Configuration File

If you've edited the configuration file and your changes aren't taking effect, make sure you've added the wait_timeout and interactive_timeout settings under the [mysqld] section. Also, ensure that you've saved your changes and restarted the MySQL service.

Connection Errors in Your Applications

If you're experiencing connection errors in your applications after changing these settings, it's possible that the timeout is set too low and connections are being closed before they can finish their work. You might need to increase the timeout value or adjust your application to handle shorter connection times.


All-in-all, managing connection timeouts in MySQL helps with optimizing the performance and reliability of your MySQL-driven apps. By adjusting the timeout settings, you can find the right balance between maintaining stable connections and better utilizing the server’s resources.

In this guide, we provided a comprehensive overview of how to work with connection timeouts in MySQL. It covers checking the current timeout settings, changing them using the MySQL command line for both the current session and global connections, and making permanent changes through the MySQL configuration file.

Additionally, we emphasized the importance of considering factors such as network latency, server performance, application requirements, and balancing performance with resource utilization when choosing appropriate timeout values.

In the end, we provided you with solutions to several common problems you may face when working with connection timeouts in MySQL, so you can be prepared for all scenarios.

Last Updated: July 2nd, 2023
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

© 2013-2024 Stack Abuse. All rights reserved.