Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i validate username password for mongodb authentication through pymongo?

I am refering to the http://api.mongodb.org/python/current/examples/authentication.html site for authentication mechanism examples. I have created a User administrator and using its credentials I created a user for my 'reporting' database. Now i need to access the same through pymongo using the username and password. I tried the following commands in python shell. Is this the right way as my authentication is failing.

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
like image 606
Mrunmayee Avatar asked Apr 08 '15 07:04

Mrunmayee


2 Answers

Just adding more to provided solutions.

I have been using as URI connection string and credentials being provided as f string it helps to reduce number of lines. One thing to note is about special characters in password where we convert using urllib package as shown below.

import urllib.parse
from pymongo import MongoClient

host = "localhost"
port = 27017

user_name = "myuser"
pass_word = "Pass@123"  

db_name = "mydb"  # database name to authenticate

# if your password has '@' then you might need to escape hence we are using "urllib.parse.quote_plus()" 
client = MongoClient(f'mongodb://{user_name}:{urllib.parse.quote_plus(pass_word)}@{host}:{port}/{db_name}') 
like image 115
Shakeel Avatar answered Nov 03 '22 09:11

Shakeel


As an FYI, you can use the URI string format as well. The pseudocode looks like this:

pymongo.MongoClient('mongodb://user:password@server:port/')

Here's a simple connection code block with auth:

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 helps = looks like you already have it though. Happy coding!!

like image 25
RandallShanePhD Avatar answered Nov 03 '22 10:11

RandallShanePhD