Forums

MySQL connection Error for scheduled tasks

My app connects to mysql directly in addition to django abstraction. "all of the sudden", one of my scheduled task scripts began throwing this error for my connection:

It works just fine on my computer, only gives this traceback on pythonanywhere when run through the scheduler

Traceback (most recent call last): File "/home/NickStefan/swimsetswebsite/swimsetswebsite/swimsetswebsite/swimsets/swimscript/dailyemail.py", line 215, in <module> con = mdb.connect('localhost','nick','dini','mydb') File "/usr/local/lib/python2.7/dist-packages/MySQLdb/init.py", line 81, in Connect return Connection(args, kwargs) File "/usr/local/lib/python2.7/dist-packages/MySQLdb/connections.py", line 187, in init super(Connection, self).init(args, **kwargs2) _mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")

2014-05-27 16:52:17 -- Completed task, took 3.00 seconds, return code was 1.

I use these EXACT same parameters both in the working part of my web app and in this scheduled script dailyemail.py

# connect to the database
try:
   # my pythonanywhere. 
    con = mdb.connect('mysql.server','NickStefan','--hidden-for-this-post--','NickStefan$swimsolodb',
    use_unicode=True, charset='utf8')
except:
   # my local dev database
    con = mdb.connect('localhost','nick','----','mydb')

Here is the working part of my webapp that connects just fine:

# connect to the database and run SQL.
        # try server, if fail, do local
        try:
            con = mdb.connect('mysql.server','NickStefan','--hidden--forthispost---','NickStefan$swimsolodb',
            use_unicode=True, charset='utf8')
        except:
            con = mdb.connect('localhost','nick','----','mydb')

        cur = con.cursor(mdb.cursors.DictCursor)

Any idea what's up?

My guess is that you're running out of database connections, but since your code is masking the original exception, it's hard to tell.

How would I go about fixing that? Ie can I get more connections? If that were the case, why does the main app never run out of connections? Only this other random scheduled script...

Both scripts do close the connections with:

        cur.close()
        con.close()

It was just a guess. Your best bet is to stop masking the exception so we can see the actual error.

So I took out the try, except (good recommendation!) and then ran the file directly from the file system ("save and run") and IT WORKS with or without the try/except!

Tried the scheduler again both with and without and it throws that same connection error:

Traceback (most recent call last): File "/home/NickStefan/swimsetswebsite/swimsetswebsite/swimsetswebsite/swimsets/swimscript/dailyemail.py", line 213, in <module> use_unicode=True, charset='utf8') File "/usr/local/lib/python2.7/dist-packages/MySQLdb/init.py", line 81, in Connect return Connection(args, kwargs) File "/usr/local/lib/python2.7/dist-packages/MySQLdb/connections.py", line 187, in init super(Connection, self).init(args, **kwargs2) _mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'mysql.server' (111)")

2014-05-27 17:37:12 -- Completed task, took 2.00 seconds, return code was 1.

So maybe its just the scheduled tasks system??? I swear some days the same file will work and then other days it doesnt. If someone could look into it, appreciated. Thanks for the suggestion Glenn.

it seems that we have the same issue with the scheduled task and mysql server, my tasks started to fail today as well https://www.pythonanywhere.com/forums/topic/1318/

Thanks for the heads-up, we're investigating.

It looks like a MySQL process died last night; I've restarted it with more logging and we'll keep an eye on it over the course of the day.