当前位置:网站首页>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
参考文献
边栏推荐
- Three components of openpyxl
- Pointer parameter passing vs reference parameter passing vs value parameter passing
- crm创建基于fetch自己的自定义报告
- Poj 3237 Tree (Tree Chain Split)
- MMAP
- Four components of logger
- 从零开始实现lmax-Disruptor队列(四)多线程生产者MultiProducerSequencer原理解析
- Zhang Lijun: penetrating uncertainty depends on four "invariants"
- 让开发效率提升的跨端方案
- Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
猜你喜欢
ICMP 介绍
QML reported an error expected token ";", expected a qualified name ID
使用Aspect制作全局异常处理类
MMAP
华为云ModelArts文本分类–外卖评论
MMAP learning
Uni app Bluetooth communication
Cross end solution to improve development efficiency rapidly
How to prepare for the algorithm interview and answer the algorithm interview questions
Zhang Lijun: penetrating uncertainty depends on four "invariants"
随机推荐
sql常用语法记录
Poj3414广泛搜索
About the writing method of SQL field "this includes" and "included in" strings
Oracle检查点队列–实例崩溃恢复原理剖析
[daily training] 729 My schedule I
Summarize the reasons for 2XX, 3xx, 4xx, 5xx status codes
Robot operation mechanism
冯唐“春风十里不如你”数字藏品,7月8日登录希壤!
大约SQL现场“这包括”与“包括在”字符串的写法
Regular expressions and re Libraries
oracle 控制文件的多路复用
Yolov5 training custom data set (pycharm ultra detailed version)
SecureCRT使用提示
Environment configuration problem record
"Grain mall" -- Summary and induction
SQL knowledge leak detection
怎么利用Tensorflow2进行猫狗分类识别
资深电感厂家告诉你电感什么情况会有噪音电感噪音是比较常见的一种电感故障情况,如果使用的电感出现了噪音大家也不用着急,只需要准确查找分析出什么何原因,其实还是有具体的方法来解决的。作为一家拥有18年品牌
Sorting out the problems encountered in MySQL built by pycharm connecting virtual machines
如何组织一场实战攻防演练