问题一、MySQL server has gone away

##### peewee
from peewee import *
from peewee import __exception_wrapper__ class RetryOperationalError(object): def execute_sql(self, sql, params=None, commit=True):
try:
cursor = super(RetryOperationalError, self).execute_sql(sql, params, commit)
except OperationalError:
if not self.is_closed():
self.close()
with __exception_wrapper__:
cursor = self.cursor()
cursor.execute(sql, params or ())
if commit and not self.in_transaction():
self.commit()
return cursor class RetryMySQLDatabase(RetryOperationalError, MySQLDatabase):
def sequence_exists(self, seq):
pass database = RetryMySQLDatabase(...)  # 创建连接对象 # 代码使用: 每次执行SQL前判断连接是否关闭
def post(self):
  if database.is_closed():
    database.connect()
  ... ##### pymysql
def get_conn():
  global conn
  global cur
  if conn is not None:
    try:
      conn.ping(True)
      return conn
    except Exception as e:
      logging.exception(e)
  try:
    conn = pymysql.Connect(host='主机名', user='用户名', password='密码', database='数据库', port=3306, charset='utf8mb4', )
    return conn
  except Exception as e:
    logging.exception(e) # 查询数据
querystr ="SELECT * FROM t_user"
get_conn()
cur.execute(querystr)
conn.commit() ### 原因
1、连接超时:当某个连接很久没有发起新的数据库操作,达到了MySQL的wait_timeout时,会被server端强行关闭,关闭之后再使用这个连接进行查询,则会出现报错信息,表现为第一天服务正常访问,第二天无法访问。
  执行语句:show global variables like '%timeout'; wait_timeout的值则为连接无操作xxx秒后断开连接
2、MySQL宕机:执行语句,show global status like 'uptime'; 查看MySQL的运行时长,uptime数值越大,MySQL的运行时间越长。 3、查询结果集过大:执行语句,show global variables like 'max_allowed_packet'; 查看结果集的最大大小限制,会伴随‘ Your SQL statement was too large.’的报错,需优化SQL语句,或修改此参数的值。set global max_allowed_packet=1024*1024*16;
4、还有一种情况是:SQL语句过长也可能报这个错误,不知道为啥~~~ ### 解决
1、延长数据库的连接时长,修改wait_timeout的值,不推荐。
2、代码当中修改:每次执行SQL语句前,先判断连接是否有效,无效则重新连接

问题二、pymysql.err.InterfaceError: (0, '')

# 原因一: mysql 连接自己给关闭导致的,对任何已经close的conn进行db相关操作,包括ping()都会爆出这个错误。 若出现了问题一的错误但未及时解决,仍然访问服务,会出现这个错误,因为使用了已经关闭的连接。
# 原因二: 数据库操作对象实例未注销,但是持有的数据库连接已经过期,导致后续数据库操作不能正常进行
# 原因三: 数据库连接代码在函数外
# 原因四: 连接MySQL之后没有关闭连接的操作,短时间内不会出问题,长时间保持这个连接会出现连接混乱 # 以下代码会直接抛出异常无法执行except代码段
try:
  db.ping(reconnect=True)
except:
  db = pymysql.connection(..)
findlly:
  cursor = db.cursor()
  cursor.execute(sql) # 转载
class DataSource(object):   def __init__(self):
    self.conn = self.to_connect()   def __del__(self):
    self.conn.close()   def to_connect(self):
    return pymysql.connections.Connection(params)   def is_connected(self):
    """Check if the server is alive"""
    try:
      self.conn.ping(reconnect=True)
      print("db is connecting")
    except:
      traceback.print_exc()
      self.conn = self.to_connect()
      print("db reconnect")

