Wrote a python program that uses blocking to get Redis queue information, which may take several days before new queue information is put into the queue. After acquiring the queue information, the captured queue information is written to the MySQL database through the MySQLdb module. If there is no information in the queue for a long time, such as more than 1 days, after 1 days, the queue information arrives at the Redis queue, and the new queue is get out, and then to the MySQL database, it will be reported "MySQL server has gone away", At first I thought it was MySQL problem, and later found not. In general this situation, there are some by setting "Wait_timeout", the Wait_timeout value is set very large, wait_timeout default is 10. But our online MySQL wait_timeout set to 86400 is already very large. See an analytic approach on stackoverflow.com, the sample code is as follows:
Import Mysqldbclass db:conn = None def connect (self): Self.conn = MySQLdb.connect () def query (self, SQL): try: cursor = Self.conn.cursor () cursor.execute (SQL) except (Attributeerror, mysqldb.operationalerror): SELF.C Onnect () cursor = Self.conn.cursor () cursor.execute (sql) return Cursordb = DB () sql = "SELECT * from foo" cur = Db.query (SQL) # Wait a long time for the Mysql connection to timeoutcur = Db.query (SQL) # still works
It probably means that after an exception, a select operation is performed again.
I also added a similar feature to the code, executing a SELECT statement before performing a real SQL operation before performing a real SQL operation.
This article from "Constant line Mowgli's blog" blog, reproduced please contact the author!
About the "MySQL server has gone away" issue