About the "MySQL server has gone away" issue

Source: Internet
Author: User
Tags redis

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.