Thursday, June 2, 2016

[Quick Install: DB Cluster]: MongoDB 3 Node replication cluster

MongoDB 3 Node replication cluster

MongoDB Fail Over Cluster

Introduction: Replication provides redundancy and increases data availability. With multiple copies of data on different database servers, replication protects a database from the loss of a single server. Replication also allows you to recover from hardware failure and service interruptions. With additional copies of the data, you can dedicate one to disaster recovery, reporting, or backup MongoDB 3 Node replication cluster
Operating System:  Red Hat Enterprise Linux Server release 6.5 (Santiago)
Scenario: I used three machines and configure the name resolution for all machine by edit the entry in /etc/hosts file like below
[root@TechTutorial ~]#cat /etc/hosts
#####################################################################################
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.42.139 machine1.com
192.168.42.142 machine2.com
192.168.42.141 machine3.com
#####################################################################################
[root@TechTutorial ~]#vim /etc/yum.repos.d/mongodb-org-3.0.repo
## >> Add below Lines in file <<##
#####################################################################################
[mongodb-org-3.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.0/x86_64/
gpgcheck=0
enabled=1
#####################################################################################

[root@TechTutorial ~]#yum install -y mongodb-org
[root@TechTutorial ~]#cp /usr/bin/mongod /usr/bin/mongod-new
[root@TechTutorial ~]#cp /etc/mongod.conf /etc/mongod-new.conf
[root@TechTutorial ~]#cp /etc/init.d/mongod /etc/init.d/mongd-new
[root@TechTutorial ~]#mkdir /DATA
[root@TechTutorial ~]#vim /etc/mongod-new.conf
## >> Change these lines in config file  << ##
####################################################################################
fork=true
port=30003
dbpath=/DATA/
bind_ip= 192.168.42.139,127.0.0.1
replSet=rs0
####################################################################################

[root@TechTutorial ~]#touch /etc/sysconfig/mongod-new
[root@TechTutorial ~]# vim /etc/init.d/mongod-new
#Delete all previous configuration of startup and use this startup script and added these bold lines accordingly#
#!/bin/bash
#
# mongodb Startup script for the mongodb server
#
# chkconfig: - 64 36
# description: MongoDB Database Server
#
# processname: mongod-new
#
# Source function library
. /etc/rc.d/init.d/functions
if [ -f /etc/sysconfig/mongod-new ]; then
. /etc/sysconfig/mongod-new
fi
prog="mongod-new"
mongod="/usr/bin/mongod-new"
config_file="/etc/mongod-new.conf"
RETVAL=0
start() {
echo -n $"Starting $prog: "
daemon $mongod "-f $config_file --fork --logpath /var/log/mongod-new.log --logappend 2>&1 >>/var/log/mongod-new.log"
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
return $RETVAL
}
reload() {
echo -n $"Reloading $prog: "
killproc $prog -HUP
RETVAL=$?
echo
return $RETVAL
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
condrestart)
if [ -f /var/lock/subsys/$prog ]; then
stop
start
fi
;;
reload)
reload
;;
status)
status $mongod
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|restart|condrestart|reload|status}"
RETVAL=1
esac
exit $RETVAL
Check permission on all files
[root@TechTutorial ~]#ll /etc/mongod-new.conf
-rw-r--r-- 1 root root 1563 Jun 26 16:09 /etc/mongod-new.conf
[root@TechTutorial ~]#ll /etc/init.d/mongod-new
-rwxr-xr-x 1 root root 1189 Jun 26 03:47 /etc/init.d/mongod-new
[root@TechTutorial ~]# ll /usr/bin/mongod-new
-rwxr-xr-x 1 root root 21519920 Jun 25 21:26 /usr/bin/mongod-new
Now Start the service
[root@TechTutorial ~]# /etc/init.d/mongod-new start
[root@TechTutorial ~]# ps -elf | grep mongod-new
1 S root 2507 1 1 80 0 - 799822 poll_s 13:47 ? 00:02:37 /usr/bin/mongod-new -f /etc/mongod-new.conf --fork --logpath /var/log/mongod-new.log --logappend
0 S root 13276 2272 0 80 0 - 25811 pipe_w 16:32 pts/0 00:00:00 grep mongod-new
[root@TechTutorial ~]# netstat -tulnp | grep mongod-new
tcp 0 0 127.0.0.1:30003 0.0.0.0:* LISTEN 18282/mongod-new
tcp 0 0 192.168.42.139:30003 0.0.0.0:* LISTEN 18282/mongod-new
IMPORTANT:- Proceed to further Please read below text carefully
Same steps revise on machine2 and machine3 then after come to machine1 or where you want to run mongoDB as master(Primary)
Note: Before starting or login to mongo shell Set IPTABLES rule and SELinux policy otherwise stop iptables service and disabled selinux policy and remember one thing before proceed or login to mongo shell you must start service of mongod on all servers then proceed to next step.
If you want to run MongoDB in Enforcing mode
Enable access to the relevant ports (e.g. 30003) for SELinux if in enforcing mode. For settings, this can be accomplished by running this command
[root@TechTutorial ~]# semanage port -a -t mongod-new_port_t -p tcp 3003
[root@TechTutorial ~]# mongo localhost:30003
> rs.initiate()
{
"info2" : "no configuration explicitly specified -- making one",
"me" : "mongodslave2.com:30003",
"ok" : 1
}
rs0:OTHER> rs.add("mongodmaster.com:30004")
{ "ok" : 1 }
rs0:PRIMARY> rs.conf()
2015-06-26T04:03:27.146+0530 E QUERY TypeError: Object function () { return "try rs.help()"; } has no method 'connf'
at (shell):1:4MongoDB Fail Over Cluster
 # mongo localhost:30003
