2015-02-05 2 views
6

매일 다음과 같은 메시지가 나타납니다. 내 스크립트는 cron 작업을 통해 실행됩니다. 누구든지이 문제를 해결할 수 있습니까?MySQL 서버가 없어졌습니다 - 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') 

내 코드 :

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

이 답변을보십시오 : http://stackoverflow.com/a/982873/974317 –

+0

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

+0

자세한 내용은 최대 패킷 크기를 초과 할 때이 오류가 발생할 수 있습니다. [here] (http://serverfault.com/a/528183/341327) – PlasmaBinturong

답변

1

을 시도해보십시오 MySQL의 포트 번호를

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" 

됩니다 follwing을 3306

0

당신은 MySQL의 연결을 열 수 있지만 가까이하지 않는 경우에 발생 그것. MySQL의 측면에 타임 아웃이 있습니다.

당신이 할 수있는 한 가지 일은 사용을 마쳤을 때 연결을 닫는 것입니다. 또 다른이 ORM을 지원하는 ORM을 사용하는 것입니다 (SQLAlchemy는 제가 믿는 것입니다).

관련 문제