4

i am trying to use MongoDB data base in django. For that i changed my setting to this I am using mlab here.

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'djongo',
        'NAME': 'crud-django',
        'HOST': 'mongodb://adityakmr:[email protected]:31956/',
        'USER': 'adityakmr',
        'PASSWORD': 'password1'
    }
}

But everytime i try to run migrations i got some error like this. I have no idea where it is comming from i have already provided correct user name and password. I have also installed djongo using pip.

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\adity\Desktop\crud-django\env\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\adity\Desktop\crud-django\env\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\adity\Desktop\crud-django\env\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\adity\Desktop\crud-django\env\lib\site-packages\django\core\management\base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "C:\Users\adity\Desktop\crud-django\env\lib\site-packages\django\core\management\base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Users\adity\Desktop\crud-django\env\lib\site-packages\django\core\management\commands\migrate.py", line 87, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File "C:\Users\adity\Desktop\crud-django\env\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "C:\Users\adity\Desktop\crud-django\env\lib\site-packages\django\db\migrations\loader.py", line 49, in __init__
    self.build_graph()
  File "C:\Users\adity\Desktop\crud-django\env\lib\site-packages\django\db\migrations\loader.py", line 212, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "C:\Users\adity\Desktop\crud-django\env\lib\site-packages\django\db\migrations\recorder.py", line 73, in applied_migrations
    if self.has_table():
  File "C:\Users\adity\Desktop\crud-django\env\lib\site-packages\django\db\migrations\recorder.py", line 56, in has_table
    return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
  File "C:\Users\adity\Desktop\crud-django\env\lib\site-packages\django\db\backends\base\introspection.py", line 48, in table_names
    return get_names(cursor)
  File "C:\Users\adity\Desktop\crud-django\env\lib\site-packages\django\db\backends\base\introspection.py", line 43, in get_names
    return sorted(ti.name for ti in self.get_table_list(cursor)
  File "C:\Users\adity\Desktop\crud-django\env\lib\site-packages\djongo\introspection.py", line 46, in get_table_list
    for c in cursor.db_conn.collection_names(False)
  File "C:\Users\adity\Desktop\crud-django\env\lib\site-packages\pymongo\database.py", line 715, in collection_names
    nameOnly=True, **kws)]
  File "C:\Users\adity\Desktop\crud-django\env\lib\site-packages\pymongo\database.py", line 674, in list_collections
    read_pref) as (sock_info, slave_okay):
  File "c:\users\adity\appdata\local\programs\python\python37-32\Lib\contextlib.py", line 112, in __enter__
    return next(self.gen)
  File "C:\Users\adity\Desktop\crud-django\env\lib\site-packages\pymongo\mongo_client.py", line 1101, in _socket_for_reads
    with self._get_socket(server) as sock_info:
  File "c:\users\adity\appdata\local\programs\python\python37-32\Lib\contextlib.py", line 112, in __enter__
    return next(self.gen)
  File "C:\Users\adity\Desktop\crud-django\env\lib\site-packages\pymongo\mongo_client.py", line 1058, in _get_socket
    with server.get_socket(self.__all_credentials) as sock_info:
  File "c:\users\adity\appdata\local\programs\python\python37-32\Lib\contextlib.py", line 112, in __enter__
    return next(self.gen)
  File "C:\Users\adity\Desktop\crud-django\env\lib\site-packages\pymongo\pool.py", line 1006, in get_socket
    sock_info.check_auth(all_credentials)
  File "C:\Users\adity\Desktop\crud-django\env\lib\site-packages\pymongo\pool.py", line 677, in check_auth
    auth.authenticate(credentials, self)
  File "C:\Users\adity\Desktop\crud-django\env\lib\site-packages\pymongo\auth.py", line 563, in authenticate
    auth_func(credentials, sock_info)
  File "C:\Users\adity\Desktop\crud-django\env\lib\site-packages\pymongo\auth.py", line 540, in _authenticate_default
    return _authenticate_scram(credentials, sock_info, 'SCRAM-SHA-1')
  File "C:\Users\adity\Desktop\crud-django\env\lib\site-packages\pymongo\auth.py", line 262, in _authenticate_scram
    res = sock_info.command(source, cmd)
  File "C:\Users\adity\Desktop\crud-django\env\lib\site-packages\pymongo\pool.py", line 579, in command
    unacknowledged=unacknowledged)
  File "C:\Users\adity\Desktop\crud-django\env\lib\site-packages\pymongo\network.py", line 150, in command
    parse_write_concern_error=parse_write_concern_error)
  File "C:\Users\adity\Desktop\crud-django\env\lib\site-packages\pymongo\helpers.py", line 155, in _check_command_response
    raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: Authentication failed.
6
  • Is you mongo database password is correct with provided credentials? Commented Apr 4, 2019 at 15:57
  • yes, the password is correct I have checked thrice. Commented Apr 4, 2019 at 15:58
  • 3
    Try this DATABASES = {{ 'default': { 'NAME': 'DB_NAME', 'AUTH_SOURCE': 'DB_NAME', 'USER': '<username>', 'PASSWORD': '<password>', 'HOST': '<code>.mlab.com', 'PORT': <port>, 'ENGINE': 'djongo' } }} Commented Apr 4, 2019 at 16:05
  • what should i put in AUTH_SOURCE ? Commented Apr 4, 2019 at 16:07
  • Your db name, the db use for django project. make sure you can correct this db with your username and password Commented Apr 4, 2019 at 16:07

2 Answers 2

2

In latest version use client to connect to DB. I hope following DB configuration will resolve your issue.

DATABASES = {
    'default': {
        'ENGINE': 'djongo',
        'ENFORCE_SCHEMA': True,
        'LOGGING': {
            'version': 1,
            'loggers': {
                'djongo': {
                    'level': 'DEBUG',
                    'propogate': False,                        
                }
            },
         },
        'NAME': 'my_db',
        'CLIENT': {
            'host': '127.0.0.1',
            'port': 27017,
            'username': 'XXXXXXXXXX',
            'password': "XXXXXXXXXXXXX",
            'authSource': 'admin',
            'authMechanism': 'SCRAM-SHA-1'
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

that not work! ``` django.core.exceptions.ImproperlyConfigured: 'djongo' isn't an available database backend or couldn't be imported. Check the above exception. To use one of the built-in backends, use 'django.db.backends.XXX', where XXX is one of: 'mysql', 'oracle', 'postgresql', 'sqlite3' ```
0

You have to use USERNAME instead USER

DATABASES = { 'default': { 'ENGINE': 'djongo', 'NAME': 'test_db', 'USERNAME': 'username', 'PASSWORD': 'password', 'HOST': 'host', 'PORT': 27017, 'AUTH_SOURCE': 'test_db', } }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.