Wednesday, January 31, 2018

MongoDB Run As a Service – Linux

MongoDB Run As a Service 
  • If you are installing mongodb with the default installer like yum or with rpm, by default the init.d scripts will be generated for mongo and the service commands also will be in place 
  • But if you are installing mongodb with tarball or binary installer, there will not be any init scripts and service commands will not work as well.
Example:

Here am gonna show you how to run mongodb as a service with tarball or binary installation.

Rules: 
  • There should be mongod script placed in /etc/init.d/mongod
  • All files in db_path, logpath, binary path, config file path should be in the ownership of mongod (user and group)
Paths:

The below mentioned paths may change based on your installation, but mongod script path should the same as given below.

Mongod script path  - /etc/init.d/mongod
Binary path - /data/mongodb-3.2.18/bin/mongod
Config file path - /data/config/mongod.conf

You don want to worry about the below given script, as we gonna change only to path variable in the script.

Sample Config File:

net:
  port: 27019
processManagement:
  pidFilePath: /var/run/mongodb/mongod.pid
  fork: true
sharding:
  clusterRole: shardsvr
replication:
  replSetName: rs_0
storage:
  dbPath: /data/usr/mongod_data
systemLog:
  destination: file
  logAppend: true
  logRotate: rename

  path: /data/usr/logs/mongod.log

mongod script:

#!/bin/bash

# mongod - Startup script for mongod

# chkconfig: 35 85 15
# description: Mongo is a scalable, document-oriented database.
# processname: mongod
# config: /etc/mongod.conf

. /etc/rc.d/init.d/functions

# NOTE: if you change any OPTIONS here, you get what you pay for:
# this script assumes all options are in the config file.
CONFIGFILE="/data/config/mongod.conf"
OPTIONS=" -f $CONFIGFILE"

mongod=${MONGOD-/data/mongodb-3.2.18/bin/mongod}

MONGO_USER=mongod
MONGO_GROUP=mongod

# All variables set before this point can be overridden by users, by
# setting them directly in the SYSCONFIG file. Use this to explicitly
# override these values, at your own risk.
SYSCONFIG="/etc/sysconfig/mongod"
if [ -f "$SYSCONFIG" ]; then
    . "$SYSCONFIG"
fi

# Handle NUMA access to CPUs (SERVER-3574)
# This verifies the existence of numactl as well as testing that the command works
NUMACTL_ARGS="--interleave=all"
if which numactl >/dev/null 2>/dev/null && numactl $NUMACTL_ARGS ls / >/dev/null 2>/dev/null
then
    NUMACTL="numactl $NUMACTL_ARGS"
else
    NUMACTL=""
fi

# things from mongod.conf get there by mongod reading it
PIDFILEPATH=`awk -F'[:=]' -v IGNORECASE=1 '/^[[:blank:]]*(processManagement\.)?pidfilepath[[:blank:]]*[:=][[:blank:]]*/{print $2}' "$CONFIGFILE" | tr -d "[:blank:]\"'" | awk -F'#' '{print $1}'`
PIDDIR=`dirname $PIDFILEPATH`

start()
{
  # Make sure the default pidfile directory exists
  if [ ! -d $PIDDIR ]; then
    install -d -m 0755 -o $MONGO_USER -g $MONGO_GROUP $PIDDIR
  fi

  # Make sure the pidfile does not exist
  if [ -f $PIDFILEPATH ]; then
      echo "Error starting mongod. $PIDFILEPATH exists."
      RETVAL=1
      return
  fi

  # Recommended ulimit values for mongod or mongos
  # See http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings
  #
  ulimit -f unlimited
  ulimit -t unlimited
  ulimit -v unlimited
  ulimit -n 64000
  ulimit -m unlimited
  ulimit -u 64000

  echo -n $"Starting mongod: "
  daemon --user "$MONGO_USER" --check $mongod "$NUMACTL $mongod $OPTIONS >/dev/null 2>&1"
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && touch /var/lock/subsys/mongod
}

stop()
{
  echo -n $"Stopping mongod: "
  mongo_killproc "$PIDFILEPATH" $mongod
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/mongod
}

restart () {
        stop
        start
}

