Search code examples
pythonmysqldjangomod-wsgipymysql

Django + PyMSQL + WSGI


I have an application accessing a distant mysql base using PySQL and the following fonction

def db_query(username, password, host, db, query):
    try:
        db = pymysql.connect(host=host, user=username, passwd=password, db=db)
    except pymysql.err.OperationalError as e:
        errorLog = e
        return [0, errorLog]

    # you must create a Cursor object. It will let you execute all the query you need
    cur = db.cursor()

    cur.execute(query)
    result = cur.fetchall()

    cur.close()
    db.close()  

    return result

I have recently update Django from 1.7.1 to 1.7.3. Before update code worked. Now, when I'm in production using WSGI and only then (not on local dev server, nor on server dev server) I get a (2003, "Can't connect to MySQL server on [server.address] ([Errno 13] Permission denied)")

I have updated pymysql to latest version available on pip.

I see that 1.7.3 correcting some security issue in WSGI, i tried to downgrade to 1.7.2 (only by doing pip install django==1.7.2 (not sure this is the good way)) and the issue is still present.

Any idea on what i could try to check ?

Thank you in advance for your help.


Solution

  • I think this is related to apache permission to access MySQL database. Can you try command:

    setsebool -P httpd_can_network_connect_db 1
    

    Using -P option makes the change persistent. Without this option, the boolean value would be reset to 0 at reboot.