Peewee MySQL server has gone away Peewee MySQL server has gone away flask flask

Peewee MySQL server has gone away


The peewee documentation has talked about this problem, here is the link: Error 2006: MySQL server has gone away

This particular error can occur when MySQL kills an idle database connection. This typically happens with web apps that do not explicitly manage database connections. What happens is your application starts, a connection is opened to handle the first query that executes, and, since that connection is never closed, it remains open, waiting for more queries.

So you have some problems on managing your database connection.


Since I can't reproduce your problem, could you please try this one, close your database this way:

@app.teardown_appcontextdef close_database(error):    db.close()

And you may get some info from the doc: Step 3: Database Connections


I know this is an old question, but since there's no accepted answer I thought I'd add my two cents.

I was having the same problem when committing largeish amounts of data in Peewee objects (larger than the amount of data MySQL allows in a single commit by default). I fixed it by changing the max_allowed_packet size in my.conf.

To do this, open my.conf, add the following line under [mysqld]:

max_allowed_packet=50M

... or whatever size you need and restart mysqld


I know this is an old question, but I also fixed the problem in another way which might be of interest. In my case, it was an insert_many which was too large.To fix it, simply do the insert in batches, as described in the peewee documentation