当前位置:网站首页>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
边栏推荐
- NET中小型企业项目开发框架系列(一个)
- The solution to the problem that Oracle hugepages are not used, causing the server to be too laggy
- AD637 usage notes
- Emotional analysis of wechat chat records on Valentine's day based on Text Mining
- Yolov5 training custom data set (pycharm ultra detailed version)
- Ethereum ETH的奖励机制
- 从零开始实现lmax-Disruptor队列(四)多线程生产者MultiProducerSequencer原理解析
- Hysbz 2243 staining (tree chain splitting)
- 办公遇到的问题--
- CRM creates its own custom report based on fetch
猜你喜欢

2.2 basic grammar of R language

Scenario interview: ten questions and ten answers about distributed locks

Summarize the reasons for 2XX, 3xx, 4xx, 5xx status codes

K210 learning notes (IV) k210 runs multiple models at the same time

Parker驱动器维修COMPAX控制器维修CPX0200H

Feng Tang's "spring breeze is not as good as you" digital collection, logged into xirang on July 8!

递归查询多级菜单数据

Parker driver maintenance COMPAX controller maintenance cpx0200h

Efficiency difference between row first and column first traversal of mat data types in opencv

资深电感厂家告诉你电感什么情况会有噪音电感噪音是比较常见的一种电感故障情况,如果使用的电感出现了噪音大家也不用着急,只需要准确查找分析出什么何原因,其实还是有具体的方法来解决的。作为一家拥有18年品牌
随机推荐
Gcc9.5 offline installation
华为快游戏调用登录接口失败,返回错误码 -1
使用Aspect制作全局异常处理类
Zhang Lijun: la pénétration de l’incertitude dépend de quatre « invariants»
Oracle checkpoint queue - Analysis of the principle of instance crash recovery
Detailed explanation of memset() function usage
Robot operation mechanism
办公遇到的问题--
深信服X计划-网络协议基础 DNS
初级软件测试必问面试题
从零开始实现lmax-Disruptor队列(四)多线程生产者MultiProducerSequencer原理解析
2.2.3 output of documents
MATLAB | App Designer·我用MATLAB制作了一款LATEX公式实时编辑器
Deeply convinced plan X - network protocol basic DNS
Zhang Lijun: penetrating uncertainty depends on four "invariants"
NET中小型企业项目开发框架系列(一个)
The real situation of programmers
Kingbasees v8r3 cluster maintenance case -- online addition of standby database management node
The primary key is set after the table is created, but auto increment is not set
Defect detection - Halcon surface scratch detection