pymongo.errors.OperationFailure:身份验证失败

时间:2019-04-04 15:54:21

标签: django mongodb

我正在尝试在Django中使用MongoDB数据库。为此,我将设置更改为此,我在这里使用mlab。

  

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'djongo',
        'NAME': 'crud-django',
        'HOST': 'mongodb://adityakmr:password1@ds231956.mlab.com:31956/',
        'USER': 'adityakmr',
        'PASSWORD': 'password1'
    }
}

但是每次我尝试运行迁移时,都会出现类似这样的错误。我不知道它来自哪里,我已经提供了正确的用户名和密码。 我还使用pip安装了djongo。

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.

2 个答案:

答案 0 :(得分:1)

在最新版本中,使用客户端连接到数据库。 我希望遵循数据库配置可以解决您的问题。

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'
        }
    }
}

答案 1 :(得分:0)

您必须使用USERNAME代替USER

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