pymysql.err.OperationalError - Lost connection to MySQL server during query

筅森魡賤 提交于 2021-02-11 12:04:45

问题


I am using Python script to insert records into MySQL database table. The script fails with the following error message.

MySQL version is 8.0.17 ,Python version 3.6.5

(pymysql.err.OperationalError) (2013, 'Lost connection to MySQL server during query ([WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond)')

(Background on this error at: http://sqlalche.me/e/e3q8)

The issue is for only few tables.


回答1:


MySQL automatically closes connections that have been idle for a specific period of time (wait_timeout for non-interactive connections). Therefore it may happen, that your connections are closed if there is too much idle time and connections are not renewed or connections are invalidated because of server restarts.

SQL-Alchemy mentions several strategies on how to tackle the issue of automatic disconnects and database restarts in its documentation on how to deal with pool disconnects.

Two options that you should have a look at are the pool_pre_ping parameter that adds a SELECT 1 before each query to check if the connection is still valid, otherwise the connection will be recycled.

The other option is pool_recycle time that should always be less then your mysql wait_timeout. After this time the connection is automatically recycled to not run in the wait_timeout.

You can check your connections in MySQL using the command

SHOW PROCESSLIST;

where you should see all open connection an the status they are in.



来源:https://stackoverflow.com/questions/63426586/pymysql-err-operationalerror-lost-connection-to-mysql-server-during-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!