Forums

Mysql django.db.utils.OperationalError: (2006, 'MySQL server has gone away')

Hi

I am trying to deploy a very basic django project with django-cms using mysql(django 1.7)

Locally it behaves fine but on pythonanywhere I am getting this error constantly

I have read this https://www.pythonanywhere.com/wiki/ManagingDatabaseConnections but i dont know where I would put that code

I also dont understand why it runs smoothly on OS X locally but then has this error consistently appears on pythonanywhere.

Do I need to change any DB settings?

Any help appreciated

thanks Chris

Are you always using Django's ORM to connect to the database? Or are you connecting to it directly? On PythonAnywhere, we time out inactive database connections after five minutes, but if you're using Django's ORM it should reconnect transparently in the background. Similarly if you're using SQLAlchemy.

Im only using ORM to query databases This is the only place that I conciously go to the DB for values. The rest is all Django Generic Insert/Update modelform stuff

sqlresults = Event.objects.values('city').filter(city__istartswith=request.GET.get('q', '')).annotate(dcount=Count('city'))

What I did do was remove all my apps from settings.py and the run the app and cms came up fine. I then added one of my apps and the problem started straight away. If you are timing them out after five minutes then I should only theoretically start seeing problems after 5 minutes? Is this a mysql setting? Perhaps I can set it on my laptop and try replicate?

thanks Chris

quick question- did you run syncdb after you added your app?

Yes

Although In order to get this project deployed I have had to play around with the sequencing of the apps as I am using a custom user model

I have definately been liberal in my use of migrate and syncdb and this being my second deployement wouldnt rule out I have done something incorrectly

Its still confusing as Ive followed the same steps locally as on pythonanywhere but still get this issue?

thanks Chris

hmmm. the error could happen if you have a query that takes a particularly long amount of time to execute. Do you know if you are getting the error for all queries or a couple longer ones?

I dont know what query as its happening right at the django-cms welcome screen?

Ive uploaded a screen video here so you can see the weird bejaviour that im seeing:

http://1drv.ms/1Got0DJ. Theres some weird stuff going on with the css almost like the connection to the app is being cut

Im not sure ive not seen anything like it before

Here's what I think is going on: Hitting a page fires off a request that takes a really long time leading to the "MySQL server has gone away" error. This ties up one of your workers for the time it takes for the error to come back from MySQL, so subsequent requests are rejected because all of your workers are busy waiting for MySQL. You are also not using the static files feature of your web app which just makes the problem worse.

To fix this, you need to first identify which query is the problem and remove or optimise it.

Thanks I have corrected the static files situation

Could you help me work out how to establish the offending query in the pythonanywhere environment?

I ran SET global log_output = 'FILE'; SET global general_log_file='mysql_general_after.log'; SET global general_log = 1; in my environment to see what was running and it is just generics cms queries that I would think are reasonably optimal particularly on an empty database.

These queries wont work in pythonanywhere as I dont have privileges so how can I do a trace?

How long is a really long time mentioned above?

Im going to build up my project from scratch and build my app slowly while adding it into pythonanywhere and see when it starts to break

Any guidance on setting up a trace in mysql would be great

thanks Chris

Hi Chris, can you work at it from the Python end? Put a few print statements in your code, before and after various database hits, and see how long they're taking?

Okay I got my project working now. I had to rebuild from scratch but this time I started with a solution built initially by django-cms installer which created everything as it should be

My sense of what happened is that I started developing on Django 1.6 and then upgraded to 1.7 along the way but being new to this Im quite sure I have made mistakes in this and subsequently migrating. So It was in all likelihood a massively flawed project. The Mysql error if im not mistaken is pretty generic in that it could be for a long running query or just a messed up one(in my case more likely)

Lesson learnt!

One question I have regarding monitoring performance on DB's in pythonanywhere. Coming from a DBA background Im used to being able to get under the hood of the database and see whats going on. If I use postresSQL do I get more access to the DB i.e. traces, slow query logs etc? Or is this something I only get with a dedicated server? Im aare that postresSQL and Djnago play well together but what else do I get for my extra $20 per month?

With Postgres you get a dedicated VM running your database instance, so you should have access to all of the admin tools via a superuser account. If any of the things you need don't work, let us know -- it's probably something we need to fix!