pymongo 身份验证在 python 脚本中失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40346767/
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
pymongo auth failed in python script
提问by sharafjaffri
I have installed mongodb and enabled auth. and its working find. I can connect it from remote notebook using robomongo application:
我已经安装了 mongodb 并启用了身份验证。及其工作发现。我可以使用 robomongo 应用程序从远程笔记本连接它:
Host: SERVER_IP
PORT: 27017
DATEBASE: prod-db
USERNAME: user_name
PASS: user_password
Auth Mechanism: MONGODB-CR
and We can connect from server shell locally using:
我们可以使用以下方法从本地服务器外壳连接:
$ mongo prod-db -u user_name -p user_password
Everything works fine, but when we try it using pymongo api. authentications failed. below is the python code:
一切正常,但是当我们尝试使用 pymongo api 时。身份验证失败。下面是python代码:
from pymongo import MongoClient
client = MongoClient()
client.prod_db.authenticate('user_name', 'user_password', mechanism='MONGODB-CR')
db = client.prod_db
result = db.users.find()
for document in result:
print(document)
Tools used:
使用的工具:
python 2.7
pymongo versiob 3.3.1
MongoDB shell version: 2.6.10
$ mongod --version
db version v2.6.10
2016-10-31T16:34:59.868+0000 git version: nogitversion
2016-10-31T16:34:59.868+0000 OpenSSL version: OpenSSL 1.0.2g 1 Mar 2016
Error trace:
错误跟踪:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/pymongo/database.py", line 1018, in authenticate
connect=True)
File "/usr/local/lib/python2.7/dist-packages/pymongo/mongo_client.py", line 444, in _cache_credentials
sock_info.authenticate(credentials)
File "/usr/local/lib/python2.7/dist-packages/pymongo/pool.py", line 343, in authenticate
auth.authenticate(credentials, self)
File "/usr/local/lib/python2.7/dist-packages/pymongo/auth.py", line 464, in authenticate
auth_func(credentials, sock_info)
File "/usr/local/lib/python2.7/dist-packages/pymongo/auth.py", line 439, in _authenticate_mongo_cr
sock_info.command(source, query)
File "/usr/local/lib/python2.7/dist-packages/pymongo/pool.py", line 239, in command
read_concern)
File "/usr/local/lib/python2.7/dist-packages/pymongo/network.py", line 102, in command
helpers._check_command_response(response_doc, None, allowable_errors)
File "/usr/local/lib/python2.7/dist-packages/pymongo/helpers.py", line 205, in _check_command_response
raise OperationFailure(msg % errmsg, code, response)
Solution:problem was with database name, following code works fine:
解决方案:问题出在数据库名称上,以下代码工作正常:
from pymongo import MongoClient
client = MongoClient('mongodb://user_name:user_password@localhost:27017/prod-db')
db = client['prod-db']
result = db.users.find()
for document in result:
print document
回答by milos.ai
Please try something like this:
请尝试这样的事情:
client = MongoClient("mongodb://user_name:user_password@SERVER_IP/prod-db")
db = client['prod-db']
回答by Harsha Biyani
For pymongo,
对于皮蒙戈,
Try below for MongoDB 4
:
试试下面的MongoDB 4
:
Add authSource
: This is the name of the database that has the collection with the user credentials.
添加authSource
:这是包含用户凭据集合的数据库的名称。
Ex:
前任:
client = MongoClient(host=<<hostname>>,
port=<<port>>,
username=<<user_name>>,
password=<<password>>,
authSource="admin")
db_obj = client[db_name]
Edit 1: I have tried this on Mongo 3.x version as well. Working for that too.
编辑 1:我也在 Mongo 3.x 版本上尝试过这个。也为此工作。
回答by Nathan
If you've tried the above answers and you're still getting an error:
如果您已经尝试了上述答案,但仍然出现错误:
pymongo.errors.OperationFailure: Authentication failed.
There's a good chance you need to add ?authSource=admin
to the end of your uri.
您很有可能需要添加?authSource=admin
到 uri 的末尾。
Here's a working solution that I'm using with MongoDB server version 4.2.6 and MongoDB shell version v3.6.9.
这是我与 MongoDB 服务器版本 4.2.6 和 MongoDB shell 版本 v3.6.9 一起使用的工作解决方案。
from pymongo import MongoClient
# Replace these with your server details
MONGO_HOST = "XX.XXX.XXX.XXX"
MONGO_PORT = "27017"
MONGO_DB = "database"
MONGO_USER = "admin"
MONGO_PASS = "pass"
uri = "mongodb://{}:{}@{}:{}/{}?authSource=admin".format(MONGO_USER, MONGO_PASS, MONGO_HOST, MONGO_PORT, MONGO_DB)
client = MongoClient(uri)
Similar fix for command line is adding --authenticationDatabase admin
命令行的类似修复正在添加 --authenticationDatabase admin