当前位置:网站首页>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
边栏推荐
- R language learning notes
- Code bug correction, char is converted to int high-order symbol extension, resulting in changes in positivity and negativity and values. Int num = (int) (unsigned int) a, which will occur in older com
- Shell script, awk condition judgment and logic comparison &||
- Sitge joined the opengauss open source community to jointly promote the ecological development of the database industry
- How to use tensorflow2 for cat and dog classification and recognition
- Daily question brushing record (XIV)
- Uni app Bluetooth communication
- EL与JSTL注意事项汇总
- K210学习笔记(四) K210同时运行多个模型
- MMAP learning
猜你喜欢
Huawei fast game failed to call the login interface, and returned error code -1
多家呼吸机巨头产品近期被一级召回 呼吸机市场仍在增量竞争
2.2.5 basic sentences of R language drawing
华为快游戏调用登录接口失败,返回错误码 -1
Sorting out the problems encountered in MySQL built by pycharm connecting virtual machines
Oracle检查点队列–实例崩溃恢复原理剖析
Kingbasees v8r3 cluster maintenance case -- online addition of standby database management node
A number of ventilator giants' products have been recalled recently, and the ventilator market is still in incremental competition
EBS Oracle 11g 克隆步骤(单节点)
Cold violence -- another perspective of objective function setting
随机推荐
Poj3414 extensive search
MMAP
Robot framework setting variables
【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
854. String BFS with similarity K
How to view Apache log4j 2 remote code execution vulnerability?
Basic grammar of interview (Part 1)
How to use tensorflow2 for cat and dog classification and recognition
Feng Tang's "spring breeze is not as good as you" digital collection, logged into xirang on July 8!
Shell script, awk uses if, for process control
阿里云有奖体验:用PolarDB-X搭建一个高可用系统
MySQL连接断开报错MySQLdb._exceptions.OperationalError 4031, The client was disconnected by the server
初级软件测试必问面试题
MMAP
Deployment of Jenkins under win7
Sitge joined the opengauss open source community to jointly promote the ecological development of the database industry
Gcc9.5 offline installation
Exercise 1 simple training of R language drawing
Some common processing problems of structural equation model Amos software
华为游戏多媒体服务调用屏蔽指定玩家语音方法,返回错误码3010