# Send TERM signal to process and wait up to 300 seconds for process to go away.
# If process is still alive after 300 seconds, send KILL signal.
# Built-in killproc() (found in /etc/init.d/functions) is on certain versions of Linux
# where it sleeps for the full $delay seconds if process does not respond fast enough to
# the initial TERM signal.
mongo_killproc()
{
  local pid_file=$1
  local procname=$2
  local -i delay=300
  local -i duration=10
  local pid=`pidofproc -p "${pid_file}" ${procname}`

  kill -TERM $pid >/dev/null 2>&1
  usleep 100000
  local -i x=0
  while [ $x -le $delay ] && checkpid $pid; do
    sleep $duration
    x=$(( $x + $duration))
  done

  kill -KILL $pid >/dev/null 2>&1
  usleep 100000

  checkpid $pid # returns 0 only if the process exists
  local RC=$?
  [ "$RC" -eq 0 ] && failure "${procname} shutdown" || rm -f "${pid_file}"; success "${procname} shutdown"
  RC=$((! $RC)) # invert return code so we return 0 when process is dead.
  return $RC
}

RETVAL=0

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart|reload|force-reload)
    restart
    ;;
  condrestart)
    [ -f /var/lock/subsys/mongod ] && restart || :
    ;;
  status)
    status $mongod
    RETVAL=$?
    ;;
  *)
    echo "Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
    RETVAL=1
esac

exit $RETVAL



Path Variable to be changed for your installation:


The above mentioned are the two variables to be take care in the entire script

Just change the config file path and the binary path, the given script will work for most of the binary installations.

After doing this the service commands will work for you, 
  • Start – sudo service mongod start
  • Stop – sudo service mongod stop
  • Restart - sudo service mongod restart


Start:

# sudo service mongod start


When we checked the process, it’s not running but when we trigged the command – the mongo process automatically started.

Stop:

# sudo service mongod stop


Here when we checked the process, its already running but when we triggered the command it stopped.

Restart:

 # sudo service mongod restart


As same as start and stop, its restarted.

Done…

Monday, January 29, 2018

Run MongoDB Server using Config file – Linux

Run MongoDB Server using Config file – Binary Installation

  • When doing a binary installation of MongoDB, there won’t be any config files configured automatically when this happens in .rpm or yum installer. 
  • So in this case, we need to configure config files manually. Here am gonna show an example, how to run mongo server using config file for the binary installation done in the previous post.


Ex config file: 


We have to mention the port_no, db_path and log_path in the config file as mentioned above,

For this example, am gonna save this config file to the path,

/home/ec2-user/mongod.conf

# vi mongod.conf

net:
  port: 27019
operationProfiling: {}
processManagement:
  pidFilePath: /var/run/mongodb/mongod.pid
  fork: "true"
storage:
  dbPath: /home/ec2-user/mongo_data
systemLog:
  destination: file
  logAppend: true
  logRotate: rename
  path: /home/ec2-user/mongo_log/mongod.log

# :wq

Here in the config file, we are using –

Port : 27019
dbPath: /home/ec2-user/mongo_data
logpath: /home/ec2-user/mongo_log/mongod.log

To start the server,

If environmental variable is set then,

# mongod -f /home/ec2-user/mongod.conf

else,

# <bin path>/mongod -f /home/ec2-user/mongod.conf


As we had set the environmental variable,


