pymysql错误:

pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query')
pymysql.err.InterfaceError: (0, '')

错误原因:

MySQL持久化链接保持时间为8小时(28800秒),过期后断开连。如果数据库没有新建连接,则会报此错。

mysql> SHOW SESSION VARIABLES LIKE 'wait_timeout';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wait_timeout | 28800 |
+---------------+-------+

解决思路:

调用前判断连接是否有效,如果有效继续,无效创建连接。

class DataBase():
"""数据库类""" def __init__(self, host='localhost', user='root', pw='password', db='test'):
self.con = pymysql.connect(host, user, pw, db)
self.cur = self.con.cursor() # 方法1:
def _reCon (self):
""" MySQLdb.OperationalError异常"""
# self.con.close()
while True:
try:
self.con.ping()
break
except OperationalError:
self.con.ping(True) # 方法2:
def _reConn (self,num=28800,stime=3):
"""
校验数据库连接是否异常
num:8小时
stime:间隔3秒重连
"""
_number = 0
_status = True
while _status and _number <= num:
try:
self.con.ping() #cping 校验连接是否异常
_status = False
except:
if self.con == True: #重新连接,成功退出
_status = False
break
_number +=1
time.sleep(stime) #连接不成功,休眠3秒钟,继续循环,直到循环8小时 def insert_visitor_info(self, time, device_id, room_id):
"""插入访客请求信息"""
self._reCon()
# self._reConn()
with self.con:
sql = "INSERT INTO visitor(time,device_id,room_id) VALUES(%s,%s,%s)"
self.cur.execute(sql, (time, device_id, room_id))
if __name__ == '__main__': db = DataBase()
db.insert_visitor_info('2019-03-31 17:57:08','8b417cb51268','312')

鸣谢地址:

pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query')的更多相关文章

  1. 解决python pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query')

    解决python pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query') 学习了:ht ...

  2. _mysql_exceptions.OperationalError: (2013, 'Lost connection to MySQL server during query')

    最近写了一个定时脚本,每天凌晨跑,每次跑时间很长. 在测试这个脚本的时候,跑了一个小时,发生一个错误,脚本中断,错误如下: _mysql_exceptions.OperationalError: (2 ...

  3. Error NO.2013 Lost connection to Mysql server during query

    系统:[root@hank-yoon ~]# cat /etc/redhat-release CentOS release 6.3 (Final) DB版本:mysql> select @@ve ...

  4. Procedure execution failed 2013 - Lost connection to MySQL server during query

    1 错误描述 Procedure execution failed 2013 - Lost connection to MySQL server during query 2 错误原因 由错误描述可知 ...

  5. mysqldump导出报错"mysqldump: Error 2013: Lost connection to MySQL server during query when dumping table `file_storage` at row: 29"

    今天mysql备份的crontab自动运行的时候,出现了报警,报警内容如下 mysqldump: Error 2013: Lost connection to MySQL server during ...

  6. Mysql导入Sql文件时报Error Code: 2013 - Lost connection to MySQL server during query

    MySql 有时我们导入sql文件,文件过大,导致Error Code: 2013 - Lost connection to MySQL server during query这种错误 执行以下: S ...

  7. navicat mysql报错误:2013 Lost connection to MySQL server during query

    好像是MySQL的navicat UI界面跟数据的连接问题,如果直接用命令导入数据的话,或许能规避这个问题.

  8. MySQL Workbench出现:Error Code: 2013. Lost connection to MySQL server during query的问题解决

    解决办法: [Edit]->[Preference]->[SQL Editor] 将下图DBMS connection read time out (in seconds)适当调大: 参考 ...

  9. 2013 lost connection to mysql server during query

    navicat 导入sql大脚本到mysql数据库报错 解决办法: 修改mysql.ini配置文件: max_allowed_packet=256M wait_timeout=5000

随机推荐

  1. 使用传统javaweb进行文件上传

    使用传统文件上传方式 1.配置依赖 <properties> <project.build.sourceEncoding>UTF-8</project.build.sou ...

  2. 05.Hibernate常用的接口和类---Configuration类和作用

    Configuration作用: 加载Hibernate配置文件,可以获取SessionFactory对象 加载方式: 1.加载配置文件 Configuration configuration = n ...

  3. Codeigniter 数据库操作事务情况下获取不到last_insert_id()

    开发中,数据库Insert使用了事务,如果 $this->db->insert_id() 放在 $this->db->trans_complete(); 这句语句之后,$thi ...

  4. tDQSS

    tDQSS - DQS latching rising transitions to associated clock edges, as described on Table 41/42 of JE ...

  5. HZOI20190829模拟33题解

    题面:https://www.cnblogs.com/Juve/articles/11436771.html A:春思 我们对a分解质因数,则$a=\prod\limits_p^{p|a}p^k$ 所 ...

  6. NOIP2016提高A组 B题 【HDU3072】【JZOJ4686】通讯

    题目描述 “这一切都是命运石之门的选择.” 试图研制时间机器的机关SERN截获了中二科学家伦太郎发往过去的一条短 信,并由此得知了伦太郎制作出了电话微波炉(仮). 为了掌握时间机器的技术,SERN总部 ...

  7. 洛谷p1008 三连击

    https://www.luogu.org/problemnew/show/P1008 题目描述 将1,2,3,4,5,6,7,8,9共9个数分成3组,分别组成3个三位数,且使这3个三位数的值构成1: ...

  8. springmvc 串口读写 基于win7使用txrx netbeans jdk1.8 maven的

    引入 <dependency> <groupId>org.rxtx</groupId> <artifactId>rxtx</artifactId& ...

  9. 深入学习:Windows下Git新手教程(下)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/huangyabin001/article/details/35840591 声明:因为本人对于Git ...

  10. 利用Python覆盖图像的某一部分,即改变图形一块区域(Region)的RGBA值

    原图如下: 改变过后的图如下: 查阅API写法如下: from PIL import Image from PIL import ImageDraw pilim = Image.open('1.jpg ...