Python操作数据库出现链接超时断开问题 pymysql.err.OperationalError
问题:Python操作数据库出现链接超时断开问题:pymysql.err.OperationalError: (2006, "MySQL server has gone away (ConnectionAbortedError(10053, '你的主机中的软件中止了一个已建立的连接。', None, 10053, None))")原因:数据库默认2分钟(interactive_timeout=12
·
问题:
Python操作数据库出现链接超时断开问题:
pymysql.err.OperationalError: (2006, "MySQL server has gone away (ConnectionAbortedError(10053, '你的主机中的软件中止了一个已建立的连接。', None, 10053, None))")
原因:
数据库默认2分钟(interactive_timeout=120)不从数据库取数据则断开连接。
方案一:
# 查看数据库默认的时间
show global variables like '%timeout%';
# 可以将interactive_timeout设置大一些。如:
mysql> set global interactive_timeout=86400;
方案二(推荐):
检查连接是否断开,如果断开就进行重连
try:
self.conn.ping(reconnect=True) # 检查连接是否断开,如果断开就进行重连
self.cursor.execute(sql)
self.conn.commit()
except Exception as e:
self.conn.rollback()
print("mysql执行失败:{}".format(e))
finally:
self.conn.close()
更多推荐
已为社区贡献1条内容
所有评论(0)