Tuesday, November 5, 2013

Build MongoDB Replica Set

Prepare DB Directories

mkdir -p /data/rs1 /data/rs2 /data/rs3



Configure Replica Set

Now start three mongo instances as follows. Note that are three commands. The browser is probably wrapping them visually.

./mongod --replSet ing --logpath "1.log" --dbpath /data/rs1 --port 27017 --smallfiles --fork
./mongod --replSet ing --logpath "2.log" --dbpath /data/rs2 --port 27018 --smallfiles --fork
./mongod --replSet ing --logpath "3.log" --dbpath /data/rs3 --port 27019 --smallfiles --fork

Now connect to a mongo shell and make sure it comes up

./mongo --port 27017

Now you will create the replica set. Type the following commands into the mongo shell:

config = { _id: "ing", members:[
          { _id : 0, host : "localhost:27017"},
          { _id : 1, host : "localhost:27018"},
          { _id : 2, host : "localhost:27019"} ]
};
rs.initiate(config);

At this point, the replica set should be coming up. You can type

rs.status()

to see the state of replication.

config = { _id: "ing", members:[
          { _id : 0, host : "localhost:27001"}
]
};
rs.initiate(config);

Other Replicaset Operation


rs.add("localhost:27002")
rs.add("localhost:27003")

rs.remove("localhost:27001")

No comments:

Post a Comment