Peewee MySQL server has gone away
Asked Answered
L

3

5

I use flask and peewee. Sometimes peewee throws this error

MySQL server has gone away (error(32, 'Broken pipe'))

Peewee database connection

db = PooledMySQLDatabase(database,**{
            "passwd": password, "user": user,
            "max_connections":None,"stale_timeout":None,
            "threadlocals" : True
        })

@app.before_request
def before_request():
    db.connect()

@app.teardown_request
def teardown_request(exception):
    db.close()

After mysql error that "MySQL server has gone away (error(32, 'Broken pipe'))", select queries works without problem, but insert,update,delete queries don't work.

On insert,update,delete queries works behind(in mysql) but peewee throw this errors.

(2006, "MySQL server has gone away (error(32, 'Broken pipe'))")
Lorola answered 2/12, 2015 at 8:40 Comment(3)
Did you try it with a stale_timeout set? The default is 300.Pennyworth
@KlausD. I tried but still has same error. This situation occurs when I close and start mysql during flask is running. Also when mysql down and restart itself.Lorola
That is a usual problem when using connection pools. The most easy way to solve that would be to restart the WSGI server (or how ever you run Flask) together with your MySQL server. Also you should restart your MySQL server rarly, Database servers are made for running not for restarting.Pennyworth
C
5

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_appcontext
def close_database(error):
    db.close()

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

Chingchinghai answered 2/12, 2015 at 13:28 Comment(2)
I tried @app.teardown_appcontext but still has same stuationLorola
@Lorola Did you got any solution to this ? I am facing the same problem !Featherbrain
F
3

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

Fidele answered 29/10, 2016 at 22:20 Comment(0)
A
1

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

Aspect answered 12/8, 2020 at 8:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.