1

I am having an unusual problem: I successfully deployed a Django app on the digital ocean app platform but the problem is that whenever I am trying to reach any route that requires database app crashes because of MongoDB connection problem and after some time I see

upstream connect error or disconnect/reset before headers. reset reason: connection termination

When I check the logs I see the ServerSelectionTimeoutError from pymongo

So the first thing I checked is the host URL which is correct because using the same credentials I am able to connect and query data from my local pc by a python program I followed the format:

"mongodb:srv//myusername:mypassword@clustername.mongodb.net/mydbname my password doesn't contain any special characters

In my Django app settings, I put all the required variables to get Django working with MongoDB -I used djongo as an engine

DATABASES = {
'default': {
    'ENGINE': 'djongo',
    'NAME': 'stocks',
    'HOST': os.getenv("DB_HOST"),
    'USER': os.getenv("DB_USERNAME"),
    'PASSWORD': os.getenv("DB_PASSWORD")
}

}

All the variables are set correctly I checked in an App platform console

Another thing is that if I run python shell in apps console on digital ocean so directly on a server I get a different error immediately but when accessing from the deployed website it takes some time to get above error:

pymongo.errors.OperationFailure: Authentication failed., full error: {'ok': 0, 'errmsg': 'Authentication failed.', 'code': 8000, 'codeName': 'AtlasError'}

I am still kinda new to web development it's maybe my 5th app and never had any problems connecting to MongoDB so maybe there something I am missing. First time asking a question here thanks anyone in advance any help would be much appreciated :)

Michał
  • 31
  • 3
  • More often than not `Authentication Failure` is simply related to wrong user and password. I've not used MongoDB with Django so cant say for sure, but the following could help: 1. Check your DB server's host:port is open for incoming connections 2. Print the environment variables and see they are what you expect them to be – Anubhab Apr 24 '21 at 05:24
  • Hey thanks but the username and password are right because using exactly the same credentials I have no problem connecting to DB from my PC, but from the server on the digital ocean's app platform it doesn't connect. Variables are correct I checked. – Michał Apr 24 '21 at 16:06

2 Answers2

2

For someone in the future having the same problem :) The solution is to specify two more keys in the Django database settings file:

  • authSource - which I set as admin
  • authMechanism - which I set to SCRAM-SHA-1

Not sure which one of those solved the issue. Now the correct connection syntax in Django's settings.py for me looks like that:

    DATABASES = {
    'default': {
        'ENGINE': 'djongo',
        'NAME': 'stocks',
        'CLIENT': {
            'host': os.getenv("DB_HOST"),
            'username': os.getenv("DB_USERNAME"),
            'password': os.getenv("DB_PASSWORD"),
            'authSource': 'admin',
            'authMechanism': 'SCRAM-SHA-1'
        }
    }
}

Special thanks for @BellyBuster :)

Michał
  • 31
  • 3
-2

More than likely you need to whitelist the IP address of the calling server in Atlas.

Belly Buster
  • 8,224
  • 2
  • 7
  • 20
  • hey thanks but it's not the case since all traffic is allowed - I put 0.0.0.0/0 value up there – Michał Apr 23 '21 at 19:26
  • Take a look at https://stackoverflow.com/questions/64616386/django-mongo-docker-getting-pymongo-errors-serverselectiontimeouterror/64633886#64633886 – Belly Buster Apr 24 '21 at 01:18
  • Thanks a lot, it solved my issue I wasted a lot of time on something so small :D – Michał Apr 24 '21 at 16:29