Wednesday, May 7, 2014

2 Setup Replication for Mongo DB on Windows

Here are some notes for setting up the replication in Mongo DB. Unlike other DBMS, mongodb’s replication more like the database mirroring . You can  not pick or choice what column get replicate .

 

prerequisite

Configure three Mongo DB run as service. Below are my configure file.

There files are identical except the service name and port number. I use port 27017, 27018 and 27019. The key thing here is the replSet. This is the replication set name. 

MongoDB1

dbpath=D:\mongoDB\data\mongo1
logpath=D:\mongoDB\DBAREPORT\mongo1-server.log
logappend=true
verbose=vvvvv
directoryperdb=true
journal=true
bind_ip=127.0.0.1
port=27017
serviceDescription=mongo1
serviceDisplayName=mongo1
serviceName=mongo1
replSet=mongorep

MongoDB1

dbpath=D:\mongoDB\data\mongo2
logpath=D:\mongoDB\DBAREPORT\mongo2-server.log
logappend=true
verbose=vvvvv
directoryperdb=true
journal=true
bind_ip=127.0.0.1
port=27018
serviceDescription=mongo2
serviceDisplayName=mongo2
serviceName=mongo2
replSet=mongorep

MongoDB3

dbpath=D:\mongoDB\data\mongo3
logpath=D:\mongoDB\DBAREPORT\mongo3-server.log
logappend=true
verbose=vvvvv
directoryperdb=true
journal=true
bind_ip=127.0.0.1
port=27019
serviceDescription=mongo3
serviceDisplayName=mongo3
serviceName=mongo3
replSet=mongorep

From procexp, you can see three service are using the same binary but of course, they all have its own database directory.

image

Because, we have not actually setup the replication yet, therefore we can see below message from the error log.

[rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)

image

Configure Replication object

1. Connect to any of the mogodb

2. Create the replication object

Mongo DB1 is the primary because we set the priority as 10.

Mongo DB3 is arbiter. Its not going to participate in the replication set but only use to vote in order to decide whats the next primary in the event of the failover.

var mongorepConfig={
  _id : "mongorep",  
  members: [
    {
      _id : 0,
      host : 'localhost:27017',
      priority: 10
     },
     {
      _id : 1,
      host : 'localhost:27018'
     },
     {
      _id : 2,
      host : 'localhost:27019',
      arbiterOnly: true
     }
    ]
}

3. Initial the replication object

rs.initiate(mongorepConfig);

image

Done. 

Replication command

rs.help() –> show the help page for all the replication related command

rs.conf()   -> show the replication configure

image

rs.status() –> Show the replication status

image

db.setSlaveOk() –> This is used to read the data from non primary. if we don’t set this, we will get the error while trying to read the data from secondary . error: { "$err" : "not master and slaveOk=false", "code" : 13435 }

image

 

Reference

http://docs.mongodb.org/manual/tutorial/deploy-replica-set/

2 comments:

 

SQL Panda Copyright © 2011 - |- Template created by O Pregador - |- Powered by Blogger Templates