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

https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.4.9.tgz

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 192.168.1.2 –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 192.168.1.3 –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 192.168.1.4 –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 192.168.1.2:23029

>rs.initiate()
{
“info2” : “no configuration explicitly specified — making one”,
“me” : “192.168.1.2:23029”,
“info” : “Config now saved locally. Should come online in about a minute.”,
“ok” : 1
}

next add node as secondary server

> rs.add(“192.168.1.3:23030”)
{ “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(“192.168.1.196:700”)
{ “ok” : 1 }

to show status of replicaset you can use command

rs.status()

————————————————

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

read
readWrite
dbAdmin
userAdmin

System User Roles

clusterAdmin
readAnyDatabase
readWriteAnyDatabase
userAdminAnyDatabase
dbAdminAnyDatabase

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
password

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

example

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

‘Erick-s’

Advertisements