Tuesday, April 30, 2019

MongoDB Installation - Linux + Mongo

MongoDB 4.0.8 -  Installation
--------------------------------------

Install MongoDB and Configure Replica Set

Prerequisite:
---------------

1) create user - mongod
   home directory - /home/mongod

   groupadd -g 490 mongod
   useradd mongod -u 496 -g mongod -c 'mongod User' -s /bin/bash -d /home/mongod
   id mongod

2) Create file in /etc/init.d - /etc/init.d/mongod
   chown mongod:mongod /etc/init.d/mongod
   chmod 775 /etc/init.d/mongod

3) create directory in /var/run/ - /var/run/mongodb/
   chown mongod:mongod /var/run/mongodb/
   chmod 775 /var/run/mongodb/

4) Change ownership and permission for /data
   chown -R mongod:mongod /data
   chmod -R 775 /data

Servers:
-----------

server1:27017 - Pri
server2:27017 - Sec
server3:27017 – Sec

Prerequisites – for all the 3 servers

Create necessary directories
-----------------------------------

# dzdo su - mongod
# mkdir -p /data/usr/conf
# mkdir -p /data/usr/logs/
# mkdir -p /data/usr/mongod_data
# chown -R mongod:mongod /data/usr
# chmod -R 775 /data/usr
# mkdir -p /var/run/mongodb/
# chown mongod:mongod /var/run/mongodb/
# chmod 775 /var/run/mongodb/
# chmod -R 775 /data/usr
# touch /etc/init.d/mongod
# chown mongod:mongod /etc/init.d/mongod
# chmod 775 /etc/init.d/mongod

Copy the Latest copy of binaries
-----------------------------------------

server10

# scp -r /data/usr/mongodb-linux-x86_64-rhel62-4.0.8 user@server1:/tmp
# scp -r /data/usr/mongodb-linux-x86_64-rhel62-4.0.8 user@server2:/tmp
# scp -r /data/usr/mongodb-linux-x86_64-rhel62-4.0.8 user@server3:/tmp

# chmod -R 777 /tmp/mongodb-linux-x86_64-rhel62-4.0.8
# dzdo su - mongod
# cp -r /tmp/mongodb-linux-x86_64-rhel62-4.0.8 /data/usr/

/

# cp -avr /data/usr/mongodb-linux-x86_64-rhel62-3.6.5 /data/usr/
# scp -r user@server10:/data/usr/mongodb-linux-x86_64-rhel62-3.6.5 /data/usr/
# tar -zxvf  mongodb-linux-x86_64-rhel62-3.6.5.tgz


Create Softlink
--------------------

# ln -s /data/usr/mongodb-linux-x86_64-rhel62-4.0.8 /data/usr/mongodb

Create Config File:
-----------------------

# vi /data/usr/conf/mongod.conf

net:
  port: 27017
  bindIpAll: true
processManagement:
  pidFilePath: /var/run/mongodb/mongod.pid
  fork: true
#security:
#  keyFile: /data/usr/keyfile
replication:
  replSetName: Media_Feed_Auto
storage:
  dbPath: /data/usr/mongod_data
systemLog:
  destination: file
  logAppend: true
  logRotate: rename
  path: /data/usr/logs/mongod.log
# :wq!

# chown -R mongod:mongod /data/usr
# chmod -R 775 /data/usr

Create Init.d scripts – to run mongod as a service
-----------------------------------------------------------

# copy the script to the file /etc/init.d/mongod

File Paths
------------

Config File Path: /data/usr/conf/mongod.conf
Log File Path: /data/usr/logs/mongod.log
Binary File Path: /data/usr/mongodb/bin/mongod

Configure Replica Set:
-----------------------------

1) Start each member of the replica set.

# dzdo service mongod start

2) Connect to a member of the replica set.

server1

# /data/usr/mongodb/bin/mongo

3) Initiate the replica set.

# rs.initiate()
# rs.add("server2:27017")
# rs.add("server3:27017")

Login to mongo console
-------------------------------

/data/usr/mongodb/bin/mongo mongodb://server1:27017,server2:27017,server3:27017/admin?replicaSet=Media_Feed_Auto

1 comment: