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

email warning: - fix

cat /etc/postfix/main.cf|grep -i inet_prot

[x007516a@xsnl11c950w ~]$ cat /etc/postfix/main.cf|grep -i inet_prot
#inet_protocols = all
inet_protocols = ipv4

service postfix restart

Passwordless SSH

https://www.tecmint.com/ssh-passwordless-login-using-ssh-keygen-in-5-easy-steps/
-------------------------------------------------------------------------------------------------------

SSH Passwordless Login

Step 1: Create Authentication SSH-Kegen Keys on – (xsnl11c949k)

ssh-keygen -t rsa

Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.

Step 2: Create .ssh Directory on – xsnl11c951b

ssh x007516a@xsnl11c951b mkdir -p .ssh

Step 3: Upload Generated Public Keys to – xsnl11c951b

cat .ssh/id_rsa.pub | ssh x007516a@xsnl11c951b 'cat >> .ssh/authorized_keys'

Step 4: Set Permissions on – xsnl11c951b

ssh x007516a@xsnl11c951b "chmod 700 .ssh; chmod 640 .ssh/authorized_keys"

Step 5: Login from xsnl11c949k to xsnl11c951b Server without Password

----------------------------------------------------------------------------------------------------

#!/bin/bash
servers="/home/servers.txt"
for server in `cat $servers`
do
ssh tommy@$server mkdir -p .ssh
cat .ssh/id_rsa.pub | ssh tommy@$server 'cat >> .ssh/authorized_keys'
ssh tommy@$server "chmod 700 .ssh; chmod 640 .ssh/authorized_keys"
done

Get Size in MongoDB

Collections Size of a DB
-------------------------

var collectionNames = db.getCollectionNames(), stats = [];
collectionNames.forEach(function (n) { stats.push(db[n].stats()); });
var stats = stats.sort(function(a, b) { return b['size'] - a['size']; });
for (var c in stats) { print(stats[c]['ns'] + ": " + stats[c]['size'] + " (" + stats[c]['storageSize'] + ")"); }

DB size
--------

db.adminCommand("listDatabases").databases.sort(function(l, r) {
         return r.sizeOnDisk - l.sizeOnDisk}).forEach(function(d) {
         print(d.name + " - " + d.sizeOnDisk)});

(OR)

db = db.getSiblingDB("admin");
dbs = db.runCommand({ "listDatabases": 1 }).databases;
var sum1=0;dbs.forEach(function(database) { sum1+=db.getMongo().getDB(database.name).stats().dataSize }); print(sum1);

Total Size of all DBs
---------------------

var sum = 0; db.getMongo().getDBs()["databases"].forEach(function(x) { sum += db.getMongo().getDB(x.name).stats().dataSize}); print(sum );


Saturday, April 20, 2019

Connection String - MongoDB

Replica Set - Connection String:
-----------------------------------

/data/usr/mongodb/bin/mongo --host rs0/server1:27018,server2:27018 -u user_name -p password --authenticationDatabase admin

/data/usr/mongodb/bin/mongo mongodb://user_name:password@server1:27018,server2:27018/admin?replicaSet=rs0

-----------------------------------------------------------

Sharded Cluster - Connection String:
--------------------------------------

/data/usr/mongodb/bin/mongo --port 27017 -u user_name -p password --authenticationDatabase admin
/data/usr/mongodb/bin/mongo mongodb://user_name:password@server1:27017/admin

-----------------------------------------------------------

Authenticate and connect to test DB
--------------------------------------

/data/usr/mongodb/bin/mongo mongodb://user_name:password@server1:27017/test?authSource=admin

-----------------------------------------------------------

without Authentication:
----------------------------

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

------------------------------------------------------------

Friday, April 19, 2019

Config Files - MongoDB - Samples

mongos:
--------

net:
  port: 27017
processManagement:
  pidFilePath: /var/run/mongodb/mongod.pid
  fork: true
security:
  keyFile: /data/usr/keyfile
sharding:
  configDB: CSRS/server1:27019,server2:27019,server3:27019
systemLog:
  destination: file
  logAppend: true
  logRotate: rename
  path: /data/usr/logs/mongos.log


Config Servers:
----------------

net:
  port: 27019
processManagement:
  pidFilePath: /var/run/mongodb/mongod.pid
  fork: true
security:
  keyFile: /data/usr/keyfile
sharding:
  clusterRole: configsvr
replication:
   replSetName: CSRS
storage:
  dbPath: /data/usr/mongod_config_data
systemLog:
  destination: file
  logAppend: true
  logRotate: rename
  path: /data/usr/logs/mongod_config.log


mongod servers:
----------------

net:
  port: 27017
processManagement:
  pidFilePath: /var/run/mongodb/mongod.pid
  fork: true
security:
  keyFile: /data/usr/keyfile
sharding:
  clusterRole: shardsvr
replication:
  replSetName: rs0
storage:
  dbPath: /data/usr/mongod_data
systemLog:
  destination: file
  logAppend: true
  logRotate: rename
  path: /data/usr/logs/mongod.log


From 3.6 Versions:
-------------------

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

MongoDB Installation - Prerequisites

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

User & Group Create

groupadd -g 490 mongod

useradd mongod -u 496 -g mongod -c 'mongod user' -s /bin/bash -d /home/mongod

id mongod 

sudo to mongod


service account configuration
------------------------------

Please run the below commands as root user,

# vi /etc/passwd/

only for the mongod user - have to change the home path and /bin/false to /bin/bash

/var/lib/mongo:/bin/false
to
/home/mongod:/bin/bash

# mkdir -p /home/mongod
# cp .bash_profile .bashrc /home/mongod/
# chown -R mongod:mongod /home/mongod/

new commands
-----------------

usermod -d /var/lib/mongo mongod

usermod --shell /bin/bash  mongod 

init script

/etc/init.d/mongod

#!/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
# pidfile: /var/run/mongodb/mongod.pid

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

# things from mongod.conf get there by mongod reading it

# 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/usr/conf/mongod.conf"
OPTIONS=" -f $CONFIGFILE"
SYSCONFIG="/etc/sysconfig/mongod"

PIDFILEPATH=`awk -F'[:=]' -v IGNORECASE=1 '/^[[:blank:]]*(processManagement\.)?pidfilepath[[:blank:]]*[:=][[:blank:]]*/{print $2}' "$CONFIGFILE" | tr -d "[:blank:]\"'"`

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

MONGO_USER=mongod
MONGO_GROUP=mongod

if [ -f "$SYSCONFIG" ]; then
    . "$SYSCONFIG"
fi

PIDDIR=`dirname $PIDFILEPATH`

# 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

start()
{
  # Make sure the default pidfile directory exists
  if [ ! -d $PIDDIR ]; then
    install -d -m 0755 -o $MONGO_USER -g $MONGO_GROUP $PIDDIR
  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

Tuesday, April 9, 2019

Reconfig MonogDB

Reconfigure any fields in a Replica Set configuration

cfg = rs.conf();
cfg.members[1].priority = 2;
rs.reconfig(cfg);


Refer: https://docs.mongodb.com/manual/reference/method/rs.reconfig/

Force Secondary to Primary

Convert secondary Node to primary if maximum of node down in a replica set

 

1. Save replica set config in a var

 

cfg = rs.conf()
printjson(cfg)

 

2. Remove the unreachable members

 

cfg.members = [cfg.members[0] , cfg.members[4] , cfg.members[7]]

 

3. Reconfig

 

rs.reconfig(cfg, {force : true}) 

 

 

Refer: https://docs.mongodb.com/manual/tutorial/reconfigure-replica-set-with-unavailable-members/