Python 如何通过 pymongo 验证用于 mongodb 身份验证的用户名密码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29508162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:40:26  来源:igfitidea点击:

how can i validate username password for mongodb authentication through pymongo?

pythonmongodbauthentication

提问by Mrunmayee

I am refering to the http://api.mongodb.org/python/current/examples/authentication.htmlsite for authentication mechanism examples. I have created a User administrator and using its credentials I created a user for my 'reporting' database. Now i need to access the same through pymongo using the username and password. I tried the following commands in python shell. Is this the right way as my authentication is failing.

我指的是http://api.mongodb.org/python/current/examples/authentication.html站点以获取身份验证机制示例。我创建了一个用户管理员,并使用其凭据为我的“报告”数据库创建了一个用户。现在我需要使用用户名和密码通过 pymongo 访问它。我在 python shell 中尝试了以下命令。由于我的身份验证失败,这是正确的方法吗?

from pymongo import MongoClient

client = MongoClient('localhost')

client.reporting.authenticate('reportsUser', '123456', mechanism='MONGODB-CR')

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/pymongo/database.py", line 746, in authenticate
    self.connection._cache_credentials(self.name, credentials)
  File "/usr/lib/python2.7/dist-packages/pymongo/mongo_client.py", line 441, in _cache_credentials
    auth.authenticate(credentials, sock_info, self.__simple_command)
  File "/usr/lib/python2.7/dist-packages/pymongo/auth.py", line 214, in authenticate
    auth_func(credentials[1:], sock_info, cmd_func)
  File "/usr/lib/python2.7/dist-packages/pymongo/auth.py", line 194, in _authenticate_mongo_cr
    cmd_func(sock_info, source, query)
  File "/usr/lib/python2.7/dist-packages/pymongo/mongo_client.py", line 607, in __simple_command
    helpers._check_command_response(response, None, msg)
  File "/usr/lib/python2.7/dist-packages/pymongo/helpers.py", line 147, in _check_command_response
    raise OperationFailure(msg % errmsg, code)
pymongo.errors.OperationFailure: command SON([('authenticate', 1), ('user', u'reportsUser'), ('nonce', u'f8158a24f1c61650'), ('key', u'14cea216c54b93bae20acd2e076bb785')]) failed: auth failed

回答by RandallShanePhD

As an FYI, you can use the URI string format as well. The pseudocode looks like this:

仅供参考,您也可以使用 URI 字符串格式。伪代码如下所示:

pymongo.MongoClient('mongodb://user:password@server:port/')

pymongo.MongoClient('mongodb://user:password@server:port/')

Here's a simple connection code block with auth:

这是一个带有 auth 的简单连接代码块:

import pymongo
conn = pymongo.MongoClient('mongodb://root:pass@localhost:27017/')
db = conn['database']
coll = db['collection']

There are more options for the query string here: http://docs.mongodb.org/manual/reference/connection-string/

这里有更多的查询字符串选项:http: //docs.mongodb.org/manual/reference/connection-string/

Hope that helps = looks like you already have it though. Happy coding!!

希望有所帮助 = 看起来您已经拥有它了。快乐编码!!