librelist archives

« back to archive

[flask-sqlalchemy] MySQL server has gone away

[flask-sqlalchemy] MySQL server has gone away

From:
Drew Larson
Date:
2014-04-08 @ 21:06
I have noticed I am getting 500 Internal Server errors occasionaly and I 
have tracked it down to my view which tries to get query the database:

OperationalError: (OperationalError) (2006, 'MySQL server has gone away') 
'SELECT quiz.id AS quiz_id, quiz.path AS quiz_path, quiz.answer AS 
quiz_answer, quiz._start AS quiz__start, quiz._end AS quiz__end \nFROM 
quiz \nWHERE quiz._start <= %s AND quiz._end > %s \n LIMIT %s' 
(datetime.datetime(2014, 4, 8, 17, 36, 21, 447590), 
datetime.datetime(2014, 4, 8, 17, 36, 21, 447590), 1)
The MySQL config looks like this # grep 'timeout' /etc/my.cnf wait_timeout
= 60 interactive-timeout

It looks like SQLAlchemy is trying to use a connection that has been 
closed by the server, but I don’t know how to fix this. I would prefer to 
fix this on the Flask side rather than MySQL side.

Any pointers would be greatly appreciated. Thanks.

Drew



Re: [flask] [flask-sqlalchemy] MySQL server has gone away

From:
Luka Raljević
Date:
2014-04-09 @ 19:44
Have you set  SQLALCHEMY_POOL_RECYCLE as per :
http://pythonhosted.org/Flask-SQLAlchemy/config.html

I set it to 3600 seconds and have no problems with 'MySQL server has gone
away' error.



On Tue, Apr 8, 2014 at 11:06 PM, Drew Larson <drew@drwlrsn.com> wrote:

> I have noticed I am getting 500 Internal Server errors occasionaly and I
> have tracked it down to my view which tries to get query the database:
>
> OperationalError: (OperationalError) (2006, 'MySQL server has gone 
away') 'SELECT quiz.id AS quiz_id, quiz.path AS quiz_path, quiz.answer AS 
quiz_answer, quiz._start AS quiz__start, quiz._end AS quiz__end \nFROM 
quiz \nWHERE quiz._start <= %s AND quiz._end > %s \n LIMIT %s' 
(datetime.datetime(2014, 4, 8, 17, 36, 21, 447590), 
datetime.datetime(2014, 4, 8, 17, 36, 21, 447590), 1)
>
> The MySQL config looks like this # grep 'timeout' /etc/my.cnf
> wait_timeout = 60 interactive-timeout
>
> It looks like SQLAlchemy is trying to use a connection that has been
> closed by the server, but I don’t know how to fix this. I would prefer to
> fix this on the Flask side rather than MySQL side.
>
> Any pointers would be greatly appreciated. Thanks.
>
> Drew
>
>
>
>
>


-- 
Luka Raljević

tel : 095 202 2035

Re: [flask] [flask-sqlalchemy] MySQL server has gone away

From:
Sasa Trifunovic
Date:
2014-04-09 @ 20:36
I had the same problem and tried various configurations changes related to
sqlalchemy and none of them worked(for me), 'server has gone away'
kept randomly occurring. So i had to make it ugly/bad with try/except
inside for loop of 3 attempts. If mysql s "fall asleep" you will get an
error but it will also "wake it up" so that the next attempt will work.

attempt = 0

    for attempt in range(3):
        try:
            firstphoto =  Photo.query.filter_by(projectkey=key).first()
            break

        except exc.OperationalError:

            db.session.rollback()
            attempt+=1



On Wed, Apr 9, 2014 at 9:44 PM, Luka Raljević
<luka@implementacija-snova.hr>wrote:

> Have you set  SQLALCHEMY_POOL_RECYCLE as per :
> http://pythonhosted.org/Flask-SQLAlchemy/config.html
>
> I set it to 3600 seconds and have no problems with 'MySQL server has gone
> away' error.
>
>
>
> On Tue, Apr 8, 2014 at 11:06 PM, Drew Larson <drew@drwlrsn.com> wrote:
>
>> I have noticed I am getting 500 Internal Server errors occasionaly and I
>> have tracked it down to my view which tries to get query the database:
>>
>> OperationalError: (OperationalError) (2006, 'MySQL server has gone 
away') 'SELECT quiz.id AS quiz_id, quiz.path AS quiz_path, quiz.answer AS 
quiz_answer, quiz._start AS quiz__start, quiz._end AS quiz__end \nFROM 
quiz \nWHERE quiz._start <= %s AND quiz._end > %s \n LIMIT %s' 
(datetime.datetime(2014, 4, 8, 17, 36, 21, 447590), 
datetime.datetime(2014, 4, 8, 17, 36, 21, 447590), 1)
>>
>> The MySQL config looks like this # grep 'timeout' /etc/my.cnf
>> wait_timeout = 60 interactive-timeout
>>
>> It looks like SQLAlchemy is trying to use a connection that has been
>> closed by the server, but I don’t know how to fix this. I would prefer to
>> fix this on the Flask side rather than MySQL side.
>>
>> Any pointers would be greatly appreciated. Thanks.
>>
>> Drew
>>
>>
>>
>>
>>
>
>
> --
> Luka Raljević
>
> tel : 095 202 2035
>



-- 
www.fractalcastle.com