peewee: OperationalError: (2006, ‘MySQL server has gone away’)

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: peewee: OperationalError: (2006, ‘MySQL server has gone away’)

MySQL默认长链接只能保持8小时,超过后就会自动断开。

peewee报错:

OperationalError: (2006, ‘MySQL server has gone away’)

在peewee2中如何维持长连接,可以如下操作:

from peewee import MySQLDatabase
from playhouse.shortcuts import RetryOperationalError
class RetryMySQLDatabase(RetryOperationalError, MySQLDatabase):
    pass
db = RetryMySQLDatabase(
    host='127.0.0.1',
    database='dataname',
    user="root",
    passwd="123456",
    charset='utf8'
)

Python3中已经去除RetryOperationalError, 后来的版本又加上了(2021-03-03补充)

不过作者不推荐使用,想象如果在一个事务中,断开了链接,进行了自动重连

from peewee import MySQLDatabase
from playhouse.shortcuts import ReconnectMixin
# 防止断开 see: https://github.com/coleifer/peewee/issues/1992
class ReconnectMySQLDatabase(ReconnectMixin, MySQLDatabase):
    """peewee作者不推荐使用"""
    pass

参考

peewee解决问题”OperationalError: (2006, ‘MySQL server has gone away’)”

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
5月前
|
关系型数据库 MySQL 数据库
解决Navicat报错:2013 - Lost connection to MySQL server during query
解决Navicat报错:2013 - Lost connection to MySQL server during query
236 0
|
6月前
|
SQL 关系型数据库 MySQL
【SQL异常】启动MySQL报错:ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)
【SQL异常】启动MySQL报错:ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)
83 1
|
7月前
|
关系型数据库 MySQL 分布式数据库
"Can't connect to MySQL server
"Can't connect to MySQL server
63 2
|
7月前
|
关系型数据库 MySQL
报错:1130-host … is not allowed to connect to this MySql server,MySQL
报错:1130-host … is not allowed to connect to this MySql server,MySQL
165 0
|
关系型数据库 MySQL 网络安全
关于对连接数据库时出现1130-host “**” is not allowed to connect to this MySql/mariadb server
关于对连接数据库时出现1130-host “**” is not allowed to connect to this MySql/mariadb server
376 0
|
SQL 机器学习/深度学习 数据库
|
关系型数据库 MySQL 数据库
MySQL数据库:Can not connect to MySQL server 解决方法
MySQL数据库:Can not connect to MySQL server 解决方法
664 0
MySQL数据库:Can not connect to MySQL server 解决方法
|
SQL 关系型数据库 MySQL
pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query')
pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query')
549 0
|
关系型数据库 MySQL Python
peewee: OperationalError: (2006, ‘MySQL server has gone away’)
peewee: OperationalError: (2006, ‘MySQL server has gone away’)
248 0
|
关系型数据库 MySQL
peewee.ImproperlyConfigured: MySQL driver not installed!
peewee.ImproperlyConfigured: MySQL driver not installed!
226 0