John Doe
John Doe

Reputation: 1

How to fix MySQL Server has gone away error in Python

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?

Upvotes: 0

Views: 1391

Answers (2)

Vikas Sharma
Vikas Sharma

Reputation: 21

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()

Upvotes: 0

luky
luky

Reputation: 2370

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).

Upvotes: 1

Related Questions