How can I check the user password for mongodb authentication through pymongo?

I refer to the http://api.mongodb.org/python/current/examples/authentication.html site for examples of the authentication mechanism. I created a user admin and used his credentials. I created a user for my reporting database. Now I need to access the same via pymongo using username and password. I tried the following commands in python shell. Is this correct as my authentication does not work.

from pymongo import MongoClient client = MongoClient('localhost') client.reporting.authenticate('reportsUser', '123456', mechanism='MONGODB-CR') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.7/dist-packages/pymongo/database.py", line 746, in authenticate self.connection._cache_credentials(self.name, credentials) File "/usr/lib/python2.7/dist-packages/pymongo/mongo_client.py", line 441, in _cache_credentials auth.authenticate(credentials, sock_info, self.__simple_command) File "/usr/lib/python2.7/dist-packages/pymongo/auth.py", line 214, in authenticate auth_func(credentials[1:], sock_info, cmd_func) File "/usr/lib/python2.7/dist-packages/pymongo/auth.py", line 194, in _authenticate_mongo_cr cmd_func(sock_info, source, query) File "/usr/lib/python2.7/dist-packages/pymongo/mongo_client.py", line 607, in __simple_command helpers._check_command_response(response, None, msg) File "/usr/lib/python2.7/dist-packages/pymongo/helpers.py", line 147, in _check_command_response raise OperationFailure(msg % errmsg, code) pymongo.errors.OperationFailure: command SON([('authenticate', 1), ('user', u'reportsUser'), ('nonce', u'f8158a24f1c61650'), ('key', u'14cea216c54b93bae20acd2e076bb785')]) failed: auth failed 
+6
source share
1 answer

Like FYI, you can also use the URI string format. The pseudocode is as follows:

<i> pymongo.MongoClient ('MongoDB: // user: password @ server: port /')

Here is a simple block of auth connection code:

 import pymongo conn = pymongo.MongoClient('mongodb://root: pass@localhost :27017/') db = conn['database'] coll = db['collection'] 

There are more options for the query string here: http://docs.mongodb.org/manual/reference/connection-string/

Hope that help = looks like you already have this. Happy coding!

+8
source

Source: https://habr.com/ru/post/984833/


All Articles