当前位置:网站首页>MySQL连接断开报错MySQLdb._exceptions.OperationalError 4031, The client was disconnected by the server
MySQL连接断开报错MySQLdb._exceptions.OperationalError 4031, The client was disconnected by the server
2022-07-05 21:52:00 【XerCis】
问题描述
Flask + SQLAlchemy 的接口服务上线后报错 (MySQLdb._exceptions.OperationalError) (4031, ‘The client was disconnected by the server because of inactivity. See wait_timeout and interactive_timeout for configuring this behavior.’)↵[SQL: xxx]↵(Background on this error at: https://sqlalche.me/e/14/e3q8)
OperationalError
不一定是程序员引起的,例如意外断开、找不到数据源、无法处理事务、内存分配错误等。这种报错源于数据库驱动程序(DBAPI),而不是 SQLAlchemy。
OperationalError
最常见的原因是数据库连接被断开,详细阅读:Dealing with Disconnects
连接池能刷新连接并将旧池中的连接设为无效,有两种方案:
1. 悲观法
每次使用连接池时都执行测试语句测试数据库连接是否正常,如 SELECT 1
。这种方法增加了一点开销但简单可靠。但是,如果丢失连接发生在事务或其他 SQL 操作中,这种预 ping 方法就失效了。
该方法可通过参数 pre_ping=True
实现
engine = create_engine('mysql+pymysql://root:[email protected]:3306/test', pool_pre_ping=True)
2. 乐观法
在断开连接时刷新连接。
参数 pool_recycle
,防止池使用超时的连接
engine = create_engine('mysql+pymysql://root:[email protected]:3306/test', pool_recycle=3600)
解决方案
1. 查看相关变量
SHOW VARIABLES WHERE `Variable_name` IN ('wait_timeout', 'interactive_timeout');
默认空闲 8 小时断开连接
2. 代码
SQLAlchemy
from sqlalchemy import create_engine
engine = create_engine(
'mysql+pymysql://root:[email protected]:3306/test',
pool_pre_ping=True,
pool_size=10,
pool_recycle=7200
)
Flask_SQLAlchemy
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:[email protected]:3306/test'
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {
'pool_pre_ping': True, 'pool_size': 10, 'pool_recycle': 7200}
db = SQLAlchemy(app)
pool_timeout
可能也有用,待试验
根据《SQLAlchemy Python数据库实战》,只用设置pool_recycle=3600
参考文献
边栏推荐
- Zhang Lijun: penetrating uncertainty depends on four "invariants"
- Oracle checkpoint queue - Analysis of the principle of instance crash recovery
- How can Huawei online match improve the success rate of player matching
- 华为快游戏调用登录接口失败,返回错误码 -1
- crm创建基于fetch自己的自定义报告
- 资深电感厂家告诉你电感什么情况会有噪音电感噪音是比较常见的一种电感故障情况,如果使用的电感出现了噪音大家也不用着急,只需要准确查找分析出什么何原因,其实还是有具体的方法来解决的。作为一家拥有18年品牌
- ESP32
- Comprehensive optimization of event R & D workflow | Erda version 2.2 comes as "7"
- 【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
- Simple interest mode - lazy type
猜你喜欢
xlrd常见操作
Haas506 2.0 development tutorial - Alibaba cloud OTA - PAC firmware upgrade (only supports versions above 2.2)
阿里云有奖体验:用PolarDB-X搭建一个高可用系统
1.2 download and installation of the help software rstudio
2.2.3 output of documents
How to view Apache log4j 2 remote code execution vulnerability?
JMeter installation under win7
Alibaba cloud award winning experience: build a highly available system with polardb-x
力扣------经营摩天轮的最大利润
Huawei fast game failed to call the login interface, and returned error code -1
随机推荐
Robot operation mechanism
Ethereum ETH的奖励机制
Deeply convinced plan X - network protocol basic DNS
Cross end solutions to improve development efficiency
Making global exception handling classes with aspect
poj 3237 Tree(树链拆分)
2.2.3 output of documents
张丽俊:穿透不确定性要靠四个“不变”
初级软件测试必问面试题
Zhang Lijun: penetrating uncertainty depends on four "invariants"
Oracle检查点队列–实例崩溃恢复原理剖析
Three components of openpyxl
阿龙的感悟
1.2 download and installation of the help software rstudio
PIP install beatifulsoup4 installation failed
MMAP学习
Tips for using SecureCRT
办公遇到的问题--
EL与JSTL注意事项汇总
华为云ModelArts文本分类–外卖评论