The server has started, as we forked the process – it’s not showing the logs like where the port is listening an al. ( fork: "true” )

To check the process,

# ps aux | grep mongo


To start the Client,

Just type in mongo and port no in the same command line and don’t need to open another, as we forked the process. ( we need to mention the port no, as we have changed the default port no to 27019)

# mongo --port 27019


Done…

Saturday, January 27, 2018

Environmental Variable Setup MongoDB – Linux

Environmental Variable Setup MongoDB – Linux

Copy the bin path where the mongo binaries are stored,


/home/ec2-user/mongodb-linux-x86_64-3.2.18/bin


Edit ~/.bashrc file as shown below,

# vi ~/.bashrc


Just edit the export path

From export PATH=/usr/bin/:$PATH

To export PATH=/home/ec2-user/mongodb-linux-x86_64-3.2.18/bin/:$PATH


# :wq

We need the source bashrc to reload -

# source ~/.bashrc

Now start the server without giving the entire path,

# mongod --dbpath /home/ec2-user/data/db


To start the client,

Open another command prompt, just type mongo –

# mongo


Done...





Install MongoDB from Tarball - Linux

Install MongoDB from Tarball - Linux

Here we gonna install MongoDB 3.2.18 using tarball approach, where we can give our own binary path and config file path.

Download the binary files



It will get downloaded in the home directory,


Extract the file from the downloaded archive

# tar -zxvf mongodb-linux-x86_64-3.2.18.tgz


Prerequisite to run mongo server

Create a data directory where the data should get stored,

# mkdir -p data/db
# cd data/db
# pwd

/home/ec2-user/data/db


Copy the binary path, where we extracted –

/home/ec2-user/mongodb-linux-x86_64-3.2.18/bin


Run the mongo server

# /home/ec2-user/mongodb-linux-x86_64-3.2.18/bin/mongod --dbpath /home/ec2-user/data/db

When click on enter, the server starts running listening to the default port  27017 as shown below –


As soon as you start the mongo server, the default files will get generated as shown below in the data directory.


Start the Mongo Client

Keep the server running in one command line and open another command line, run the below commands –

# /home/ec2-user/mongodb-linux-x86_64-3.2.18/bin/mongo


Done...








Friday, January 26, 2018

Install MongoDB using .rpm Packages (download and install) – Linux

Install MongoDB using .rpm Packages (download and install) – Linux

Here we gonna install MongoDB 3.2.16 by downloading the .rpm packages and installing, instead of having .repo files.

Download the rpm packages


sudo yumdownloader mongodb-org-3.2.16 mongodb-org-server-3.2.16 mongodb-org-shell-3.2.16 mongodb-org-mongos-3.2.16 mongodb-org-tools-3.2.16


It will get downloaded in the home directory,


Install the binaries

sudo yum install mongodb-org-3.2.16-1.el7.x86_64.rpm mongodb-org-mongos-3.2.16-1.el7.x86_64.rpm mongodb-org-server-3.2.16-1.el7.x86_64.rpm mongodb-org-shell-3.2.16-1.el7.x86_64.rpm mongodb-org-tools-3.2.16-1.el7.x86_64.rpm


When click on enter, the below screen will appear after the successful installation,


By default, after the installation the below mentioned files will be created –

/var/log/mongodb/mongod.log
/etc/mongod.conf – Configuration File

When we start the server, the server will start according to the configuration mentioned in /etc/mongod.conf file.

Configuration File – by default

dbpath - /var/lib/mongo
logpath - /var/log/mongodb/mongod.log
port – 27017

To start the server

# sudo service mongod start


You can check the mongo service by using the command,
# ps aux | grep mongo

To Start the client

Just type mongo in the same terminal, as we forked the process which is mentioned in the config file.

# mongo


Done...







Thursday, January 18, 2018

Install MongoDB using YUM Installer – Linux

Install MongoDB using YUM Installer – Linux

Here am gonna show you an example to install MongoDB 3.2 version using YUM installer UNIX

Configure the package management system (yum)

Create a /etc/yum.repos.d/mongodb-org-3.2.repo file so that you can install MongoDB directly, using yum.

For the latest stable release of MongoDB - Use the following repository file,

[mongodb-org-3.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc

# sudo vi /etc/yum.repos.d/mongodb-org-3.2.repo
Copy and paste the repo file here

# :wq



Install the MongoDB packages and associated tools.

To install the latest stable version of MongoDB, issue the following command:

# sudo yum install -y mongodb-org

Just trigger this command, after successful completion you can see the below screen –


By default, after the installation the below mentioned files will be created –

/var/log/mongodb/mongod.log
/etc/mongod.conf – Configuration File

When we start the server, the server will start according to the configuration mentioned in /etc/mongod.conf file.

Configuration File – by default

Dbpath - /var/lib/mongo
Logpath - /var/log/mongodb/mongod.log
Port – 27017

To start the server

# sudo service mongod start


You can check the mongo service by using the command,
# ps aux | grep mongo

To Start the client

Just type mongo in the same terminal, as we forked the process which is mentioned in the config file.

# mongo


Done...

Tuesday, January 16, 2018

Setting Up Environmental Variable for MongoDB - Windows

Environmental Variable Setup for MongoDB

Go till bin path  where the mongodb is installed– and copy it,


C:\Users\tommy\Downloads\mongo_server\bin


Steps to be followed,

1)     Ctrl + R
2)     Type sysdm.cpl, click ok


3)     The following page will appear, In that select Advanced and click on Environment Variables


              4)     Here select Path and click on Edit

                            

       
         5)     Click on New -> Paste the bin path and click on OK, OK and OK for the three                                      consecutive windows which is already opened.


          Done..


    Now to start the server
               
                1)     Go to command prompt, just type mongod and mention the dbpath if any or else it will 
                go to the default directory data/db – hit enter ( don’t need to specify the path till bin )

     mongod –dbpath  C:\Users\tommy\Downloads\mongo_data


           2)     The server starts running listening to the default port  27017


          To start the client,

                1)     Open another command prompt, just type mongo

                 


            Note: By setting up the Environmental Variable, we don’t have specify the entire mongo                                binary path – Just type in the binary name.


Done..