当前位置:网站首页>MySQL disconnection reports an error MySQL ldb_ exceptions. OperationalError 4031, The client was disconnected by the server
MySQL disconnection reports an error MySQL ldb_ exceptions. OperationalError 4031, The client was disconnected by the server
2022-07-05 21:56:00 【XerCis】
List of articles
Problem description
Flask + SQLAlchemy An error is reported after the interface service of (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 Not necessarily caused by programmers , For example, accidental disconnection 、 Data source not found 、 Unable to process transaction 、 Memory allocation error, etc . This error is caused by the database driver (DBAPI), instead of SQLAlchemy.
OperationalError The most common reason is that the database connection is disconnected , Read in detail :Dealing with Disconnects
The connection pool can refresh the connections and make the connections in the old pool invalid , There are two options :
1. Pessimism
Every time you use the connection pool, execute the test statement to test whether the database connection is normal , Such as SELECT 1. This method adds a little overhead but is simple and reliable . however , If the loss of connection occurs in a transaction or other SQL In operation , This pre ping The method fails .
This method can pass the parameter pre_ping=True Realization
engine = create_engine('mysql+pymysql://root:[email protected]:3306/test', pool_pre_ping=True)
2. Optimism
Refresh connection when disconnected .
Parameters pool_recycle , Prevent the pool from using timed out connections
engine = create_engine('mysql+pymysql://root:[email protected]:3306/test', pool_recycle=3600)
Solution
1. View related variables
SHOW VARIABLES WHERE `Variable_name` IN ('wait_timeout', 'interactive_timeout');

Default free 8 Hour disconnect
2. Code
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_timeoutIt may also be useful , To be tested
according to 《SQLAlchemy Python Database practice 》, Just set uppool_recycle=3600
reference
- The client was disconnected by the server because of inactivity Solution
- MySQL SHOW VARIABLES Statement
- airflow Report errors mysql Connection timeout disconnected problem solved
- Connection Pooling — SQLAlchemy Documentation
- solve mysql The server actively disconnects when there is no operation timeout
- Further study of sqlalchemy Connection pool
- flask_sqlalchemy pool_pre_ping only working sometimes
边栏推荐
- Interview questions for basic software testing
- Huawei fast game failed to call the login interface, and returned error code -1
- 递归查询多级菜单数据
- sql常用语法记录
- PIP install beatifulsoup4 installation failed
- AD637 usage notes
- CRM creates its own custom report based on fetch
- 场景化面试:关于分布式锁的十问十答
- Three components of openpyxl
- ICMP introduction
猜你喜欢

Daily question brushing record (XIV)

装饰器学习01

Getting started with microservices (resttemplate, Eureka, Nacos, feign, gateway)

华为游戏多媒体服务调用屏蔽指定玩家语音方法,返回错误码3010

Matlab | app designer · I used Matlab to make a real-time editor of latex formula

xlrd常见操作

Incentive mechanism of Ethereum eth

An exception occurred in Huawei game multimedia calling the room switching method internal system error Reason:90000017

張麗俊:穿透不確定性要靠四個“不變”

Yolov5 training custom data set (pycharm ultra detailed version)
随机推荐
Oracle HugePages没有被使用导致服务器很卡的解决方法
MMAP learning
POJ 3237 tree (tree chain splitting)
他们主动布局(autolayout)环境的图像编辑器
Defect detection - Halcon surface scratch detection
854. String BFS with similarity K
CRM creates its own custom report based on fetch
Oracle checkpoint queue - Analysis of the principle of instance crash recovery
Yolov5 training custom data set (pycharm ultra detailed version)
Learning notes of statistical learning methods -- Chapter 1 Introduction to statistical learning methods
HDU 4391 Paint The Wall 段树(水
Objects in the list, sorted by a field
NET中小型企业项目开发框架系列(一个)
oracle 控制文件的多路复用
EL与JSTL注意事项汇总
Daily question brushing record (XIV)
Reptile practice
华为游戏多媒体调用切换房间方法出现异常Internal system error. Reason:90000017
Emotional analysis of wechat chat records on Valentine's day based on Text Mining
JMeter installation under win7