pymongo auth在python脚本中失败

时间:2016-10-31 16:51:17

标签: python mongodb pymongo

我已经安装了mongodb并启用了auth。和它的工作发现。我可以使用robomongo应用程序从远程笔记本连接它:

Host: SERVER_IP
PORT: 27017
DATEBASE: prod-db
USERNAME: user_name
PASS: user_password
Auth Mechanism: MONGODB-CR

我们可以使用以下方式从本地服务器shell连接:

$ mongo prod-db -u user_name -p user_password

一切正常,但是当我们尝试使用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)

使用的工具:

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

错误追踪:

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)

解决方案: 问题是数据库名称,以下代码工作正常:

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

4 个答案:

答案 0 :(得分:28)

请尝试这样的事情:

{{1}}

答案 1 :(得分:5)

如果您尝试了上述答案,但仍然出现错误:

pymongo.errors.OperationFailure: Authentication failed.

您很有可能需要在您的uri末尾添加?authSource=admin

这是我在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)

类似的命令行修复程序正在添加--authenticationDatabase admin

答案 2 :(得分:1)

这对我有用....

import pymongo

client = pymongo.MongoClient("mongodb://username:12%40password@ip:27017/sample_db",authSource="admin") 
db = client['sample_db']

请记住,如果密码中包含特殊字符(例如#,@),则需要对密码进行编码(请参见%40)。如果不执行authSource =“ admin”,将收到身份验证错误。 username-您的mongodb用户名,ip-ip地址,因为它假定数据库托管在远程服务器上。 sample_db是您要访问的数据库。

答案 3 :(得分:0)

对于pymongo,

尝试以下MongoDB 4

添加authSource:这是具有用户凭据集合的数据库的名称。

例如:

client = MongoClient(host=<<hostname>>,
                     port=<<port>>, 
                     username=<<user_name>>, 
                     password=<<password>>,
                    authSource="admin")
db_obj = client[db_name]