Home > MongoDB > Convert a Standalone MongoDB to a Replica Set

Convert a Standalone MongoDB to a Replica Set

I already have standalone mongod instance (called svr01) and want to convert into four member replica set (1 primary, 2 secondary, 1 arbiter). My objective is to distribute read query through secondaryPreferred read preference.

Create siteRootAdmin user to manage replica set.

#connect as admin
mongo --host localhost --port 27001 -u admin -p admin --authenticationDatabase admin

#in mongo shell
use admin
db.createUser( {
    user: "siteRootAdmin",
    pwd: "admin",
    roles: [ { role: "root", db: "admin" } ]

Create key file, this file will be use to internal authentication (between mongod instance). For simplicity I cerate file filled with string “password” named keyFile and save to /var/. Set permission to 600 (owner only).

Create 3 ec2 instance (I’m using AWS), I called them svr02, svr03, svr04. I used AMI from existing mongo server. After those server up and running I empty dbpath and logpath directory.

#empty dbpath
cd /data/db
rm -rf *

#empty log file
cd /var/log
> mongodb.log

Stop mongod instance at svr01 (kill that process).

Start mongod at svr01, svr02, svr03

mongod --auth --storageEngine wiredTiger --port 27001 --replSet abc --logpath /var/log/mongodb.log --logappend --keyFile /var/keyFile --fork

At svr04 (arbiter)

mongod --auth --port 27001 --replSet abc --logpath /var/logs/log.1 --logappend --keyFile /var/keyFile --fork

Make sure mongod up and running

ps -ef | grep mongod
netstat -an | grep 27017

Connect to mongo at svr01 (I’m doing this inside svr01)

#connect as siteRootAdmin
mongo --port 27017 -u siteRootAdmin -p admin --authenticationDatabase admin

#in mongo shell
{_id:0, host:"svr01:27001"},
{_id:1, host:"svr02:27001"},
{_id:2, host:"svr03:27001"},
{_id:3, host:"svr04:27001", arbiterOnly: true}

At the beginning, status of replica set:

abc:OTHER> rs.status();
        "set" : "abc",
        "date" : ISODate("2015-12-01T14:37:32.243Z"),
        "myState" : 1,
        "members" : [
                        "_id" : 0,
                        "name" : "svr01:27001",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 704,
                        "optime" : Timestamp(1448980645, 1),
                        "optimeDate" : ISODate("2015-12-01T14:37:25Z"),
                        "electionTime" : Timestamp(1448980647, 1),
                        "electionDate" : ISODate("2015-12-01T14:37:27Z"),
                        "configVersion" : 1,
                        "self" : true
                        "_id" : 1,
                        "name" : "svr02:27001",
                        "health" : 1,
                        "state" : 5,
                        "stateStr" : "STARTUP2",
                        "uptime" : 7,
                        "optime" : Timestamp(0, 0),
                        "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
                        "lastHeartbeat" : ISODate("2015-12-01T14:37:31.158Z"),
                        "lastHeartbeatRecv" : ISODate("2015-12-01T14:37:31.321Z"),
                        "pingMs" : 0,
                        "syncingTo" : "svr01:27001",
                        "configVersion" : 1
                        "_id" : 2,
                        "name" : "svr03:27001",
                        "health" : 1,
                        "state" : 5,
                        "stateStr" : "STARTUP2",
                        "uptime" : 7,
                        "optime" : Timestamp(0, 0),
                        "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
                        "lastHeartbeat" : ISODate("2015-12-01T14:37:31.156Z"),
                        "lastHeartbeatRecv" : ISODate("2015-12-01T14:37:31.321Z"),
                        "pingMs" : 0,
                        "syncingTo" : "svr01:27001",
                        "configVersion" : 1
                        "_id" : 3,
                        "name" : "svr04:27001",
                        "health" : 1,
                        "state" : 7,
                        "stateStr" : "ARBITER",
                        "uptime" : 7,
                        "lastHeartbeat" : ISODate("2015-12-01T14:37:31.158Z"),
                        "lastHeartbeatRecv" : ISODate("2015-12-01T14:37:31.211Z"),
                        "pingMs" : 0,
                        "configVersion" : 1
        "ok" : 1

Wait until all secondary become “SECONDARY”, STARTUP2 means the slave sync data from master.

Connect to each slave and check the data (except svr04)

mongo --port 27001 -u your_userName -p your_password --authenticationDatabase yourDB
use yourDB

You can find replica set command using help function.

abc:PRIMARY> rs.help();
        rs.status()                     { replSetGetStatus : 1 } checks repl set status
        rs.initiate()                   { replSetInitiate : null } initiates set with default settings
        rs.initiate(cfg)                { replSetInitiate : cfg } initiates set with configuration cfg
        rs.conf()                       get the current configuration object from local.system.replset
        rs.reconfig(cfg)                updates the configuration of a running replica set with cfg (disconnects)
        rs.add(hostportstr)             add a new member to the set with default attributes (disconnects)
        rs.add(membercfgobj)            add a new member to the set with extra attributes (disconnects)
        rs.addArb(hostportstr)          add a new member which is arbiterOnly:true (disconnects)
        rs.stepDown([secs])             step down as primary (momentarily) (disconnects)
        rs.syncFrom(hostportstr)        make a secondary to sync from the given member
        rs.freeze(secs)                 make a node ineligible to become primary for the time specified
        rs.remove(hostportstr)          remove a host from the replica set (disconnects)
        rs.slaveOk()                    shorthand for db.getMongo().setSlaveOk()

        rs.printReplicationInfo()       check oplog size and time range
        rs.printSlaveReplicationInfo()  check replica set members and replication lag
        db.isMaster()                   check who is primary

        reconfiguration helpers disconnect from the database so the shell will display
        an error, even if the command succeeds.

The most frequently used command is printSlaveReplicationInfo() (to check gap between primary and secondary) and rs.status().

Categories: MongoDB
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: