2015-10-20 78 views
1

我正在使用MySQLdb(http://mysql-python.sourceforge.net/)。看來connection.open和connection.sqlstate()不適合我。下面是代碼:如何檢查MySQL連接是否在Python中打開?

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的服務mysql的停止;睡眠60; sudo的服務mysql的開始;」做測試。輸出如下。看起來,以後的一段輸出重複了(我終於殺死了這個過程)。當服務器關閉時,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 
... 

UPDATE

我再次測試。輸出如下。每次睡眠都是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. 

回答

0

嘗試這個 -

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" 
+0

你誤解了我的問題... – BAE

1

我一直在尋找這種解決方案一段時間,我無法找到一個完美的解決方案。

似乎沒有一個這樣做的直接方式。如果您嘗試執行查詢,則只會發現連接已關閉。

我最終會做類似這樣的回答了一句: How to check the connection alive in python?

0

你應該這樣的代碼: 在每前執行,平了MySQL服務器,例如

import MySQLdb as db 
    class DB(object): 
     def __init__(self): 
      try: 
       self.conn =mdb.connect(host='***',port=3306,user='',passwd='') 
      if (self.conn): 
       INFO_LOG("DB init success") 
      else: 
       INFO_LOG("DB init fail") 
      self.conn.autocommit(True) 
      self.conn.select_db(DB_NAME) 
      self.cursor = self.conn.cursor() 
     except Exception as e: 
      CRITICAL_LOG("DB init fail %s " % str(e)) 
    def insert(self,player_id,cmd): 
     try: 
      if self.conn is None: 
       self.__init__() 
      else: 
       self.conn.ping(True) 
      self.cursor.execute('INSERT INTO table values("%s",%s")' % 
      (player_id,cmd)) 
     except Exception as e: 
      import traceback 
      traceback.print_exc() 
      #error ocurs,rollback 
      self.conn.rollback() 
相關問題