Peewee MySQL服务器不见了 [英] Peewee MySQL server has gone away

查看:169
本文介绍了Peewee MySQL服务器不见了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用烧瓶和薄皮纸。有时peewee会引发此错误

I use flask and peewee. Sometimes peewee throws this error

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

Peewee数据库连接

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()

在mysql错误后 MySQL服务器已消失(错误(32,'管道损坏')),选择查询没有问题,但是插入,更新,删除查询不起作用。

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.

在插入,更新,删除查询后(在mysql中)起作用,但peewee抛出此错误。

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

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


推荐答案

peewee文档讨论了此问题,这里是链接:错误2006:MySQL服务器已消失

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


当MySQL终止空闲的数据库连接时,可能会发生此特定错误。对于未明确管理数据库连接的Web应用程序,通常会发生这种情况。发生的情况是您的应用程序启动,打开了连接以处理执行的第一个查询,并且由于该连接从未关闭,因此它将保持打开状态,等待更多查询。

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()

您可能会得到一些文档中的信息:第3步:数据库连接

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

这篇关于Peewee MySQL服务器不见了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