我每天收到以下内容。我的脚本正在运行cron作业。任何人都可以帮忙解决这个问题吗?
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
答案 0 :(得分:1)
尝试下面的
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"
对于mysql端口号是3306
答案 1 :(得分:0)
当您打开MySQL连接但未关闭它时会发生这种情况。 MySQL的一面已经超时了。
您可以做的一件事是在完成使用后关闭连接。另一种方法是使用支持它的ORM或内置池(SQLAlchemy是我相信的。)