MongoDB replication: How it works
Master slave replication and replica set
MongoDB support both of the two flavor of replication. Replica set is the improved version of master slave replication, it support automatic fail over. When the primary goes down, one of the slaves will automatically be promoted to primary. In a MongoDB replica set, there is a arbiter server responsible for observing the failure and automatically elect a new primary if it happens.
Beside the fail over, since the data in secondary node is always sync with primary node, it can be used to balance rad across replicas. Some operation like backup, build index are expensive operation, to relief the pressure on primary node, these operation can perform on the secondary nodes.
Set up replica on Windows
A simple replica consist of three server: one primary, one secondary, one arbiter. Create data directory for them:
mkdir node1 mkdir node2 mkdir arbiter
Start three server process in different cmd window
mongod --replSet myapp --dbpath /data/node1 --port 40001 mongod --replSet myapp --dbpath /data/node2 --port 40002 mongod --replSet myapp --dbpath /data/arbiter --port 40003
Connect to one of the two nodes and call rs.initiate().
D:\mongodb-win32-i386-2.4.3\bin>mongo --host localhost --port 40001 MongoDB shell version: 2.4.3 connecting to: localhost:40001/test Server has startup warnings: Wed May 08 19:17:36.985 [initandlisten] Wed May 08 19:17:36.985 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary. Wed May 08 19:17:36.985 [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --journal). Wed May 08 19:17:36.985 [initandlisten] ** Note that journaling defaults to off for 32 bit and is currently off. Wed May 08 19:17:36.985 [initandlisten] ** See http://dochub.mongodb.org/core/32bit Wed May 08 19:17:36.985 [initandlisten] > rs.initiate() { "info2" : "no configuration explicitly specified -- making one", "me" : "Z8JQMEW9HCLQDH2:40001", "info" : "Config now saved locally. Should come online in about a minute.", "ok" : 1 } >
Now add the other two nodes:
rs.add("localhost:40002") rs.add("localhost:40003", {arbiterOnly : true })
Check status of replica
myapp:SECONDARY> rs.status() { "set" : "myapp", "date" : ISODate("2013-05-08T12:33:16Z"), "myState" : 2, "syncingTo" : "Z8JQMEW9HCLQDH2:40001", "members" : [ { "_id" : 0, "name" : "Z8JQMEW9HCLQDH2:40001", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 28, "optime" : { "t" : 1368015878, "i" : 1 }, "optimeDate" : ISODate("2013-05-08T12:24:38Z"), "lastHeartbeat" : ISODate("2013-05-08T12:33:14Z"), "lastHeartbeatRecv" : ISODate("2013-05-08T12:33:15Z"), "pingMs" : 0 }, { "_id" : 1, "name" : "Z8JQMEW9HCLQDH2:40002", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 28, "optime" : { "t" : 1368015878, "i" : 1 }, "optimeDate" : ISODate("2013-05-08T12:24:38Z"), "errmsg" : "syncing to: Z8JQMEW9HCLQDH2:40001", "self" : true }, { "_id" : 2, "name" : "Z8JQMEW9HCLQDH2:40003", "health" : 1, "state" : 7, "stateStr" : "ARBITER", "uptime" : 17, "lastHeartbeat" : ISODate("2013-05-08T12:33:15Z"), "lastHeartbeatRecv" : ISODate("2013-05-08T12:33:16Z"), "pingMs" : 0 } ], "ok" : 1 }