mongodb replica set
mongodb replica set allows you to have a backed up data in case one of your server crashes. When data is written to a primary node, the other secondary node will get the same copy of the data that was written to the primary node. Here is how to create replica nodes in mongodb.
Create data directory for each of the mongo replica.
[code language=”shell”]
mkdir -p /data/rs1 /data/rs2 /data/rs3
[/code]
Start 3 mongod instances.
[code language=”shell”]
mongod –replSet mongodb123 –logpath "/data/rs1.log" –dbpath /data/rs1 –port 27017 –fork
mongod –replSet mongodb123 –logpath "/data/rs2.log" –dbpath /data/rs2 –port 27018 –fork
mongod –replSet mongodb123 –logpath "/data/rs3.log" –dbpath /data/rs3 –port 27019 –fork
[/code]
Connect to mongo instance on port 27017, configure the replication settion, add a test collection and exit.
[code language=”javascript”]
mongo –port 27017
config = { _id: "mongodb123", members:[
{ _id : 0, host : "localhost:27017"},
{ _id : 1, host : "localhost:27018"},
{ _id : 2, host : "localhost:27019"} ]
};
rs.initiate(config);
rs.status()
use test;
db.dummycollection.insert({a:1,b:2,c:3});
db.dummycollection.find();
exit
[/code]
Connect to the other two mongodb replica and verify the test collection dummycollection has the data that was inserted in the above step.
[code language=”javascript”]
mongo –port 27018
rs.slaveOk();
use test;
db.dummycollection.find();
exit
mongo –port 27019
rs.slaveOk();
use test;
db.dummycollection.find();
exit
[/code]
Mongodb command help.
[code language=”shell”]
mongo –help
mongod help
help #inside mongo shell
[/code]
Search within Codexpedia

Search the entire web