> rs.initiate()
{
"info2" : "no configuration explicitly specified -- making one",
"me" : "mongodslave2.com:30003",
"ok" : 1
}
rs0:OTHER> rs.add("mongodmaster.com:30004")
{ "ok" : 1 }
rs0:PRIMARY> rs.conf()
2015-06-26T04:03:27.146+0530 E QUERY TypeError: Object function () { return "try rs.help()"; } has no method 'connf'
at (shell):1:4
rs0:PRIMARY> rs.conf()
{
"_id" : "rs0",
"version" : 2,
"members" : [
{
"_id" : 0,
"host" : "mongodslave2.com:30003",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
},
{
"_id" : 1,
"host" : "mongodmaster.com:30004",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatTimeoutSecs" : 10,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
}
}
}
rs0:PRIMARY> use akash
switched to db akash
rs0:PRIMARY> db
akash
rs0:PRIMARY> db.movie.insert({"name":"tech tutorial"})
WriteResult({ "nInserted" : 1 })
rs0:PRIMARY> show dbs
akash 0.078GB
local 1.078GB
rs0:PRIMARY> exit
bye

Check your logs in this open new connections
# tail -f /var/log/mongodb.log
2015-06-26T04:04:13.323+0530 I NETWORK [conn6] end connection 192.168.42.142:45804 (1 connection now open)
2015-06-26T04:04:13.324+0530 I NETWORK [initandlisten] connection accepted from 192.168.42.142:45805 #7 (2 connections now open)
2015-06-26T04:04:18.136+0530 I INDEX [conn1] allocating new ns file /DATA/akash.ns, filling with zeroes...
2015-06-26T04:04:18.265+0530 I STORAGE [FileAllocator] allocating new datafile /DATA/akash.0, filling with zeroes...
2015-06-26T04:04:18.267+0530 I STORAGE [FileAllocator] done allocating datafile /DATA/akash.0, size: 64MB, took 0.001 secs
2015-06-26T04:04:18.276+0530 I WRITE [conn1] insert akash.movie query: { _id: ObjectId('558c81ea839b14ed7d7942f8'), name: "tech tutorials" } ninserted:1 keyUpdates:0 writeConflicts:0 numYields:0 locks:{ Global: { acquireCount: { r: 4, w: 4 } }, MMAPV1Journal: { acquireCount: { w: 8 } }, Database: { acquireCount: { w: 3, W: 1 } }, Collection: { acquireCount: { W: 1 } }, Metadata: { acquireCount: { W: 4 } }, oplog: { acquireCount: { w: 2 } } } 140ms
2015-06-26T04:04:18.277+0530 I COMMAND [conn1] command akash.$cmd command: insert { insert: "movie", documents: [ { _id: ObjectId('558c81ea839b14ed7d7942f8'), name: "tech tutorials" } ], ordered: true } keyUpdates:0 writeConflicts:0 numYields:0 reslen:80 locks:{ Global: { acquireCount: { r: 4, w: 4 } }, MMAPV1Journal: { acquireCount: { w: 8 } }, Database: { acquireCount: { w: 3, W: 1 } }, Collection: { acquireCount: { W: 1 } }, Metadata: { acquireCount: { W: 4 } }, oplog: { acquireCount: { w: 2 } } } 263ms
Now you go to Second machine and login to mongo shell
# mongo mongodmaster.com:30004
So if any time your master server or Primary is stop or power failure this secondary server become Primary or master automatically.
If you want to add more member in your replSet then you should Proceed further in this Document and these operation perform only Primary server
rs0:SECONDARY>
rs.add("mongodmaster.com:30002")
{ "ok" : 1 }
rs0:PRIMARY> rs.conf()
{
"_id" : "rs0",
"version" : 3,
"members" : [
{
"_id" : 0,
"host" : "mongodslave2.com:30003",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
},
{
"_id" : 1,
"host" : "mongodmaster.com:30004",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
},
{
"_id" : 2,
"host" : "mongodmaster.com:30002",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatTimeoutSecs" : 10,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
}
}
}
rs0:PRIMARY> rs.conf()
{
"_id" : "rs0",
"version" : 2,
"members" : [
{
"_id" : 0,
"host" : "mongodslave2.com:30003",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
},
{
"_id" : 1,
"host" : "mongodmaster.com:30004",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatTimeoutSecs" : 10,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
}
}
}
rs0:PRIMARY> use akash
switched to db akash
rs0:PRIMARY> db
akash
rs0:PRIMARY> db.movie.insert({"name":"tutorials point"})
WriteResult({ "nInserted" : 1 })
rs0:PRIMARY> show dbs
akash 0.078GB
local 1.078GB
rs0:PRIMARY> exit
bye
Check your logs in this open new connections
[root@TechTutorial ~]# tail -f /var/log/mongodb.log
2015-06-26T04:04:13.323+0530 I NETWORK [conn6] end connection 192.168.42.142:45804 (1 connection now open)
2015-06-26T04:04:13.324+0530 I NETWORK [initandlisten] connection accepted from 192.168.42.142:45805 #7 (2 connections now open)
2015-06-26T04:04:18.136+0530 I INDEX [conn1] allocating new ns file /DATA/akash.ns, filling with zeroes...
2015-06-26T04:04:18.265+0530 I STORAGE [FileAllocator] allocating new datafile /DATA/akash.0, filling with zeroes...
2015-06-26T04:04:18.267+0530 I STORAGE [FileAllocator] done allocating datafile /DATA/akash.0, size: 64MB, took 0.001 secs
2015-06-26T04:04:18.276+0530 I WRITE [conn1] insert akash.movie query: { _id: ObjectId('558c81ea839b14ed7d7942f8'), name: "tech tutorials" } ninserted:1 keyUpdates:0 writeConflicts:0 numYields:0 locks:{ Global: { acquireCount: { r: 4, w: 4 } }, MMAPV1Journal: { acquireCount: { w: 8 } }, Database: { acquireCount: { w: 3, W: 1 } }, Collection: { acquireCount: { W: 1 } }, Metadata: { acquireCount: { W: 4 } }, oplog: { acquireCount: { w: 2 } } } 140ms
2015-06-26T04:04:18.277+0530 I COMMAND [conn1] command akash.$cmd command: insert { insert: "movie", documents: [ { _id: ObjectId('558c81ea839b14ed7d7942f8'), name: "tech tutorials" } ], ordered: true } keyUpdates:0 writeConflicts:0 numYields:0 reslen:80 locks:{ Global: { acquireCount: { r: 4, w: 4 } }, MMAPV1Journal: { acquireCount: { w: 8 } }, Database: { acquireCount: { w: 3, W: 1 } }, Collection: { acquireCount: { W: 1 } }, Metadata: { acquireCount: { W: 4 } }, oplog: { acquireCount: { w: 2 } } } 263ms

Now you go to Second machine and login to mongo shell
# mongo mongodmaster.com:30004
rs0:SECONDARY>
So if any time your master server or Primary is stop or powerfailure this secondary server become Primary or master automatically.
If you want to add more member in your replSet then you should Proceed further in this Document and these operation perform only Primary server
rs.add("mongodmaster.com:30002")
{ "ok" : 1 }
rs0:PRIMARY> rs.conf()
{
"_id" : "rs0",
"version" : 3,
"members" : [
{
"_id" : 0,
"host" : "mongodslave2.com:30003",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
},
{
"_id" : 1,
"host" : "mongodmaster.com:30004",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
},
{
"_id" : 2,
"host" : "mongodmaster.com:30002",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatTimeoutSecs" : 10,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
}
}
}

No comments: