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'))")
Alexander
  • 1,720
  • 4
  • 22
  • 40
  • Did you try it with a `stale_timeout` set? The default is `300`. – Klaus D. Dec 02 '15 at 08:49
  • @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. – Alexander Dec 02 '15 at 09:11
  • 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. – Klaus D. Dec 02 '15 at 10:57

3 Answers3

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

lord63. j
  • 4,500
  • 2
  • 22
  • 30
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

silenus
  • 53
  • 3
0

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

Dominus
  • 808
  • 11
  • 25