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".