Monday, March 24, 2014

MongoDB 2.4 - Replica Set for Testing and Developing

1. start a single mongod as a standalone server

- cd
- mkdir 1
- mongod --dbpath 1 --port 27001 --smallfiles --oplogSize 50 --logpath 1.log --logappend --fork
- mongo --port 27001

2. convert the mongod instance to a single server replica set

- exit
- killall mongod
- mongod --replSet rstest --dbpath 1 --port 27001 --smallfiles --oplogSize 50 --logpath 1.log --logappend --fork
- mongo --port 27001
- Note: use hostname command to check hostname of the server
-

cfg =
{
        "_id" : "rstest",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "localhost:27001"
                }
        ]
}

- rs.initiate(cfg)

3. add two more members to the set

- exit
- cd
- mkdir 2 3
- mongod --replSet rstest --dbpath 2 --port 27002 --smallfiles --oplogSize 50 --logpath 2.log --logappend --fork
- mongod --replSet rstest --dbpath 3 --port 27003 --smallfiles --oplogSize 50 --logpath 3.log --logappend --fork
- mongo --port 27001

3.1.

- cfg = rs.conf()
- cfg.members[1] = { "_id" : 1, "host" : "localhost:27002" }
- cfg.members[2] = { "_id" : 2, "host" : "localhost:27003" }
- rs.reconfig(cfg)

3.2.

- rs.add('localhost:27002')
- rs.add('localhost:27003')

4. retire the first member from the set

- rs.stepDown(300)
- exit
- terminate mongod process for memeber 1
- go to the new primary of the set
- cfg = rs.conf()
- cfg.members.shift()
- rs.reconfig(cfg)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.