Report this

What is the reason for this report?

Django/Mysql/502 error.

Posted on February 27, 2017

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!



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!

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.

@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.

The issue that is being experienced with Django and MySQL on your Droplet seems to be related to either the configuration or the environment setup for your production server. Here’s a step-by-step guide to troubleshoot and resolve it:

Verify MySQL Credentials

Ensure the credentials in settings.py match those in your MySQL server. Test them with the MySQL command-line client:

mysql -u root -p

Then try connecting to the specific database:

USE ueba;
SHOW TABLES;

If you encounter access issues:

  • Grant the necessary privileges to the root user for localhost:
GRANT ALL PRIVILEGES ON ueba.* TO 'root'@'localhost' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;

Verify Django Database Configuration

Ensure your settings.py is correctly configured:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'ueba',
        'USER': 'root',
        'PASSWORD': 'password',
        'HOST': 'localhost',  # Or IP address if remote
        'PORT': '3306',
    }
}

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.