顿顿锅包又 2021-03-04 08:38 采纳率: 66.7%
浏览 639
已采纳

flask 使用pymysql 操作mysql 老断开链接报错 10054

使用flask写的项目 不定时的就回有这样的报错。然后mysql 服务就停止了,需要手动开启mysql服务

Traceback (most recent call last):
  File "D:\newflaskdome\venv\lib\site-packages\pymysql\connections.py", line 732, in _read_bytes
    data = self._rfile.read(num_bytes)
  File "C:\Users\huoda\AppData\Local\Programs\Python\Python39\lib\socket.py", line 704, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [WinError 10054] 远程主机强迫关闭了一个现有的连接。

During handling of the above exception, another exception occurred:
  • 写回答

1条回答 默认 最新

  • 多鱼的夏天 2021-03-04 13:23
    关注

    是因为mysql长链接超时断开,可以考虑采用检测到断开后尝试重连。

    可以参考下面的代码

    #! /usr/bin/python
    # coding=utf-8
    
    import pymysql as MySQL
    
    import time
    
    class mysql:
        def __init__(self,
                  host = '',
                  user = '',
                  passwd = '',
                  db = '',
                  port = 3306,
                  charset = 'utf8'):
            self.host = host
            self.host   = host
            self.user   = user
            self.passwd = passwd
            self.db     = db
            self.port   = port
            self.charset= charset
            self.conn   = None
            self._conn()
    
        def _conn(self):
            try:
                self.conn = MySQL.Connection(host = self.host, user = self.user, passwd = self.passwd, db = self.db, port = self.port , charset = self.charset)
                print("数据库连接成功")
                return True
            except Exception as e:
                print("数据库连接失败:"+str(e))
                return False
    
        #通过ping()实现数据库的长连接
        def _reConn(self,num = 28800,stime = 3):
            _number = 0
            _status = True
            while _status and _number <= num:
                try:
                    #ping校验连接是否异常
                    self.conn.ping()
                    _status = False
                except:
                    print("数据库断开连接,重连")
                    if self._conn()==True: 
                        _status = False
                        break
                    _number +=1
                    time.sleep(stime)      
    
                  
        def querry(self,sql):
            try:
                self._reConn()
                self.cursor = self.conn.cursor()
                self.cursor.execute(sql)
                results = self.cursor.fetchall()
                self.cursor.close()
                return results;
            except Exception as e:
                print("querry异常:"+str(e))
                return None;
    
        def insert(self,sql,param):
            try:
                self._reConn()
                self.cursor = self.conn.cursor()
                res = self.cursor.execute(sql,param);
                self.conn.commit();
                self.cursor.close()
                return res;
            except Exception as e:
                self.conn.rollback();
                print("insert异常:"+str(e))
                return False;
    
        def close(self):
            self._reConn()
            self.conn.close();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 android报错 brut.common.BrutException: could not exec (exit code = 1)
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?