0

I am running a live detection python script. So the python script runs 24/7 without closing but after few hours, I get MySQL has hone away error

This is my code to make connection

def connectToMySQL(config_str):
    return mysql.connector.connect(
        host=config_str[1].strip('\n').strip('\r\n'),
        user=config_str[2].strip('\n').strip('\r\n'),
        passwd=config_str[3].strip('\n').strip('\r\n'),
        database=config_str[4].strip('\n').strip('\r\n')
    )
mydb = connectToMySQL(config_str)

mycursor = mydb.cursor()

How can i set the timeout limit to infinity?

John Doe
  • 1
  • 4
  • Possible duplicate of [How to enable MySQL client auto re-connect with MySQLdb?](https://stackoverflow.com/questions/207981/how-to-enable-mysql-client-auto-re-connect-with-mysqldb) – Mike Scotty Jul 05 '19 at 14:09
  • Possible duplicate of [MySQL error 2006: mysql server has gone away](https://stackoverflow.com/questions/7942154/mysql-error-2006-mysql-server-has-gone-away) – shaik moeed Jul 05 '19 at 14:09
  • 1
    1.) It do not use mysql-connector 2.) It is not even related to python. – John Doe Jul 05 '19 at 14:30

2 Answers2

1

i had this problem while using connection in multi threaded enviroment. i fixed it by making connection again from the each thread (and closing it in it).

luky
  • 2,263
  • 3
  • 22
  • 40
0

Execute the below queries in MySQL.

SET @@GLOBAL.wait_timeout=31536000;
SET @@GLOBAL.interactive_timeout=31536000;

(for windows use 2147483 value)

Restart MySQL server.

Also, make sure to close the cursor and connection after using it.

mycursor.close()
mydb.close()
Vikas Sharma
  • 11
  • 1
  • 3