pymongo gridfs mongodb 身份验证失败

pymongo gridfs mongodb authentication fails

我正在使用 gridfs 从我的 MongoDB 数据库

存储和获取文件
fs = gridfs.GridFS(MongoClient('mongodb://username:password@XX.XX.XX.XXX:27017/').mydb)

并获取我正在使用的文件

file = fs.get_last_version(file_name).read()

在这一行我得到身份验证失败异常。

Traceback (most recent call last):
  File "C:\Python36\lib\site-packages\flask\app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Python36\lib\site-packages\flask\app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Python36\lib\site-packages\flask\app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python36\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "C:\Python36\lib\site-packages\flask\app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Python36\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Python36\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python36\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "C:\Python36\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Python36\lib\site-packages\flask\app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "D:\git\EEG\ABM\BLabPythonService\app.py", line 1039, in start_processing
    sys.stderr.flush()
  File "D:\git\EEG\ABM\BLabPythonService\process_studies.py", line 271, in start_processing
    overlay_artifacts(study)
  File "D:\git\EEG\ABM\BLabPythonService\process_studies.py", line 49, in overlay_artifacts
    edf_file = fs.get_last_version(file_name).read()
  File "C:\Python36\lib\site-packages\gridfs\__init__.py", line 200, in get_last_version
    return self.get_version(filename=filename, **kwargs)
  File "C:\Python36\lib\site-packages\gridfs\__init__.py", line 184, in get_version
    grid_file = next(cursor)
  File "C:\Python36\lib\site-packages\pymongo\cursor.py", line 1132, in next
    if len(self.__data) or self._refresh():
  File "C:\Python36\lib\site-packages\pymongo\cursor.py", line 1055, in _refresh
    self.__collation))
  File "C:\Python36\lib\site-packages\pymongo\cursor.py", line 892, in __send_message
    **kwargs)
  File "C:\Python36\lib\site-packages\pymongo\mongo_client.py", line 950, in _send_message_with_response
    exhaust)
  File "C:\Python36\lib\site-packages\pymongo\mongo_client.py", line 961, in _reset_on_error
    return func(*args, **kwargs)
  File "C:\Python36\lib\site-packages\pymongo\server.py", line 99, in send_message_with_response
    with self.get_socket(all_credentials, exhaust) as sock_info:
  File "C:\Python36\lib\contextlib.py", line 81, in __enter__
    return next(self.gen)
  File "C:\Python36\lib\site-packages\pymongo\server.py", line 168, in get_socket
    with self.pool.get_socket(all_credentials, checkout) as sock_info:
  File "C:\Python36\lib\contextlib.py", line 81, in __enter__
    return next(self.gen)
  File "C:\Python36\lib\site-packages\pymongo\pool.py", line 852, in get_socket
    sock_info.check_auth(all_credentials)
  File "C:\Python36\lib\site-packages\pymongo\pool.py", line 570, in check_auth
    auth.authenticate(credentials, self)
  File "C:\Python36\lib\site-packages\pymongo\auth.py", line 486, in authenticate
    auth_func(credentials, sock_info)
  File "C:\Python36\lib\site-packages\pymongo\auth.py", line 466, in _authenticate_default
    return _authenticate_scram_sha1(credentials, sock_info)
  File "C:\Python36\lib\site-packages\pymongo\auth.py", line 237, in _authenticate_scram_sha1
    res = sock_info.command(source, cmd)
  File "C:\Python36\lib\site-packages\pymongo\pool.py", line 477, in command
    collation=collation)
  File "C:\Python36\lib\site-packages\pymongo\network.py", line 116, in command
    parse_write_concern_error=parse_write_concern_error)
  File "C:\Python36\lib\site-packages\pymongo\helpers.py", line 210, in _check_command_response
    raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: Authentication failed.

当我的 mongo 数据库没有身份验证时一切正常。

数据库的其他使用工作正常。我有一个使用 flask_pymogno 连接到数据库的 python 服务和一个也使用 c# 连接到数据库的 c# 应用程序,并且所有连接都使用相同的凭据。

解决了。问题实际上是在我的密码 中包含“+”字符。它是一个保留的 URI 字符。通过将“+”替换为“%2B”来修复此问题,现在可以使用了。

参考: https://en.wikipedia.org/wiki/Percent-encoding