简体   繁体   中英

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM