当前位置:网站首页>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
边栏推荐
- Hysbz 2243 staining (tree chain splitting)
- Deeply convinced plan X - network protocol basic DNS
- 每日刷题记录 (十四)
- one hundred and twenty-three thousand four hundred and fifty-six
- HDU 4391 Paint The Wall 段树(水
- Analysis and test of ModbusRTU communication protocol
- Three components of openpyxl
- 华为快游戏调用登录接口失败,返回错误码 -1
- A long's perception
- 华为联机对战如何提升玩家匹配成功几率
猜你喜欢
"Grain mall" -- Summary and induction
The real situation of programmers
xlrd常见操作
递归查询多级菜单数据
Drawing HSV color wheel with MATLAB
Matlab | app designer · I used Matlab to make a real-time editor of latex formula
Huawei cloud modelarts text classification - takeout comments
Zhang Lijun: la pénétration de l’incertitude dépend de quatre « invariants»
Kingbasees v8r3 cluster maintenance case -- online addition of standby database management node
使用Aspect制作全局异常处理类
随机推荐
The solution to the problem that Oracle hugepages are not used, causing the server to be too laggy
MMAP
EBS Oracle 11g 克隆步骤(单节点)
datagrid直接编辑保存“设计缺陷”
Cold violence -- another perspective of objective function setting
crm创建基于fetch自己的自定义报告
AD637使用筆記
xlrd常见操作
About the writing method of SQL field "this includes" and "included in" strings
Feng Tang's "spring breeze is not as good as you" digital collection, logged into xirang on July 8!
华为快游戏调用登录接口失败,返回错误码 -1
每日刷题记录 (十四)
Gcc9.5 offline installation
How can Huawei online match improve the success rate of player matching
SecureCRT使用提示
Codeforces 12D Ball 树形阵列模拟3排序元素
K210 learning notes (IV) k210 runs multiple models at the same time
張麗俊:穿透不確定性要靠四個“不變”
Matlab | app designer · I used Matlab to make a real-time editor of latex formula
EBS Oracle 11g cloning steps (single node)