Showing posts with label MongoDB Replicaset. Show all posts
Showing posts with label MongoDB Replicaset. Show all posts

Wednesday, March 30, 2016

MongoDB Replicaset installation on VMs

MongoDB Replicaset installation on VMs

1.       Create 3 VMs and install RHEL
[root@node2 ~]# uname -a
Linux node2.rs05.mongodb 2.6.32-504.el6.x86_64 #1 SMP Tue Sep 16 01:56:35 EDT 2014 x86_64 x86_64 x86_64 GNU/Linux
[root@node2 ~]#

2.       Add static IP ( Private IP in our case) to all the 3VMs
[root@node2 ~]# more /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=192.168.0.45
NETMAST=255.255.255.0
GATEWAY=192.168.0.1
DNS1=192.168.0.1
[root@node2 ~]#

3.       Assign hostname to all the 3VMs in /etc/sysconfig/network file.
[root@node2 ~]# hostname
node2.rs05.mongodb
[root@node2 ~]#

4.       Map 3VMs static IP and hostname details in the /etc/hosts file
[root@node1 ~]# more /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.0.44    node1.rs05.mongodb
192.168.0.45    node2.rs05.mongodb
192.168.0.46    node3.rs05.mongodb
[root@node1 ~]#

5.       Disable the firewall (iptables)  /etc/sysconfig/iptables
[root@node1 ~]# service iptables stop
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]
[root@node1 ~]#

6.       Download MongoDB tar package and extract it and rename as mongodb
[root@node1 ~]# ll
total 44
-rw-------. 1 root root  1385 Mar 13 23:40 anaconda-ks.cfg
-rw-r--r--. 1 root root 28054 Mar 13 23:40 install.log
-rw-r--r--. 1 root root  7572 Mar 13 23:40 install.log.syslog
drwxr-xr-x. 3 root root  4096 Mar 14 03:20 mongo


7.       Create new mongodb folder under /usr/local/mongodb and copy mongo contents into it.
[root@node1 local]# ll mongodb/
total 100
drwxr-xr-x. 2 root root  4096 Mar 14 03:28 bin
-rw-r--r--. 1 root root 34520 Mar 14 03:28 GNU-AGPL-3.0
-rw-r--r--. 1 root root 16726 Mar 14 03:28 MPL-2
-rw-r--r--. 1 root root  1359 Mar 14 03:28 README
-rw-r--r--. 1 root root 35910 Mar 14 03:28 THIRD-PARTY-NOTICES
[root@node1 local]#

8.       Create configuration file under /etc/mongo.conf and all db, log, replicaset and mongo.pid details.
[root@node1 local]# more /etc/mongodb.conf
logpath=/var/log/mongodb/mongod.log
logappend=true
fork=true
port=27017
dbpath=/var/lib/mongodb
pidfilepath=/var/run/mongodb/mongod.pid
replSet=RS05
oplogSize=1024
[root@node1 local]#

9.       Add mongodb binaries into PATH variable  in bash profile file
[root@node1 local]# more ~/.bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin:/usr/local/mongodb/bin

export PATH
[root@node1 local]#

10.   Start the mongod instance in all the VMs (nodes)
[root@node1 local]# mongod -f /etc/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 4631
child process started successfully, parent exiting
[root@node1 local]#

11.   Start the mongo in node one and add the remaining 2 nodes into the replica set.
$mongo
> config={_id:"RS05", members:[{_id:0, host:"node1.rs05.mongodb:27017", priority:3}, {_id:1, host:"node2.rs05.mongodb:27017", priority:2}, {_id:2, host:"node3.rs05.mongodb:27017", priority:1}]}
> rs.initiate(config)
>rs.status()
RS05:PRIMARY> rs.status()
{
        "set" : "RS05",
        "date" : ISODate("2016-03-15T09:32:19.938Z"),
        "myState" : 1,
        "term" : NumberLong(4),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "members" : [
                {
                        "_id" : 0,
                        "name" : "node1.rs05.mongodb:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 180,
                        "optime" : {
                                "ts" : Timestamp(1458034175, 2),
                                "t" : NumberLong(4)
                        },
                        "optimeDate" : ISODate("2016-03-15T09:29:35Z"),
                        "electionTime" : Timestamp(1458034175, 1),
                        "electionDate" : ISODate("2016-03-15T09:29:35Z"),
                        "configVersion" : 1,
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "node2.rs05.mongodb:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 179,
                        "optime" : {
                                "ts" : Timestamp(1458034175, 2),
                                "t" : NumberLong(4)
                        },
                        "optimeDate" : ISODate("2016-03-15T09:29:35Z"),
                        "lastHeartbeat" : ISODate("2016-03-15T09:32:19.432Z"),
                        "lastHeartbeatRecv" : ISODate("2016-03-15T09:32:19.544Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "node1.rs05.mongodb:27017",
                        "configVersion" : 1
                },
                {
                        "_id" : 2,
                        "name" : "node3.rs05.mongodb:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 179,
                        "optime" : {
                                "ts" : Timestamp(1458034175, 2),
                                "t" : NumberLong(4)
                        },
                        "optimeDate" : ISODate("2016-03-15T09:29:35Z"),
                        "lastHeartbeat" : ISODate("2016-03-15T09:32:19.432Z"),
                        "lastHeartbeatRecv" : ISODate("2016-03-15T09:32:19.545Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "node1.rs05.mongodb:27017",
                        "configVersion" : 1
                }
        ],
        "ok" : 1
}
RS05:PRIMARY>


12.   Now the replica set is ready to play.