首页 > 解决方案 > 尝试在 mongo 游标上循环时,Pymongo 身份验证失败

问题描述

我设法让这个工作。

我正在尝试遍历从查询到远程 mongo 的响应中的文档。我可以连接到 mongo 并使用返回的游标对象运行查询。但是,当我尝试执行除打印类型以外的任何操作时,我得到了似乎是身份验证错误的信息。

import pymongo
import bson
import sys
from bson import ObjectId
try:
    client = pymongo.MongoClient("mongodb://<user>:<password>@<host>:<port>")
    db=client.get_database('<database name>')
    result=db.listings.find({'import_id':ObjectId('54de51bf7a40554a06000002'),'deleted_at':None})

except pymongo.errors.ServerSelectionTimeoutError as err:
    print('mongo login failed')
    print(err)
    sys.exit()

print type(result)
print str(result)
for item in result:
    print 'a'
    break
client.close()

Python 2.7.10 Pymongo 3.0.0 mongo 2.6

这是我得到的堆栈跟踪:

<class 'pymongo.cursor.Cursor'>
<pymongo.cursor.Cursor object at 0x1035c4fd0>
Traceback (most recent call last):
  File "feed_id_shove.py", line 25, in <module>
    print result.count()
  File "/Library/Python/2.7/site-packages/pymongo/cursor.py", line 673, in count
    return self.__collection.count(**options)
  File "/Library/Python/2.7/site-packages/pymongo/collection.py", line 1009, in count
    with self._socket_for_reads() as (sock_info, slave_ok):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "/Library/Python/2.7/site-packages/pymongo/mongo_client.py", line 687, in _socket_for_reads
    with self._get_socket(read_preference) as sock_info:
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "/Library/Python/2.7/site-packages/pymongo/mongo_client.py", line 653, in _get_socket
    with server.get_socket(self.__all_credentials) as sock_info:
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "/Library/Python/2.7/site-packages/pymongo/server.py", line 102, in get_socket
    with self.pool.get_socket(all_credentials, checkout) as sock_info:
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "/Library/Python/2.7/site-packages/pymongo/pool.py", line 511, in get_socket
    sock_info.check_auth(all_credentials)
  File "/Library/Python/2.7/site-packages/pymongo/pool.py", line 276, in check_auth
    auth.authenticate(credentials, self)
  File "/Library/Python/2.7/site-packages/pymongo/auth.py", line 406, in authenticate
    auth_func(credentials, sock_info)
  File "/Library/Python/2.7/site-packages/pymongo/auth.py", line 388, in _authenticate_default
    return _authenticate_mongo_cr(credentials, sock_info)
  File "/Library/Python/2.7/site-packages/pymongo/auth.py", line 381, in _authenticate_mongo_cr
    sock_info.command(source, query)
  File "/Library/Python/2.7/site-packages/pymongo/pool.py", line 184, in command
    codec_options, check, allowable_errors)
  File "/Library/Python/2.7/site-packages/pymongo/network.py", line 54, in command
    helpers._check_command_response(response_doc, msg, allowable_errors)
  File "/Library/Python/2.7/site-packages/pymongo/helpers.py", line 188, in _check_command_response
    raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: command SON([('authenticate', 1), ('user', u'admin'), ('nonce', u'cab3611fe12663d6'), ('key', u'4f9230ab8b035b389dcf735cfbf9f0e8')]) on namespace placester_production.$cmd failed: auth fails

当我尝试循环光标而不是更早时,为什么它会给我“身份验证失败”?

标签: pythonmongodbpymongo

解决方案


我发现了我的问题。使用

try:
    # The ismaster command is cheap and does not require auth.
    client.admin.command('ismaster')
except:
    print("Server not available")

我发现我实际上并没有连接到我的数据库。这帮助我找到了我的用户名和凭据中的错字。


推荐阅读