2015-02-05 28 views
6

Ricevo tutti i giorni seguenti. I miei script stanno eseguendo lavori cron. Qualcuno può aiutare a risolvere questo problema?Il server MySQL è andato via - Python

File "/usr/lib64/python2.6/site-packages/MySQLdb/cursors.py", line 174, in execute 
    self.errorhandler(self, exc, value) 
    File "/usr/lib64/python2.6/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler 
    raise errorclass, errorvalue 
_mysql_exceptions.OperationalError: (2006, 'MySQL server has gone away') 

Il mio codice:

def get_id(test_mysql_conn,id): 
    cursor = test_mysql_conn.cursor() 
    cursor.execute("""select id from test where id = %s """, (id)) 
    row = cursor.fetchone() 
    if row is not None: 
     return row[0] 
    return 0 
+1

Dai un'occhiata a questa risposta: http://stackoverflow.com/a/982873/974317 –

+0

verifica [questo] (http://dev.mysql.com/doc/refman/5.0/en/gone-away.html). –

+0

Per vostra informazione, questo errore può verificarsi quando si supera la dimensione massima del pacchetto: vedere [qui] (http://serverfault.com/a/528183/341327) – PlasmaBinturong

risposta

1

Prova il follwing

if (os.getenv('SERVER_SOFTWARE') and os.getenv('SERVER_SOFTWARE').startswith('Google App Engine/')): 
      db = MySQLdb.connect(unix_socket = UNIX_SOCKET + INSTANCE_NAME, host =" HOST/IP", db = "DB_Name", user = "User_Name") //if your mysql is on google server 
     else: 
      db = MySQLdb.connect(host = "HOST/IP", port = "Port_number", db = "DB_name", user = "User_Name", passwd = "password") 

    cursor = db.cursor() 
    cursor.connection.autocommit(True) 

except Exception, err: 
    logging.info("Error In DataBase Connection : " + traceback.format_exc()) 
    return 'DataBaseProblem'   
try: 
    sql = query+str(req_args) 
    logging.info("QUERY = "+str(sql)) 
    cursor.execute(sql) 
    procedureResult = cursor.fetchall(); 
    if str(procedureResult) == '()': 
     logging.info("Procedure Returned 0 Record") 
     procedureResult = 'DataBaseProblem' 


    #logging.info("procedureResult : " + str(procedureResult)) 
except Exception, err: 
    #trackBack = str (traceback.format_exc()) 
    #raise Exception('DataBaseProblem',trackBack) 
    procedureResult="DataBaseProblem" 

per numero di porta è mysql 3306

0

Questo accade quando si apre una connessione MySQL, ma non chiudere esso. C'è un timeout sul lato di MySQL delle cose.

Una cosa che puoi fare è chiudere la connessione quando hai finito di usarla. Un altro sarebbe utilizzare un ORM che supporti questo o il pool integrato (SQLAlchemy è uno credo).