Error 2006: “MySQL server has gone away” using Python, Bottle Microframework and Apache

爷,独闯天下 提交于 2019-12-03 09:06:28

This is MySQL error, not Python's.

The list of possible causes and possible solutions is here: MySQL 5.5 Reference Manual: C.5.2.9. MySQL server has gone away.

Possible causes include:

  • You tried to run a query after closing the connection to the server. This indicates a logic error in the application that should be corrected.
  • A client application running on a different host does not have the necessary privileges to connect to the MySQL server from that host.
  • You have encountered a timeout on the server side and the automatic reconnection in the client is disabled (the reconnect flag in the MYSQL structure is equal to 0).
  • You can also get these errors if you send a query to the server that is incorrect or too large. If mysqld receives a packet that is too large or out of order, it assumes that something has gone wrong with the client and closes the connection. If you need big queries (for example, if you are working with big BLOB columns), you can increase the query limit by setting the server's max_allowed_packet variable, which has a default value of 1MB. You may also need to increase the maximum packet size on the client end. More information on setting the packet size is given in Section C.5.2.10, “Packet too large”.
  • You also get a lost connection if you are sending a packet 16MB or larger if your client is older than 4.0.8 and your server is 4.0.8 and above, or the other way around.
  • and so on...

In other words, there are plenty of possible causes. Go through that list and check every possible cause.

Make sure you are not trying to commit to a closed MySqldb object

An answer to a (very closely related) question has been posted here: https://stackoverflow.com/a/982873/209532

It relates directly to the MySQLdb driver (MySQL-python (unmaintained) and mysqlclient (maintained fork)), but the approach is the the same for other driver the does not support automatic reconnect.

For me this was fixed using

MySQLdb.connect("127.0.0.1","root","","db" )

instead of

MySQLdb.connect("localhost","root","","db" )

and then

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