Search code examples
mongodbreplicationdatabase-replicationreplicasetmongodb-replica-set

How to auto config mongodb replica set


I want to create a MongoDB replica set, and according to the documentation I need to run sth like this in my first mongo instance in order to config the replica set and this works fine. However, I was wondering if there is a way to automate this process and don't ssh to the server and run this piece of code every time. I tried putting it in a config file but it didn't work

rs.initiate( {
 _id : "rs0",
 members: [
    { _id: 0, host: "mongodb0.example.net:27017" },
    { _id: 1, host: "mongodb1.example.net:27017" },
    { _id: 2, host: "mongodb2.example.net:27017" }
  ]
})

Solution

  • Are you talking about situation where you create replica set(s) with docker, terraform, ... kind of tools?

    You can always include local init -script what have that rs.initiate(...) command.

    Replica set needs ALWAYS initial initiate -command. Without it, it doesn't exist.