MongoDB is a cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas. MongoDB is developed by MongoDB Inc. and licensed under the Server Side Public License.

This article demonstrates standalone Mongodb NoSQL database installation and configuration on Redhat Linux environment.

Step 1 – Download mongodb binary file from https://www.mongodb.com/

Step 2 – Create mongod account and group

Step 3 – Create below directory structure

  mkdir -p /database/mongodb 
  mkdir -p /database/mongodb/data 
  mkdir -p /database/mongodb/data/log 
  mkdir -p /database/mongodb/data/db 
  mkdir -p /database/mongodb/data/config 
  mkdir -p /database/mongodb/data/audit 

Step 4 – Grant folder permission

chmod –R 0755 /database/monogdb

Step 5 – Untar binary file

tar -zxvf mongodb-linux-x86_64-enterprise-rhel70-4.0.3.tgz

Step 6 Copy untar file to /database/mongodb folder

cp -R -n mongodb-linux-x86_64-enterprise-rhel70-4.0.3 /database/mongodb

Step 7 – Set PATH for mongodb

export PATH=/database/mongodb/mongodb-linux-x86_64-enterprise-rhel70- 4.0.3/bin:$PATH
export PATH=/usr/local/bin:/usr/bin:/bin:$HOME/bin:/database/mongodb/mongodb-linux-x86_64-enterprise-rhel70-4.0.3/bin

Step 8 – Set bashrc value

echo ‘export PATH=”/database/mongodb/mongodb-linux-x86_64-enterprise-rhel70-4.0.3/bin”’ >> ~/.bashrc
echo ‘export PATH=$PATH:/home/mongod/.local/bin:/home/mongod/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:”/database/mongodb/mongodb-linux-x86_64-enterprise-rhel70-4.0.3/bin”’ >> ~/.bash_profile

Step 9 – Create Config File

systemLog:
destination: file
path: “/database/mongodb/data/log/mongod.log”
logAppend: false
storage:
dbPath: /database/mongodb/data/db/
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /database/mongodb/data/mongod.pid
timeZoneInfo: /usr/share/zoneinfo
net:
bindIp: 10.222.14.84
port: 27018
setParameter:
enableLocalhostAuthBypass: false
#security:
#cd authorization: ‘enabled’

Step 10 – Create start/stop service file in /etc/init.d/mongodb

# !/bin/sh
# /etc/init.d/mongodb
# Subsystem file for “MyIT-Mongodb” server
#
# chkconfig: 2345 95 05 (1)
# description: MyIT-Mongodb server daemon
#MONGOBIN -> point to mongod binary.
MONGOBIN=”/database/mongodb/mongodb-linux-x86_64-enterprise-rhel70-4.0.1/bin/mongod”
do_start()
{
echo “starting!”;
eval “$MONGOBIN -f /database/mongodb/data/config/mongod.conf”;
}
do_stop()
{
echo “stopping!”
eval “$MONGOBIN -f /database/mongodb/data/config/mongod.conf –shutdown”;
}
case “$1” in
start)
do_start
;;
stop)
do_stop
;;
esac
exit 0

Step 11 – Make start file executable using below command.

chmod a+x /etc/init.d/mongodb

Step 12 – Make start file runable in runlevel 3,4 & 5. So use below command.

chkconfig –level 345 mongodb on
chkconfig –list mongodb

Step 13 – Add to chkconfig

chkconfig –add mongodb
chkconfig mongodb on

Step 14 – Start and Stop the services

service mongodb start
service mongodb stop

Step 15 – Log to mongo first time

mongo –port 27018

Step 16 – Create administrator account

use admin
db.createUser(
{
user: “myUserAdmin”,
pwd: “abc123”,
roles: [ { role: “userAdminAnyDatabase”, db: “admin” } ]
}
)

Step 17 – Create root account

use admin
db.createUser({user: “root”, pwd: “abc123”, roles:[“root”]})

Step 18 – Log out of mongodb

Step 19 – Log back with the new credential

mongo -u ‘admin’ -p ‘abc123’ –authenticationDatabase “admin” –port 27018

Step 19 – To run database backup

sudo /database/mongodb/mongodb-linux-x86_64-enterprise-rhel70-4.0.1/bin/mongodump –port 27018 -u administrator -p abc123 –out /database/mongodb/data/backup/

–Backup Specific Database, eg. Fruits
sudo /database/mongodb/mongodb-linux-x86_64-enterprise-rhel70-4.0.1/bin/mongodump –port 27018 –db Fruits -u administrator -p abc123 –out /database/mongodb/data/backup/Fruits

–Backup Specific Collection from Database, eg. Banada Collection from Fruits database
sudo /database/mongodb/mongodb-linux-x86_64-enterprise-rhel70-4.0.1/bin/mongodump –port 27018 –collection Banana –db Fruits -u fruit -p fruit –out /database/mongodb/data/backup/Banana_Collection

Finally to create backup crone job

#!/bin/sh

#=============================================

# Set the following variables as per your requirement

#=============================================

# Database Name to backup

MONGO_DATABASE=”mydb”

# Database host name

MONGO_HOST=”127.0.0.1″

# Database port

MONGO_PORT=”27018″

# Backup directory

BACKUPS_DIR=”/var/backups/$MONGO_DATABASE”

# Database user name

DBUSERNAME=”username”

# Database password

DBPASSWORD=”passw0rd”

# Authentication database name

DBAUTHDB=”admin”

# Days to keep the backup

DAYSTORETAINBACKUP=”14″

#=========================================

TIMESTAMP=`date +%F-%H%M`

BACKUP_NAME=”$MONGO_DATABASE-$TIMESTAMP”

echo “Performing backup of $MONGO_DATABASE”

echo “——————————————–“

# Create backup directory

if ! mkdir -p $BACKUPS_DIR; then

echo “Can’t create backup directory in $BACKUPS_DIR. Go and fix it!” 1>&2

exit 1;

fi;

# Create dump

mongodump -d $MONGO_DATABASE –username $DBUSERNAME –password $DBPASSWORD –authenticationDatabase $DBAUTHDB

# Rename dump directory to backup name

mv dump $BACKUP_NAME

# Compress backup

tar -zcvf $BACKUPS_DIR/$BACKUP_NAME.tgz $BACKUP_NAME

# Delete uncompressed backup

rm -rf $BACKUP_NAME

# Delete backups older than retention period

find $BACKUPS_DIR -type f -mtime +$DAYSTORETAINBACKUP -exec rm {} +

echo “——————————————–“

echo “Database backup complete!”

  • chomd +x mongobackup.sh
  • crontab –e
  • 00 00 * * * /path/to/script/mongobackup.sh