我正在使用MySQLdb(http://mysql-python.sourceforge.net/)。似乎connection.open和connection.sqlstate()对我不起作用。下面是代码:
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()仍然抛出异常。有什么想法吗?谢谢。
...
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秒。除了connection.open是1,即使服务器关闭,输出也正常。但是connection.sqlstate()是正确的(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.
你应该这样编码: 在每次执行之前,ping mysql服务器,例如
我已经寻找这个解决方案一段时间了,我找不到一个优雅的解决方案。
似乎没有直接的办法。只有在尝试执行查询时,您才会发现连接已关闭。
最后我做了类似于这个答案的事情: How to check the connection alive in python?
试试这个-
相关问题 更多 >
编程相关推荐