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

  • PRIMAY, SECONDARY
  • 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 http://repo.mongodb.org/apt/ubuntu 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

Restart

sudo service mongod stop

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

rs.initiate()
rs.add("localhost:40001")

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

testset:SECONDARY>rs.slaveOk()

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
testset:SECONDARY>db.sample.items.find({})

We can see result you inserted from master

PRIMARY(mongo service), SECONDARY, ARBITER

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

replication:
  replSetName:testset

testset is replicaset name. You can use own name

If you want to limit ipaddress for aceess

bind_ip=127.0.0.1,192.168.161.100

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

#bind_ip=xxxxx

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

mongo command access port 27019 by default

>config = {_id: 'testSet',
   members: [
      {_id: 0, host: '127.0.0.1:27017'},
      {_id: 1, host: '127.0.0.1:40000'},
      {_id: 2, host: '127.0.0.1:40001', arbiterOnly : true}
   ]
}
>rs.initiate(config)

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

mongo --part 40000
SECONDARY>rs.slaveOk()

Failover

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.

Ref

MongoDBのレプリケーション
Install MongoDB Community Edition on Ubuntu
EC2によるMongoDBのレプリカセットを構築する