Cant connect to Mongodb using python

Hello! Can you help me with problem?
I use connection like this:
uri = ‘mongodb://myUsername:myPassword@hostname/?tls=True’
connect = MongoClient(uri)
db = connect.сallсenter
collection = db.Journal

And next after any request i have an error.
For example:

collection = db.Journal
c = collection.find()
for r in c:

An error:
OperationFailure: not authorized on сallсenter to execute command…

At the same time I can connet to this database using MongoDBCompass. So connection data is correct. Why I cant connect using python?

The error message tells you that thus user is not authorised to do read operations on the database callcenter. Check the users roles and privileges.

Thank you. But I can connect to database using MongoDBCompass. I use the same username and password. So I can read data
Just now I was able to connect to the database using clicksense. But i still can’t connect using python

Post screenshots of doing so.

Same user with same password on same database of the same server should be able to do the same thing.

Here it is

This is a but different from your original post. You are not connecting to a standalone instance like hinted by your original URI. You are connecting to a replica set. The issue might simply be because you are not specifying the replica set name in python. You seem to do it with Compass.

Check syntax at

If your credentials are correct, then try restarting Jupyter’s python kernel. It is possible you are forgetting to execute the connection cell after you edit URI, or variables just got stuck somehow and won’t change without a restart.

I have added replicaSet but nothing changed.

uri = ‘mongodb://callcenter:password@example6:27017,example7:27017,example8:27017/callcenter?readPreference=secondary&authSource=admin&replicaSet=rs0’
connect = MongoClient(uri)
db = connect.сallсenter

I still have an error: not authorized on сallсenter to execute command

Maybe you know another reasons why i cant connect?

Thank you. But it didnt help

here is the thing: you are either using the wrong authSource, connecting to wrong database or this “callcenter” user does not have access to callcenter database.

first, make sure you are not having a typo in your connection string.

use authSource=callcenter and try again.

do you have admin right on that database? check the output of these commands to see what access rights this “callcenter” user has

use admin
use callcenter

I checked the connection string. It`s correct. With this conection string I can connect to db using QlikSense. And I connected to db using MongoDBCompass using this string

Just discovered that if I specify only one server, not three, then everything works fine. So, the problem is that I wrote three servers. How can I specify three servers in one connection string? Maybe you know some features?

if there is no typo, your above URI string should have no problem connecting to a replica set.

The error you are getting is about the auth. It is possible your user is not set up correctly and one or more replica members do not know how to deal with it. or maybe the replica set is not set up correctly.

You said you can connect to one of them if you use single hostname. Can you try to connect each of the members separately?

Try removing

Do the following on each host individually:

One more thing is to ensure you are connecting to the same machines. You seem to be running off a 10.* network. I have seen many times that the same 10.* IPs are not referring to the same machines. When connection from a test gateway you reach test machines and from the prod. gateway you reach prod. machine with the exact same IPs.


In principal the same URI refers to the same user, same password, same database and same server/cluster. With all the redacting you are doing with the URI you share it is very hard to spot what differs from one application to the other. Unless there is a major bug in one of the driver where it sends the wrong credentials, you are the same user in the server with the same provileges.

1 Like