问题三、pymysql.err.OperationalError: (2013, Lost connection to MySQL server during query')

# 每次执行数据库操作前执行,待补充
def reConnect(self):
try:
db.ping()
except:
db = pymysql.connect(...)

MySQL server has gone away && Lost connection to MySQL server during query的更多相关文章

  1. MySQL查询过程中出现lost connection to mysql server during query 的解决办法

    window7 64位系统,MySQL5.7 问题:在使用shell进行数据表更新操作的过程,输入以下查询语句: ,; 被查询的表记录数达到500W条,在查询过程中出现如题目所示的问题,提示" ...

  2. MySQL报错解决方案:2013-Lost connection to MySQL server

    今天上课的时候,在搭建完MySQL测试环境中出现的问题,整理如下: 问题描述:搭建完MySQL,用远程连接工具(Navicat)连接时报错: 2013-Lost connection to MySQL ...

  3. mysql: Data source rejected establishment of connection, message from server: "Too many connections"

    http://www.oschina.net/question/558677_66703 com.mysql.jdbc.exceptions.MySQLNonTransientConnectionEx ...

  4. Pycharm连接Mysql失败. [08001] Could not create connection to database server.

    使用Pycharm连接MySQL时遇到如下问题,错误代码[08001] 查了很多资料归纳一下可能是如下几个原因 0.mysql.server没开 找到对应系统下的mysql.server 启动/重启命 ...

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

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

  6. 解决idea中mysql连接失败Could not create connection to database server. Attempted reconnect 3 times. Giving up.

    原因是少一个参数,设置时区的.  解决方法: 加一个参数: serverTimezone=UTC jdbc:mysql://localhost:3306/SshProject?useUnicode=t ...

  7. Connection to 天mysql failed. [08001] Could not create connection to database server. Attempted ,报错处理方法

    https://blog.csdn.net/myzh215219/article/details/90314345 点击图上的DRIVER,然后点击GO TO DRIVER,之后更改合适的驱动. 我的 ...

  8. MySQL远程连接丢失问题解决方法Lost connection to MySQL server at ‘reading initial communication packet’, system error: 0

    最近远程连接mysql总是提示 Lost connection 很明显这是连接初始化阶段就丢失了连接的错误 其实问题很简单,都是MySQL的配置文件默认没有为远程连接配置好,只需要更改下MySQL的配 ...

  9. 虚拟机中MySQL连接问题:Lost connection to MySQL server at 'reading initial communication packet, system error: 0 以及 host is not allowed to connect mysql

    环境:在VirtualBox中安装了Ubuntu虚拟机,网络使用了NAT模式,开启了端口转发. 局域网内其他计算机访问虚拟机中的MySQL Server出现两个问题: Lost connection ...

随机推荐

  1. JVM 配置常用参数和常用 GC 调优策略

    链接:https://juejin.im/post/5c94a123f265da610916081f   JVM 配置常用参数 堆参数 回收器参数 如上表所示,目前主要有串行.并行和并发三种,对于大内 ...

  2. python并发编程之IO模型 同步 异步 阻塞 非阻塞

    IO浅谈 首先 我们在谈及IO模型的时候,就必须要引入一个“操作系统”级别的调度者-系统内核(kernel),而阻塞非阻塞是跟进程/线程严密相关的,而进程/线程又是依赖于操作系统存在的,所以自然不能脱 ...

  3. STC单片机Flash做EEPROM的代码

    STC官方给出的建议: /***************************************************************Author:Liming*** * @brie ...

  4. 不一样的go语言-玩转语法之一

      这段时间为俗事所累,疲以应付,落下了不少想法,错过了更新的日子.这个专题开始之际,已经准备了不下十几个主题,而在写作的过程中,又有新想法与主题涌现出来.未来预计想写写的内容主要包括: 玩转语法系列 ...

  5. Rust 智能指针(二)

    1. Rc<T> 引用计数指针 Rc<T> 是引用计数指针,可以使用clone使得指针所指向的数据具有多个所有者. enum List { Cons(i32, Rc<Li ...

  6. Linux新装系统简单指南

    也许更好的阅读体验 换源 1. 备份原来的源 sudo cp /etc/apt/sources.list /etc/apt/sources_init.list 2.更换源 先用\(gedit\)打开文 ...

  7. GreenPlum 最佳实践

    数据模型 Greenplum数据库是一种shared nothing的分析型MPP数据库.这种模型与高度规范化的/事务型的SMP数据库有显著区别.Greenplum数据库使用非规范化的模式设计会工作得 ...

  8. ubuntu系统下防火墙简单使用

    apt-get install ufw      安装防火墙sudo ufw enable|disable|status         开启/关闭/查看防火墙状态sudo ufw allow 22/ ...

  9. .net Dapper 实践系列(1) ---项目搭建(Layui+Ajax+Dapper+MySQL)

    目录 写在前面 一.前期准备 1.在MySQL创建数据库 2.创建项目 3.安装程序包 4.添加插件 5.添加DbOption文件夹 6.添加实体类 写在前面 学习并实践使用Dapper 这个小型的O ...

  10. CephFS 使用

    原文:https://www.jianshu.com/p/c22ff79c4452 之前介绍了 RBD 的使用方法,有了 RBD,远程磁盘挂载的问题就解决了,但 RBD 的问题是不能多个主机共享一个磁 ...