to setup mongodb on replicate mode atleast you must have 3 servers as primary, secondary and arbiter , you can follow step by step on bellow to setup it

primary server, secondary server and arbiter server

create directory for database path

mkdir /var/lib/mongodb/your_db

create directory for mongo log path

mkdir /var/log/mongod_log

download mongodb-2.4.9 from this url bellow

next extrac source mongodb to /opt

tar xzvf mongodb-linux-x86_64-2.4.9.tgz -C /opt

rename directory mongodb

mv mongodb-linux-x86_64-2.4.9 mongodb

start service mongod on primary server

/opt/mongodb/bin/mongod –bind_ip –port 23029 –dbpath /var/lib/mongodb/your_db –logpath /var/log/mongod_log –rest –fork –replSet foo

secondary server

/opt/mongodb/bin/mongod –bind_ip –port 23030 –dbpath /var/lib/mongodb/your_db –logpath /var/log/mongod_log –rest –fork –replSet foo

arbiter server

/opt/mongodb/bin/mongod –bind_ip –port 23031 –dbpath /var/lib/mongodb/your_db –logpath /var/log/mongod_log –rest –fork –replSet foo

now you must initiate mongodb as primary node

/opt/mongodb/bin/mongo –host

“info2” : “no configuration explicitly specified — making one”,
“me” : “”,
“info” : “Config now saved locally. Should come online in about a minute.”,
“ok” : 1

next add node as secondary server

> rs.add(“”)
{ “ok” : 1 }

secondary server is initiate and connect to server primary

after that you must add one server again as arbiter. Arbiter can buffer and promote secondary server as a primary automatic when primary fail
> rs.addArb(“”)
{ “ok” : 1 }

to show status of replicaset you can use command



enable auth on mongodb replication

before you enable auth on replication mode you must complete setup replication with arbiter if no auth can’t enable. Mongodb have two levels of access control, system (global) and per database.

User roles, write on mongodb ver 2.4

Database User Roles


System User Roles


first we will enable auth on system (global) level, create database “admin”

> use admin

then set user name, password and roles

> db.addUser({ user:”rwUser”, pwd:”password”, roles:[“clusterAdmin”,”readAnyDatabase”,”readWriteAnyDatabase”,”userAdminAnyDatabase”,”dbAdminAnyDatabase”] })

if finish, shutdown all mongodb on primary, secondary and arbiter, then you make directory on three of them and create file on the directory with content password of user admin

# mkdir /data/keyfile

# vim /data/keyfile/auth_key

give execute mode (x) to auth_key

# chmod 700 /data/keyfile/auth_key

now start mongodb and add option –auth to enable auth then –keyFile to comunicate among server mongodb


/opt/mongodb/bin/mongod –keyFile /data/keyfile/key-masakdb –auth –bind_ip –port 32000 –dbpath /data/ –logpath /var/log/mongodb/mongod.log –replSet masakdb –rest –fork