Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

mongodb - pymongo auth failed in python script

I have installed mongodb and enabled auth. and its working find. I can connect it from remote notebook using robomongo application:

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:

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Please try something like this:

client = MongoClient("mongodb://user_name:user_password@SERVER_IP/prod-db")
db = client['prod-db']

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...