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:

djongo.exceptions.SQLDecodeError: 

        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/utils.py", line 97, in inner
    return func(*args, **kwargs)
  File "/home/kafka/anaconda3/envs/djangocrops/lib/python3.9/site-packages/djongo/cursor.py", line 70, in fetchmany
    raise db_exe from e
djongo.database.DatabaseError

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/threading.py", line 973, in _bootstrap_inner
    self.run()
  File "/home/kafka/anaconda3/envs/djangocrops/lib/python3.9/threading.py", line 910, in run

I set my mongodb in settings and in a utils.py file:

DB django settings:

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

utils.py

from pymongo import MongoClient
import pymongo

def get_db_handle(db_name, host, port, username, password): #, username, password):
    client = MongoClient(host=host,
                         port=int(port),
                         username=username,
                         password=password,
                         authSource="admin",
                        )# 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', '127.0.0.1', 27017, 'admin', 'mypwd')[1]

collection = client.meteodata

What went wrong?
Some hint?
Thx,

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 127.0.0.1 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 utils.py this code::

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

    try:
        connection.meteodata
        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'
check_mongo_connection(client_uri)

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