如何检查 MySQL 连接是否在 Python 中打开?

BAE*_*BAE 6 python mysql mysql-python

我正在使用 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
Run Code Online (Sandbox Code Playgroud)

但是当我运行“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
...
Run Code Online (Sandbox Code Playgroud)

更新

我又测试了。输出如下。每次睡眠时间为 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.
Run Code Online (Sandbox Code Playgroud)

Sha*_*per 6

您可以使用此库python-mysql 的is_connected方法检查连接:

cnx = connector.connect(user="user",password="pass",host="host",database="database")

if (cnx.is_connected()):
    print("Connected")
else:
    print("Not connected")
Run Code Online (Sandbox Code Playgroud)


Cra*_*akC 4

尝试这个-

import MySQLdb

def main():
  # Connect to the MySQL database
  db = MySQLdb.connect(host = 'z.cs.utexas.edu', user = 'userName', passwd = 'password', db = 'dbName')

  # Check if connection was successful
  if (db):
    # Carry out normal procedure
    print "Connection successful"
  else:
    # Terminate
    print "Connection unsuccessful"
Run Code Online (Sandbox Code Playgroud)

  • 这对于检查“MysqlConnection”对象是否已实例化或是否已建立连接非常有用。它*不*验证连接是否**仍然有效** (3认同)