Mongodb django settings

Hi I have a django project which in local it works fine. I deploy on a remote server but mongodb it seems doesn’t work because I have this partial traceback:


        Keyword: FAILED SQL: SELECT "station"."station_name", "station"."id_station" FROM "station"
Params: ()
Pymongo error: OrderedDict([('ok', 0.0), ('errmsg', 'command find requires authentication'), ('code', 13), ('codeName', 'Unauthorized')])
Version: 1.3.6
        Sub SQL: None
        FAILED SQL: None
        Params: None
        Version: None

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/kafka/anaconda3/envs/djangocrops/lib/python3.9/site-packages/django/db/", line 97, in inner
    return func(*args, **kwargs)
  File "/home/kafka/anaconda3/envs/djangocrops/lib/python3.9/site-packages/djongo/", line 70, in fetchmany
    raise db_exe from e

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/kafka/anaconda3/envs/djangocrops/lib/python3.9/", line 973, in _bootstrap_inner
  File "/home/kafka/anaconda3/envs/djangocrops/lib/python3.9/", line 910, in run

I set my mongodb in settings and in a file:

DB django settings:

    'default': {
        'ENGINE' : 'djongo',
        'NAME' : 'meteodata',
        'HOST': '', #localhost',
        'PORT': 27017,
        'USERNAME': 'admin',
        'PASSWORD': 'mypwd',
        'MONGO_URI': "mongodb://admin:mypwd@"

from pymongo import MongoClient
import pymongo

def get_db_handle(db_name, host, port, username, password): #, username, password):
    client = MongoClient(host=host,
                        )# localhost 27017
    print('client', client)
    db_handle = client[db_name]
    return db_handle, client

def get_collection_handle(db_handle, collection_name):
    return db_handle[collection_name]

client = get_db_handle('meteodata', '', 27017, 'admin', 'mypwd')[1]

collection = client.meteodata

What went wrong?
Some hint?

On your remote/production server are you using a hosted MongoDB server or are you running MongoDB locally?

My guess is your configuration should be updated to point to this server or your MongoDB server is missing something to allow your app to access it.

Hi @jeff i run mongo on the same server (locally). I try instead of localhost, the ip of the server but any result.

I would verify that the mongodb instance is actually listening on port 27017 and that there’s no firewall blocking communication with that port.

@KenWhitesell Using on my pc Mongo db Compass I access without problems.
I suppose there is not firewall problem.

I insert in my this code::

def check_mongo_connection(client_uri):
    connection = MongoClient(client_uri)

        print('Data Base Connection Established........')

    except OperationFailure as err:
        print(f"Data Base Connection failed. Error: {err}")

client_uri = 'mongodb://admin:mypwd@myipserver:27017/meteodata'

and it seesm the connection with the database is established because it shows Data Base Connection Established…

According to the Djongo docs, the database settings for a Djongo connection are different than for most other databases. See Get Started - Djongo. But aside from that, I’m not following why you’re trying to connect to MongoDB when you have a default database connection defined for it.

Thank you @KenWhitesell with standard Djongo configuration it works. I used standard configuration because on my pc I used mongo without authentication, but on remote mongodb I use authentication.