问题描述
我正在使用MySQLdb( http://mysql-python.sourceforge.net/).似乎connection.open和connection.sqlstate()对我不起作用.下面是代码:
I am using MySQLdb (http://mysql-python.sourceforge.net/). It seems that connection.open and connection.sqlstate() do not work for me. Below is the code:
def open(self):
#TODO: check the connection's status
# self.__conn.open OR self.__conn.sqlstate()
try:
print "sqlstate:"+str( self.__conn.sqlstate() )
print "open?"+str( self.__conn.open )
return "00000" == self.__conn.sqlstate()
except Exception as e:
print "Exception while checking MYSQL Connection:"+str(e)
return False
但是当我运行"sudo service mysql stop; sleep 60; sudo service mysql start;"时做测试.输出如下.似乎下面的输出永远重复了(我最终杀死了该过程).当服务器关闭时,connection.open为1,connection.sqlstate()为00000.但是,当服务器关闭时,connection.executemany()仍会引发异常.有任何想法吗?谢谢.
But when I ran "sudo service mysql stop; sleep 60; sudo service mysql start;" to do the testing. The output is as following. It seems that the following piece of output repeated for ever (I killed the process finally). When the server is down, connection.open is 1 and connection.sqlstate() is 00000. But when server is up, connection.executemany() still throw exceptions. Any ideas? Thanks.
...
2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
2015-10-20 14:09:06 sqlstate:00000
2015-10-20 14:09:06 open?1
2015-10-20 14:09:06 Reconnected to MYSQL.
2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
2015-10-20 14:09:06 sqlstate:00000
2015-10-20 14:09:06 open?1
2015-10-20 14:09:06 Reconnected to MYSQL.
2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
2015-10-20 14:09:06 sqlstate:00000
2015-10-20 14:09:06 open?1
2015-10-20 14:09:06 Reconnected to MYSQL.
2015-10-20 14:09:06 Exception while executing statement:(2006, 'MySQL server has gone away')
2015-10-20 14:09:06 sqlstate:00000
2015-10-20 14:09:06 open?1
...
更新
我再次测试.输出如下.每次睡眠是10秒.输出是OK,除了connection.open为1,即使服务器关闭也是如此.但是connection.sqlstate()是正确的(HY000).
I tested again. The output is as following. each sleep is 10 seconds. The output is OK except the connection.open is 1 even when server is down. But connection.sqlstate() is right (HY000).
2015-10-20 14:35:56 Exception while executing statement:(2006, 'MySQL server has gone away')
2015-10-20 14:35:56 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
2015-10-20 14:35:56 sqlstate:HY000
2015-10-20 14:35:56 open?1
2015-10-20 14:35:56 sleeping...
2015-10-20 14:36:06 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
2015-10-20 14:36:06 sqlstate:HY000
2015-10-20 14:36:06 open?1
2015-10-20 14:36:06 sleeping...
2015-10-20 14:36:16 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
2015-10-20 14:36:16 sqlstate:HY000
2015-10-20 14:36:16 open?1
2015-10-20 14:36:16 sleeping...
2015-10-20 14:36:26 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
2015-10-20 14:36:26 sqlstate:HY000
2015-10-20 14:36:26 open?1
2015-10-20 14:36:26 sleeping...
2015-10-20 14:36:36 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
2015-10-20 14:36:36 sqlstate:HY000
2015-10-20 14:36:36 open?1
2015-10-20 14:36:36 sleeping...
2015-10-20 14:36:46 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
2015-10-20 14:36:46 sqlstate:HY000
2015-10-20 14:36:46 open?1
2015-10-20 14:36:46 sleeping...
2015-10-20 14:36:56 Exception while ping:(2003, "Can't connect to MySQL server on '10.1.1.25' (111)")
2015-10-20 14:36:56 sqlstate:HY000
2015-10-20 14:36:56 open?1
2015-10-20 14:36:56 sleeping...
2015-10-20 14:37:06 sqlstate:00000
2015-10-20 14:37:06 open?1
2015-10-20 14:37:06 Reconnected to MYSQL.
推荐答案
一段时间以来,我一直在寻找该解决方案,但找不到合适的解决方案.
I have been searching for this solution for some time and I couldn't find an elegant solution.
似乎没有直接的方法可以做到这一点.如果尝试执行查询,您只会发现连接已关闭.
It seems there isn't a direct way of doing that. You will only find out your connection is closed if you try to execute a query.
我最终做的事情与此答案类似:如何在python中实时检查连接?
I end up doing something similar to this answer:How to check the connection alive in python?
这篇关于如何检查MySQL连接是否在Python中打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!