1
votes

[![enter image description here][1]][1]Hi im trying to connect to mongoDB via a python code

client = MongoClient("mongodb://username:mypassword@localhost/mydatabase-1")
db = client['mydatabase-1']

and or

connection = MongoClient('localhost', 27017)
db = connection['mydatabase-1']
db.authenticate('username', 'mypassword')

uri = "mongodb://username:[email protected]/mydatabase-1"
client = MongoClient(uri)

but i keep getting the same error without fail

ConfigurationError: command SON([('authenticate', 1), ('user', u'CEI'), ('nonce', u'950483ef6634f14'), ('key', u'76f0102585a7571d96dace50d7aca5a6')]) failed: auth failed

I am using pymongo 2.7 with python 2.7. I guess this means I'm working with mechanism='MONGODB-CR'

I am able to use these credentials to connect to the MongoDB database using MongoChef (a GUI) and it works perfectly.

I have tried to update the pymongo, and it is at version 2.7 i cant use a higher version because I have python 2.7. I cant find any more solutions online. From what i read on the mongoDB website it should work with mechanism='MONGODB-CR' but not for mechanism='SCRAM-SHA-1' due to being v2.7

have mongodb running using

E:\MongoDB\bin\mongod.exe --auth --port 27017 --dbpath E:\Data\db

and set up the user using

use admin
db.createUser(
  {
    user: "username",
    pwd: "mypassword",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

UPDATE!!!!!!!!!!!!!!!!!!!!!!!!!!

managed to get pymongo 3.4 working using pip install. But still getting this message

runfile('C:/Users/mdb_emr_syn/untitled0.py', wdir='C:/Users/mdb_emr_syn')
Traceback (most recent call last):

  File "<ipython-input-19-2ecc66a5cadd>", line 1, in <module>
    runfile('C:/Users/mdb_emr_syn/untitled0.py', wdir='C:/Users/mdb_emr_syn')

  File "C:\Program Files\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)

  File "C:\Program Files\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 87, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)

  File "C:/Users/mdb_emr_syn/untitled0.py", line 11, in <module>
    connection.yo.authenticate("CEI","Syn12345.")

  File "C:\Program Files\Anaconda2\lib\site-packages\pymongo\database.py", line 1048, in authenticate
    connect=True)

  File "C:\Program Files\Anaconda2\lib\site-packages\pymongo\mongo_client.py", line 505, in _cache_credentials
    sock_info.authenticate(credentials)

  File "C:\Program Files\Anaconda2\lib\site-packages\pymongo\pool.py", line 523, in authenticate
    auth.authenticate(credentials, self)

  File "C:\Program Files\Anaconda2\lib\site-packages\pymongo\auth.py", line 470, in authenticate
    auth_func(credentials, sock_info)

  File "C:\Program Files\Anaconda2\lib\site-packages\pymongo\auth.py", line 450, in _authenticate_default
    return _authenticate_scram_sha1(credentials, sock_info)

  File "C:\Program Files\Anaconda2\lib\site-packages\pymongo\auth.py", line 201, in _authenticate_scram_sha1
    res = sock_info.command(source, cmd)

  File "C:\Program Files\Anaconda2\lib\site-packages\pymongo\pool.py", line 419, in command
    collation=collation)

  File "C:\Program Files\Anaconda2\lib\site-packages\pymongo\network.py", line 116, in command
    parse_write_concern_error=parse_write_concern_error)

  File "C:\Program Files\Anaconda2\lib\site-packages\pymongo\helpers.py", line 210, in _check_command_response
    raise OperationFailure(msg % errmsg, code, response)

OperationFailure: Authentication failed.

any ideas as to why????? thanks guys

2
I'm not sure whether it's a typo in your question, but you have a . (dot) in your URI password value. If it's a typo, please update your question. Thanks.Wan Bachtiar

2 Answers

3
votes

I am totally new in MongoDB, and I've just been through this.

Hope this help for others like me.

# version info

mongodb : 3.4.7
python  : 2.7
pymongo : 3.5.1

In my case, the reason I got Authentication failed is that I didn't have any user created in the db which I wanted to access.

I can check user with db.getUsers() in mongodb shell.

> use mydatabase-1
switched to db mydatabase-1
> db.getUsers()
[ ]

And it's empty.

So what I learned was :

After created a db and put some docs into collection, should also createUser() for that db, than pymongo can access.

In mongodb shell, continue with the example from original post.

db name: mydatabase-1

> use mydatabase-1
switched to db mydatabase-1
> db.createUser(
...  {
...    user: "username",
...    pwd: "myPWD",
...    roles: [ { role: "readWrite" } ]
...  }
...)
Successfully added user: { "user" : "username", "roles" : [ "readWrite" ] }

And than, In python

To access your db, do this...

client = MongoClient("mongodb://username:myPWD@localhost:port/mydatabase-1")
db = client['mydatabase-1']

Or this

connection = MongoClient('localhost', port)
db = connection['mydatabase-1']
db.authenticate('username', 'myPWD')

And than, print doc from collection my_collection_name

cursor = db.my_collection_name.find()
for doc in cursor:
    print doc

This should solved Authentication failed.

0
votes

Upgrade PyMongo. Just because you're using Python 2.7 doesn't mean you have to use PyMongo 2.7. PyMongo 3.4.0 is the latest release, and it works with all versions of Python.