Adding ReplicaSet with PyMongo


I’m attempting to add an additional member to an existing replica set on one of our clusters. I’ve created a data base user that has readWriteAnyDatabase, dbAdminAnyDatabase & clusterMonitor. However, when I go to perform the below command in Pymongo…
config = client.admin.command('replSetGetConfig') I receive the below error…
(Unauthorized) not authorized on admin to execute command.

The role clusterMonitor has been explicitly added to this user. According to the documentation, that should be enough access to run the replSetGetConfig command.

Does anyone know what’s going wrong here?

Full code…

from pymongo import MongoClient
client = MongoClient('mongodb+srv://{dbUserName}:{dbPassword}@{cluster_url}0/{database}?retryWrites=true&w=majority')
config = client.admin.command('replSetGetConfig')
    '_id': 3,
    'host': '{hostname}',
    'priority': 0,
    'hidden': True
client.admin.command('replSetReconfig', config)

The connection to the DB works fine and i’m able to query results etc. I just can’t run the above queries on replSetReconfig.

Is your cluster self hosted or is it running on MongoDB Atlas? MongoDB Atlas does not allow adding replica set members in this way. Replica set config needs to happen through the MongoDB Atlas website UI (or Atlas Administration API).

If this is self hosted then ensure your DB user is created in the admin database. You can verify the app is authenticated as the expected user by running the connectionStatus command:

>>> client.admin.command('connectionStatus', showPrivileges=True)['authInfo']
{'authenticatedUsers': [...], 'authenticatedUserRoles': [...], 'authenticatedUserPrivileges': [...]}

Hi Shane,

We are using MongoDB Atlas - so that’s probably what is happening. I’ll investigate how to add replica sets through the UI or via the Atlas Admin API.

Really appreciate the help!

Hi Shane,

Looking through the documentation (Deploy a Replica Set — MongoDB Cloud Manager, - it’s not clear to me how I can add an additional member to an existing replica set within the Atlas UI. Is it possible to add a member in this way? The steps in the first link appear to be outdated, or my version of Atlas is an older version, ‘Create New Replica Set’ is not an option for me.

Thanks in advance