python检测mysql是否开启,如何检查MySQL连接是否在Python中打开?

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

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

...

UPDATE

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.

I end up doing something similar to this answer:

How to check the connection alive in python?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值