V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
azure
V2EX  ›  Python

MySQL server has gone away..求助!

  •  
  •   azure · 2012-11-22 10:14:00 +08:00 · 6149 次点击
    这是一个创建于 4174 天前的主题,其中的信息可能已经有所发展或是发生改变。
    "/home/envs/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler raise errorclass, errorvalue
    sqlalchemy.exc.OperationalError: (OperationalError) (2006, 'MySQL server has gone away')


    python,flask,SqlAlchemy
    网站刚刚拉起来的时候运行正常,但是过1,2个小时以后就报503错误。差了下日志如上。
    百度and谷歌了一下,
    都是推荐修改
    wait_timeout=2880000
    interactive_timeout = 2880000  
    但是这两个值如果没设置的话,默认就是8小时吧?



    max_allowed_packet = 10M这个貌似就更没必要修改了吧?因为一开始是正常的。过1,2个小时才有问题。


    请问要怎么弄呀。。
    5 条回复    1970-01-01 08:00:00 +08:00
    Livid
        1
    Livid  
    MOD
       2012-11-22 10:15:08 +08:00   ❤️ 1
    出错时就让程序重连一下 MySQL 服务器。
    neildd
        2
    neildd  
       2012-11-22 10:16:48 +08:00
    是不是在多线程里调用了MySQLdb,多线程里会出这种问题。
    neildd
        3
    neildd  
       2012-11-22 10:17:45 +08:00
    import MySQLdb

    class MyDBClass:
    def __init__(self, db_host, db_user, db_pass, db_name, db_charset='UTF8'):
    self.db_host = db_host
    self.db_user = db_user
    self.db_pass = db_pass
    self.db_name = db_name
    self.db_charset = db_charset
    self.db = None
    def db_connect(self):
    self.db = MySQLdb.connect(host=self.db_host, user=self.db_user, passwd=self.db_pass, db=self.db_name, charset=self.db_charset)
    self.cursor = self.db.cursor()
    def db_ping(self):
    if not self.db:
    self.db_connect()
    else:
    try:
    self.db.ping()
    except:
    self.db_connect()
    def db_fetchone(self, *args):
    self.db_ping()
    self.cursor.execute(*args)
    ret = self.cursor.fetchone()
    return ret
    def db_fetchall(self, *args):
    self.db_ping()
    self.cursor.execute(*args)
    ret = self.cursor.fetchall()
    return ret
    def db_execute(self, *args):
    self.db_ping()
    self.cursor.execute(*args)
    return self.db.insert_id()
    codenamea
        4
    codenamea  
       2012-11-22 12:26:27 +08:00   ❤️ 1
    app.config["SQLALCHEMY_POOL_RECYCLE"] = XXX 这个值比mysql里设置的略小一点。这样每次在mysql连接超时之前,sqlalchemy就会自动回收。
    另外2个小时就掉了,感觉不是8小时的超时。你可以自己输出以下mysql的配置看一下。 :
    show variables like '%timeout';
    ElmerZhang
        5
    ElmerZhang  
       2012-11-22 13:30:44 +08:00
    报gone away时重新连接就好了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2693 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 04:13 · PVG 12:13 · LAX 21:13 · JFK 00:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.