Question
Django/Mysql/502 error.
I am having problems connecting to MySQL database with Django through Droplet.
In the settings.py, if I have this set up as my database:
DATABASES = {
‘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:
DATABASES = {
'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!
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.
×
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: “174.138.77.90”
@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:
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:
… followed by:
or
@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/gunicorn.py 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.