Django/Mysql/502 error.

Posted February 27, 2017 4.2k views
MySQLDjangoUbuntu 16.04

I am having problems connecting to MySQL database with Django through Droplet.

In the, if I have this set up as my database:

‘default’: {
'ENGINE’: 'django.db.backends.sqlite3’,
'NAME’: os.path.join(BASE_DIR, 'db.sqlite3’),

and I load the website, it gives a Django error that it cannot find some table in the database. But that’s because I’m not using SQLite.

If I add in the MySQL settings:
'default’: {
'ENGINE’: 'django.db.backends.mysql’,
'NAME’: 'ueba’,
'USER’: 'root’,
'PASSWORD’: ’password’,
'HOST’: 'localhost’,
'PORT’: '3306’,

It gives me the 502 Bad Gateway error.

It works fine if I run the server on localhost just with Django/MySQL, but started having problems when I tried migrating it to Droplet. I currently pull MySQL data off the workbench app.

If I need to give more detail, just let me know and any help is greatly appreciated!

  • Have a look in the Django error log, since the 502 error comes from Nginx (I’m guessing you’re using) cannot communicate with the upstream server.

  • I’m not sure where to see the Django error log, but the error in the Nginx error log is:

    2017/02/27 14:26:41 [error] 2236#2236: 254 connect() to unix:/home/django/gunicorn.socket failed (111: Connection refused) while connecting to upstream, client: *client ip, server: _, request: “GET /ueba/ HTTP/1.1”, upstream: “http://unix:/home/django/gunicorn.socket:/ueba/”, host: “”

  • @bdave

    The connect() error is stating that NGINX failed to connect to / the connection was refused by:


    Either that file doesn’t exist, or the permissions on it are incorrect, thus NGINX throws an error. So the error does’t appear (from what you’ve pasted in) to be with MySQL, rather, connecting to the socket.

    What I would recommend doing is running:

    systemctl status gunicorn

    That should give you a detailed look at what’s going on with Gunicorn, and tell you whether it’s running or not. If you will, copy and paste the output here as well.

    You can also try running:

    systemctl restart gunicorn

    … followed by:

    systemctl restart nginx


    service nginx restart
  • @jtittle
    gunicorn.service - Gunicorn daemon for Django Project
    Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled)
    Active: inactive (dead) (Result: exit-code) since Tue 2017-02-28 07:25:51 UTC; 2s ago
    Process: 27202 ExecStart=/usr/bin/gunicorn –name=djangoproject –pythonpath=/home/django/djangoproject –bind unix:/home/django/gunicorn.socket –config /etc/gunicorn.d/ dja
    Main PID: 27202 (code=exited, status=1/FAILURE)

    Feb 28 07:25:51 ueba systemd[1]: gunicorn.service: Main process exited, code=exited, status=1/FAILURE
    Feb 28 07:25:51 ueba systemd[1]: gunicorn.service: Unit entered failed state.
    Feb 28 07:25:51 ueba systemd[1]: gunicorn.service: Failed with result ‘exit-code’.
    Feb 28 07:25:51 ueba systemd[1]: gunicorn.service: Service hold-off time over, scheduling restart.
    Feb 28 07:25:51 ueba systemd[1]: Stopped Gunicorn daemon for Django Project.
    Feb 28 07:25:51 ueba systemd[1]: gunicorn.service: Start request repeated too quickly.
    Feb 28 07:25:51 ueba systemd[1]: Failed to start Gunicorn daemon for Django Project.
    lines 1-13/13 (END)

    There ya go. I also was messing around with it and took the advice to put the IP address of the server into ALLOWED_HOST=[ ], and now get an (104: Connection reset by peer). Not sure if that gets me closer or not.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer


That’d be the issue :-). Gunicorn has failed, thus the socket file doesn’t exist, so when NGINX tries to proxy the request, there’s nothing for it to proxy to and you end up with the failure to connect.

So looking back over the configuration, if the issue is with the MySQL configuration that you are using, you need to double-check that the username, password, and database name are correct and that the user you’ve created has the ability to access the database.

Generally, you’d want to use something such as:

grant all on database.* to 'username'@'localhost' identified by 'password';

… to create your database using the MySQL CLI. You’d then import any data using (if needed):

mysql -u username -p database < data.sql

… or upon successful connection, if your application is supposed to populate the database, then allow it to do so.

The above tells MySQL to use the user username, requests that a password be entered using the -p argument, and tells it to restore the data to the database database. You really don’t want or need to use root as the user as it’s a global user and can do pretty much anything – so if someone does get past any security measures in place, and access is gained, it’d be relatively easy to wipe all data with that user (i.e a simple drop database dbname would be all it takes).

You may want to take a look at:

Since you’re using MySQL, simply skip the Postgres details.

by Justin Ellingwood
Django is a powerful web framework that can help you get your Python application or website off the ground. Django includes a simplified development server for testing your code locally, but for anything even slightly production related, a more secure and powerful web server...