How to check if “'MySQL server has gone away'”

我与影子孤独终老i 提交于 2021-02-18 14:38:33

问题


    cursor.execute(sql)
  File "/usr/lib64/python2.6/site-packages/MySQLdb/cursors.py", line 173, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib64/python2.6/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
_mysql_exceptions.OperationalError: (2006, 'MySQL server has gone away')

I'm running an infinite loop, but every 30-60 minutes, I get the error above. I think I want to add a conditional at the start of each loop iteration to check if MySQL server connection is still connected and OK. If it has gone away, then I'd like to establish a new connection and cursor before continuing the next iteration.

How can I test if the server is here or not? And is this a proper solution?

Thank-you!


回答1:


You can catch the OperationalError exception and reconnect.

from MySQLdb import OperationalError

try:
    my_sql_operation()
except OperationalError as e:
    #do what you want to do on the error
    reconnect()
    print e

If you explicitly want to check if the connection is gone before running queries, you can run a test query to check if the exception occurs.

I am not sure in what other cases OperationalError is raised. But if you want to catch only the MySQL server has gone away error, you can do something like this.

from MySQLdb import OperationalError

try:
    my_sql_operation()
except OperationalError as e:
    if 'MySQL server has gone away' in str(e):
        #do what you want to do on the error
        reconnect()
        print e
    else:
        raise e()

it will catch only the 'gone away' error and let OperationalError exceptions raised due to other reasons be raised.

UPDATE

As I said in the comment my querying function would be like this:

def fetch_data(query):
    try:
        cursor = conn.Cursor()
        cursor.execute(query)
        return cursor.fetchall()
    except OperationalError as e:
        reconnect()
        print 'reconnecting and trying again...'
        fetch_data(query)

Thats just an example and may not be suitable for in your case. The point i am trying to make is its better to have different functions for different things so you can handle these situations easily.




回答2:


You can catch the error like shshank suggested, or use a library like torndb which will automatically reconnect.




回答3:


Cannot add this as a comment to Andrew Johnson, so I'll answer here instead.

Torndb does not always reconnect. Quite often you see the infamous "MySQL has gone away.." error.

The best way to deal with this is to make a function for all the queries and use that, indeed, like others suggested. Another way is hand out timed cookies to users, and if someone with a long enough time passed requests something from your website, go through a class that has:

def initialize(self):
    db.reconnect()

This way you reconnect the database, but this is not very scalable to big websites with many databases.



来源:https://stackoverflow.com/questions/24923226/how-to-check-if-mysql-server-has-gone-away

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!