Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB: Can't initiate replica set; 'has data already, cannot initiate set'

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.

like image 478
Mr.Budris Avatar asked Dec 14 '16 21:12

Mr.Budris


People also ask

How does replica set work in MongoDB?

A replica set is a group of mongod instances that maintain the same data set. A replica set contains several data bearing nodes and optionally one arbiter node. Of the data bearing nodes, one and only one member is deemed the primary node, while the other nodes are deemed secondary nodes.

What is MongoDB replica set name?

On MongoDB Atlas project page, select the Deployment link on the left hand side menu. Under Processes tab, with Topology view you should see the replica set name (this should be the default view presented when you click on Deployment ). Generally, the replica set name is the cluster name plus -shard-0 .


1 Answers

When you initialize the full replica set at once, every node should not have data.

When you turn an existing single node into a replica set, its data becomes the replica set data, and then adding additional nodes will replicate data to them wiping out their existing data.

What you should be doing is starting up the nodes, initializing the replica set and then creating users (only on the primary).

the users were ALREADY created on the other servers by a script

This is the issue at the core - the only way this could have been done on an uninitialized member of a replica set is if it was first brought up as non-replica set node, users were added and then it was restarted with the replSet option. That's not the correct sequence to follow. For correct list of steps check the docs.

like image 135
Asya Kamsky Avatar answered Oct 21 '22 20:10

Asya Kamsky