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!

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

@bdave

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:

https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04

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