I've got a MongoDB instance, set up using a config file and a key file.
I'd like to initiate a replica set using pymongo. When I attempt to initiate the replcia set, by executing a python script against the server which will become the replica set primary, as such:
from pymongo import MongoClient
uri = "mongodb://correctWorkingUsername:password@localhost:27017"
c = MongoClient(uri)
config = {'_id': 'RelicaSetName', 'members': [
{'_id': 0, 'host': 'FirstServer:27017'},
{'_id': 1, 'host': 'SecondServer:27017'},
{'_id': 2, 'host': 'ThirdServer:27017'}]}
c.admin.command("replSetInitiate", config)
I get an error message, the following:
'SecondSErver:27017' has data already, cannot initiate set
However, if I authenticate to the database using
mongo admin -u correctWorkingUsername -p password
I can initiate the replication, and successfully add members:
rs.initiate()
rs.add('SecondServer:27017')
I was unsure if this was related to the keyfile authentication, or the fact that the users were ALREADY created on the other servers by a script. Each server has also been started with a config file, mongod.conf, which contains a replica set name.
Why is this failing? The rs.initiate() and rs.add() work perfectly, but the python script does not work though it can infact connect to the database.