【问题标题】:pymongo.errors.OperationFailure: Authentication failedpymongo.errors.OperationFailure:身份验证失败
【发布时间】:2020-07-24 13:41:14
【问题描述】:

我正在尝试在 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.

【问题讨论】:

  • 您提供的凭据是否正确 mongo 数据库密码?
  • 是的,密码正确我检查了三次。
  • 试试这个DATABASES = {{ 'default': { 'NAME': 'DB_NAME', 'AUTH_SOURCE': 'DB_NAME', 'USER': '&lt;username&gt;', 'PASSWORD': '&lt;password&gt;', 'HOST': '&lt;code&gt;.mlab.com', 'PORT': &lt;port&gt;, 'ENGINE': 'djongo' } }}
  • 我应该在 AUTH_SOURCE 中放什么?
  • 您的数据库名称,用于 django 项目的数据库。确保您可以使用您的用户名和密码更正此数据库

标签: django mongodb


【解决方案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'
        }
    }
}

【讨论】:

  • 那行不通! ``` django.core.exceptions.ImproperlyConfigured: 'djongo' 不是可用的数据库后端或无法导入。检查上述异常。要使用内置后端之一,请使用“django.db.backends.XXX”,其中 XXX 是以下之一:“mysql”、“oracle”、“postgresql”、“sqlite3”```
【解决方案2】:

您必须使用 USERNAME 而不是 USER

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

【讨论】:

    猜你喜欢
    • 2020-11-14
    • 2019-12-17
    • 2014-01-24
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    相关资源
    最近更新 更多