Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the default Mysql connection timeout when connecting through python?

I connected to a mysql database using python con = _mysql.connect('localhost', 'dell-pc', '', 'test') The program that I wrote takes a lot of time in full execution i.e. around 10 hours. Actually, I am trying to read distinct words from a corpus. After reading was finished there was a timeout error.

I checked Mysql default timeouts which were:

+----------------------------+----------+ | Variable_name              | Value    | +----------------------------+----------+ | connect_timeout            | 10       | | delayed_insert_timeout     | 300      | | innodb_lock_wait_timeout   | 50       | | innodb_rollback_on_timeout | OFF      | | interactive_timeout        | 28800    | | lock_wait_timeout          | 31536000 | | net_read_timeout           | 30       | | net_write_timeout          | 60       | | slave_net_timeout          | 3600     | | wait_timeout               | 28800    | +----------------------------+----------+ 

How can I change the default timeout ?

like image 680
Animesh Pandey Avatar asked Feb 06 '13 10:02

Animesh Pandey


People also ask

How do I keep MySQL connection alive?

To prevent these connections being automatically closed, the connector can be configured to keep the connection alive by submitting a simple SELECT statement (actually SELECT 'KEEP_ALIVE';) periodically to ensure that the MySQL timeout is not reached and the connection closed.

How do I change the connection timeout in MySQL workbench?

Can I adjust the timeout? Yes, go to Preferences, SQL Editor, and adjust the DBMS connection read time out option that defaults to 600 seconds. This sets the maximum amount of time (in seconds) that a query can take before MySQL Workbench disconnects from the MySQL server.


1 Answers

Do:

con.query('SET GLOBAL connect_timeout=28800') con.query('SET GLOBAL interactive_timeout=28800') con.query('SET GLOBAL wait_timeout=28800') 

Parameter meaning (taken from MySQL Workbench in Navigator: Instance > Options File > Tab "Networking" > Section "Timeout Settings")

  • connect_timeout: Number of seconds the mysqld server waits for a connect packet before responding with 'Bad handshake'
  • interactive_timeout Number of seconds the server waits for activity on an interactive connection before closing it
  • wait_timeout Number of seconds the server waits for activity on a connection before closing it

BTW: 28800 seconds are 8 hours, so for a 10 hour execution time these values should be actually higher.

like image 77
Ivelin Avatar answered Oct 06 '22 07:10

Ivelin