当前位置:网站首页>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
边栏推荐
- 华为游戏多媒体服务调用屏蔽指定玩家语音方法,返回错误码3010
- Sitge joined the opengauss open source community to jointly promote the ecological development of the database industry
- Ad637 notes d'utilisation
- crm创建基于fetch自己的自定义报告
- xlrd常见操作
- Codeforces 12D Ball 树形阵列模拟3排序元素
- oracle 控制文件的多路复用
- How to organize an actual attack and defense drill
- Ethereum ETH的奖励机制
- Did you brush the real title of the blue bridge cup over the years? Come here and teach you to counter attack!
猜你喜欢
Shell script, awk condition judgment and logic comparison &||
使用Aspect制作全局异常处理类
Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
EBS Oracle 11g 克隆步骤(单节点)
ICMP 介绍
Xlrd common operations
K210学习笔记(四) K210同时运行多个模型
怎么利用Tensorflow2进行猫狗分类识别
MMAP
Experienced inductance manufacturers tell you what makes the inductance noisy. Inductance noise is a common inductance fault. If the used inductance makes noise, you don't have to worry. You just need
随机推荐
854. 相似度为 K 的字符串 BFS
A number of ventilator giants' products have been recalled recently, and the ventilator market is still in incremental competition
Pointer parameter passing vs reference parameter passing vs value parameter passing
Yolov5 training custom data set (pycharm ultra detailed version)
poj 3237 Tree(樹鏈拆分)
力扣------经营摩天轮的最大利润
Parker driver maintenance COMPAX controller maintenance cpx0200h
ESP32
Interviewer: will concurrent programming practice meet? (detailed explanation of thread control operation)
SecureCRT使用提示
他们主动布局(autolayout)环境的图像编辑器
Robot operation mechanism
ICMP 介绍
Multiplexing of Oracle control files
Did you brush the real title of the blue bridge cup over the years? Come here and teach you to counter attack!
Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
Codeforces 12D ball tree array simulation 3 sorting elements
Reptile practice
Recursive query of multi-level menu data
Huawei fast game failed to call the login interface, and returned error code -1