当前位置:网站首页>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_timeout
It 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
边栏推荐
- Simple interest mode - lazy type
- Making global exception handling classes with aspect
- 怎么利用Tensorflow2进行猫狗分类识别
- 2.2.5 basic sentences of R language drawing
- POJ 3237 tree (tree chain splitting)
- Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
- Interviewer: will concurrent programming practice meet? (detailed explanation of thread control operation)
- MMAP learning
- [Yugong series] go teaching course in July 2022 004 go code Notes
- 办公遇到的问题--
猜你喜欢
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
华为游戏多媒体服务调用屏蔽指定玩家语音方法,返回错误码3010
Daily question brushing record (XIV)
JMeter installation under win7
2.2 basic grammar of R language
"Grain mall" -- Summary and induction
A number of ventilator giants' products have been recalled recently, and the ventilator market is still in incremental competition
Interviewer: will concurrent programming practice meet? (detailed explanation of thread control operation)
Shell script, awk uses if, for process control
华为联机对战如何提升玩家匹配成功几率
随机推荐
Multiplexing of Oracle control files
Cross end solutions to improve development efficiency
Zhang Lijun: la pénétration de l’incertitude dépend de quatre « invariants»
Simple interest mode - lazy type
EL与JSTL注意事项汇总
GCC9.5离线安装
Installation of VMware Workstation
SecureCRT使用提示
他们主动布局(autolayout)环境的图像编辑器
JMeter installation under win7
ESP32
NET中小型企业项目开发框架系列(一个)
大约SQL现场“这包括”与“包括在”字符串的写法
Poj 3237 Tree (Tree Chain Split)
1.2 download and installation of the help software rstudio
Oracle checkpoint queue - Analysis of the principle of instance crash recovery
MySQL连接断开报错MySQLdb._exceptions.OperationalError 4031, The client was disconnected by the server
Reptile practice
冯唐“春风十里不如你”数字藏品,7月8日登录希壤!
【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用