Pymongo permissions issue for safe inserts

2019-07-26 16:47发布

问题:

I have an instance of Mongo running and can connect and authenticate successfully to a database. I can bulk insert records using collection.insert([list of records to insert]).

However, when I add safe=True to ensure that the records are inserted, like the following command, I get the error below, which seems like a permissions issue. How can I fix this?

    collection.insert(records_to_insert, safe=True)
  File "/.../python2.6/site-packages/pymongo/collection.py", line 270, in insert
    check_keys, safe, kwargs), safe)
  File "/.../python2.6/site-packages/pymongo/connection.py", line 732, in _send_message
    return self.__check_response_to_last_error(response)
  File "/.../lib/python2.6/site-packages/pymongo/connection.py", line 684, in __check_response_to_last_error
    raise OperationFailure(error["err"])
pymongo.errors.OperationFailure: unauthorized

回答1:

You are running in MongoDB in auth mode and did not provide the related the related credentials upon connection time. Calling db.authenticate(...) should be your friend.