无法使用django / mongoengine连接到MongoDB(auth失败)

时间:2015-06-03 01:27:50

标签: python django mongodb mongoengine

下面是我尝试连接到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>

如何解决这个问题?

我的设置:

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

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',
)

1 个答案:

答案 0 :(得分:4)

我认为问题是 mongoengine&lt; = 0.9.0 不支持Mongo3 +版本使用的新安全算法,即SCRAM-SHA-1,但Mongo2.x版本使用了MONGODB-CR

我首先使用命令

从MongoDb 2.6.7创建用户
> use myDatabase
> db.addUser("username","secretpass")

然后在同一个dbpath上启动MongoDb v3.0.4服务器。直到现在我已经能够使用我的Django 1.8应用程序成功保存文档。如果出现问题,将会更新。