Django always connects to localhost mongodb

Viewed 1089

I have followed the guide of db connection config: https://nesdis.github.io/djongo/database-configuration/

However, it always connects to localhost one, not my setting's one.

Does anyone have any idea on this issue?

my packages versions:

Django  2.0
django-cors-headers 2.4.0   
django-rest-auth    0.9.3   
djangorestframework 3.9.0   
djongo  1.1 
mongoengine 0.16.3  
pip 10.0.1  
pymongo 3.7.2   
urllib3 1.24.1  

my settings

DATABASES = {
    'default': {
        'ENGINE': 'djongo',
        'NAME': 'test_db',
        'HOST': 'somewhere.com',
        'PORT': 27017
    }
}
3 Answers

Is seems that somewhere along the way, djongo changed the structure of the database settings. After wasting days trying to find the solution, I came across a page that had the updated structure, Try setting your DATABASE structure to this:

DATABASES = {
  'default': {  
    'ENGINE':   'djongo',
    'NAME':     'yourmongodb',
    'CLIENT': {
      'host': 'some-host.or.ip',
      'port': 27017,
      'username': 'youruser',
      'password': 'yourdbpass',
      'authSource': 'yourcollection',
    }
  },
}

Have you edited your setting in the settings.py file

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'XXXX',
        'USER': 'XXX',
        'PASSWORD': 'XXXX',
        'HOST': 'XXX.XXX.XXX.XXX',
        'PORT': '3306',
        'OPTION': {
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
        }
    }
}

First: Create a SSH Tunnel to remote mysql server on localhost

ssh -N -L 3306:127.0.0.1:3306 root@192.168.0.122

192.168.0.122 >> is a remote LAN server of mine.

Second : use 127.0.0.1 as a HOST on Django connection string.

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'XXXX',
    'USER': 'XXX',
    'PASSWORD': 'XXXX',
    'HOST': '127.0.0.1',
    'PORT': '3306',
    'OPTION': {
        'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
    }
}

OR change HOST to 127.0.0.1 in .env if using environment file.

This worked for me.

Related