简体   繁体   中英

Not able to connect to MongoDB using django/mongoengine (auth failed)

Below is the screen dump of what I get when I try to connect to mongodb

PS D:\projects\project1> python manage.py shell
Traceback (most recent call last):
  File "C:\Python33\lib\site-packages\pymongo\mongo_client.py", line 381, in __init__
    self._cache_credentials(source, credentials, _connect)
  File "C:\Python33\lib\site-packages\pymongo\mongo_client.py", line 456, in _cache_credentials
    auth.authenticate(credentials, sock_info, self.__simple_command)
  File "C:\Python33\lib\site-packages\pymongo\auth.py", line 243, in authenticate
    auth_func(credentials[1:], sock_info, cmd_func)
  File "C:\Python33\lib\site-packages\pymongo\auth.py", line 222, in _authenticate_mongo_cr
    cmd_func(sock_info, source, query)
  File "C:\Python33\lib\site-packages\pymongo\mongo_client.py", line 687, in __simple_command
    helpers._check_command_response(response, None, msg)
  File "C:\Python33\lib\site-packages\pymongo\helpers.py", line 178, in _check_command_response
    raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: command SON([('authenticate', 1), ('user', 'root'), ('nonce', '341583b26e94ab52'), ('ke
y', 'd5c3bb5bb28e646499bfc65cf7fd3457')]) failed: auth failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python33\lib\site-packages\mongoengine\connection.py", line 122, in get_connection
    _connections[alias] = connection_class(**conn_settings)
  File "C:\Python33\lib\site-packages\pymongo\mongo_client.py", line 383, in __init__
    raise ConfigurationError(str(exc))
pymongo.errors.ConfigurationError: command SON([('authenticate', 1), ('user', 'root'), ('nonce', '341583b26e94ab52'), ('
key', 'd5c3bb5bb28e646499bfc65cf7fd3457')]) failed: auth failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python33\lib\site-packages\django\core\management\__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "C:\Python33\lib\site-packages\django\core\management\__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python33\lib\site-packages\django\core\management\__init__.py", line 261, in fetch_command
    commands = get_commands()
  File "C:\Python33\lib\site-packages\django\core\management\__init__.py", line 107, in get_commands
    apps = settings.INSTALLED_APPS
  File "C:\Python33\lib\site-packages\django\conf\__init__.py", line 54, in __getattr__
    self._setup(name)
  File "C:\Python33\lib\site-packages\django\conf\__init__.py", line 49, in _setup
    self._wrapped = Settings(settings_module)
  File "C:\Python33\lib\site-packages\django\conf\__init__.py", line 128, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "C:\Python33\lib\importlib\__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1584, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1565, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1532, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 584, in _check_name_wrapper
  File "<frozen importlib._bootstrap>", line 1022, in load_module
  File "<frozen importlib._bootstrap>", line 1003, in load_module
  File "<frozen importlib._bootstrap>", line 560, in module_for_loader_wrapper
  File "<frozen importlib._bootstrap>", line 868, in _load_module
  File "<frozen importlib._bootstrap>", line 313, in _call_with_frames_removed
  File "D:\projects\project1\project1\settings.py", line 94, in <module>
    mongoengine.connect(_MONGODB_NAME, host=_MONGODB_DATABASE_HOST)
  File "C:\Python33\lib\site-packages\mongoengine\connection.py", line 161, in connect
    return get_connection(alias)
  File "C:\Python33\lib\site-packages\mongoengine\connection.py", line 124, in get_connection
    raise ConnectionError("Cannot connect to database %s :\n%s" % (alias, e))
mongoengine.connection.ConnectionError: Cannot connect to database default :
command SON([('authenticate', 1), ('user', 'root'), ('nonce', '341583b26e94ab52'), ('key', 'd5c3bb5bb28e646499bfc65cf7fd
3457')]) failed: auth failed
PS D:\projects\project1>

how can this be solved ?

My setup:

PyMongo = 2.7
MongoEngine = 0.8.8
MongoDB = 3.0.3
Python = 3.3.5
Django = 1.6

Database Section of settings.py :

import mongoengine

DATABASES = {
    'default': {
        'ENGINE': '',
        'NAME': 'test',
    },
}


SESSION_ENGINE = 'mongoengine.django.sessions' # optional

_MONGODB_USER = 'root'
_MONGODB_PASSWD = 'root'
_MONGODB_HOST = '127.0.0.1'
_MONGODB_NAME = 'test'
_MONGODB_DATABASE_HOST = \
    'mongodb://%s:%s@%s/%s' \
    % (_MONGODB_USER, _MONGODB_PASSWD, _MONGODB_HOST, _MONGODB_NAME)

mongoengine.connect(_MONGODB_NAME, host=_MONGODB_DATABASE_HOST)

AUTHENTICATION_BACKENDS = (
    'mongoengine.django.auth.MongoEngineBackend',
)

I think the problem is that mongoengine <= 0.9.0 do not support the new security algorithm used by Mongo3+ versions which is SCRAM-SHA-1 but Mongo2.x verions used MONGODB-CR.

I first created the user from MongoDb 2.6.7 using command

> use myDatabase
> db.addUser("username","secretpass")

and then started MongoDb v3.0.4 server on same dbpath. Till now I have been able to successfully save document using my Django 1.8 app. Will update if something goes wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM