0

I'm new to Python and I'm trying to connect to a MongoDB ReplicaSet, but when I try to connect to the database I get the following error in the "for x in mydoc" statement:

File "C:\Users\Nahuel Gabioud\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\helpers.py", line 159, in _check_command_response raise OperationFailure(msg % errmsg, code, response) pymongo.errors.OperationFailure: Authentication failed.

The code It's the following

import pymongo
import urllib

password = urllib.parse.quote_plus("moica2$PerformanceGGR!!")
try: 
    myclient = pymongo.MongoClient("mongodb://performance:"+password+"@db-mdb-30.serverurl:27017,db-mdb-31.serverurl:27017,db-mdb-32.serverurl:27017/?authMechanism=SCRAM-SHA-256&replicaSet=rs30&readPreference=primaryPreferred")
    print("Connected successfully!!!") 
except:   
    print("Could not connect to MongoDB") 
mydb = myclient["moica2"]
mycol = mydb["moicaTickets"]

myquery = {"nroTkt": "190701PD0100"}

mydoc = mycol.find(myquery)

for x in mydoc:
  print(x)

input()

For security reasons, I didn't post the real url and used "serverurl" instead.

As you can see, I'm using the connection string as in https://docs.mongodb.com/manual/reference/connection-string/#standard-connection-string-format

The exception is not caught in the try and except. The error appears in "for x in mydoc".

0

2 Answers 2

2

Authentication failed would lead you to believe that the ID / Password is incorrect. If you're behind a corporate firewall, though, it might be a connectivity issue and a poor error message.

I'd suggest downloading / installing Compass and see if that connects with the same connection string / credentials.

https://www.mongodb.com/products/compass

if it does work, I'd suggest this line

urllib.parse.quote_plus("moica2$PerformanceGGR!!")

is doing something to change the password, and that is where it is failing.

P.s. I'd edit the password out of your question!

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Allan for your answer. I use NoSQL Manager for MongoDB and it's connecting properly. I tried with to use the password with quote_plus and without it. Although, I can tell you that if I want to import the url in NoSQL Manager I get the following error: "Valid port expected. at line: 1, column: 23."
Ok, I tried on Compass and you said. First I couldn't connect because the field "Authentication Database" was set to "admin" and it should be "moica2". I edited that and connected properly
I added "&authSource=moica2" and it worked. Thanks!
Oh man, I just searched half a day why I cannot authenticate! And now I know that it's just because I haven't encoded the "+"... Thanks a lot for your answer!
1

You have to mention like this way:

client = MongoClient('X.X.X.X',port=27017,username='iamlogin', password='abcd',authSource='xyz_db')

authSource of your username accessible database name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.