Bash script to create a replica set

Hi @Hamza_El_Aouane,

The script layout you have now should in theory at least initiate the replicaSet on the 10.0.1.100 instance, but I could see it failing to add the other instances as the replicaSet might not be ready when those commands are sent to the mongod instance.

My question here is, if you’re just going to add members right away, why not add those members to the initiate() method call?

rs.initiate({
    _id : 'rs0',
    members: [
        {'_id': 0, 'host': '10.0.1.100:27017' },
        {'_id': 1, 'host': '10.0.2.100:27017' },
        {'_id': 2, 'host': '10.0.3.100:27017' }
    ]
})

This will save you some hassle. If you’re trying to make sure that the instance on 10.0.1.100 is the primary, then you can add priority: 2 to the block for that instance and the it would be the primary member as long as it was up and accessible to the other two nodes.

I’m not sure what your ultimate plan is with this bash script so it’s hard to tell you the best way about setting it up.

1 Like