MongoDB Replication

How to set up replication for MongoDB

Server Structure

Actually, 3 instance should be separate server, so we need to prepare at least 3 servers for it.
But, in this time, 3 instances are same server for testing.
We can make 3 instances on the same server(data files are different)
According to situation, we have 2 cases

  • PRIMARY(mongo service), SECONDARY, ARBITER

I use Ubuntu 15.10.
/etc/mongod.conf is configuration name

MongoDB replication role

Install mongo(Community Edition)

In Ubuntu, it prepares distribution edition and this is easy to install
Install MongoDB Community Edition on Ubuntu

echo "deb trusty/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
sudo apt-get update
sudo apt-get install -y mongodb-org

Prepare for services

sudo apt-get install upstart-sysv

Start mongo service

sudo service mongod start


sudo service mongod stop


sudo service mongod stop

mongodb works with mongodb command

PRIMAY, SECONDARY by mongod command

We can run multiple mongod instance on the same server.
So, I run 2 instances on the same server with different data.
1. Start 2 instances with different port and data, and same replSet

mongod --replSet testset --dbpath /var/lib/primary --port 40000 
mongod --replSet testset --dbpath /var/lib/secondary --port 40001

If you want to work with background, please add &

2. Access primary with mongo shell

mongo --port 40001

Access primary first.
3. Set replication


localhost is machine name, we can use IP address, or machine(hostname)
After it, switch to secondary


run slaveOk command, we can run mongo query.

How to check?
Please access primary and insert test data

mongo --port 40000
testset:PRIMARY>use sample
testset:PRIMARY>db.sample.items.insert({title: 'Hello Mongo!'})

Insert one item to sample database, items collection

Access to slave and run query to check data

mongo --port 40001
testset:SECONDARY>use sample

We can see result you inserted from master


Set replicaset name in /etc/mongod.conf for services
Please open file and add replicaset settings


testset is replicaset name. You can use own name

If you want to limit ipaddress for aceess


You can add
If you don’t have any limitation, you can comment out.


This means any machine can access.

1. Start 1 instance with mongo service
repliset is already set just start

sudo service mongod start

2. Start 2 instance with mongod command(1 is secondary, 1 is arbiter

mongod --replSet testset --dbpath /var/lib/secondary --port 40000
mongod --replSet testset --dbpath /var/lib/arbiter --port 40001

respSet is same, dbpath is different(dbpath is mondo data path, if this is blank, create new dataset)

3. Access primary and set replication
By default, mongo port is 27019


mongo command access port 27019 by default

>config = {_id: 'testSet',
   members: [
      {_id: 0, host: ''},
      {_id: 1, host: ''},
      {_id: 2, host: '', arbiterOnly : true}

To run query from SECONDARY, you need to run slaveOk()

mongo --part 40000


By default, mongodb replication has failover features.
If you kill process, secondary works with primary

kill -9 xxxxx

xxxxs is process ID for primary mongod
Next, you restart failed one and it becomes secondary.


Install MongoDB Community Edition on Ubuntu