Home >Database >Mysql Tutorial >How to Extend MySQL Connection Timeouts in Python?
How to Alter Default MySQL Connection Timeout from Python
In Python, the default timeout for MySQL connections can pose a challenge when working with longer-running programs, as it can lead to unexpected errors. MySQL offers multiple timeout variables that govern different aspects of connection behavior. To address this issue, you can modify the default timeouts to ensure that your program does not encounter premature disconnections.
Modifying the Default Timeout
To change the default MySQL timeout settings, you can use the SET GLOBAL statement through a Python connection. This allows you to specify a new value for the desired timeout variable. The following code snippet demonstrates how to alter three crucial timeouts:
con.query('SET GLOBAL connect_timeout=28800') con.query('SET GLOBAL interactive_timeout=28800') con.query('SET GLOBAL wait_timeout=28800')
Timeout Variable Meanings
In this example, the timeout values are set to 28800 seconds, which equates to 8 hours. However, since your program runs for 10 hours, you should consider increasing the specified values accordingly.
The above is the detailed content of How to Extend MySQL Connection Timeouts in Python?. For more information, please follow other related articles on the PHP Chinese website!