Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fingul/4fcc1484fe2891384d658f3da75da71a to your computer and use it in GitHub Desktop.
Save fingul/4fcc1484fe2891384d658f3da75da71a to your computer and use it in GitHub Desktop.
Install latest MongoDB on RedHat/CentOS 7
#!/bin/bash
# Simple install script for stock RedHat/CentOS 7.x
# Allows yum update to pull security & other fixes automatically from MongoDB.com's repos
# (versus ancient packages in Red Hat/Cent repos)
# To completely purge all remnants of Mongo (repo conf, rpms, yum cache, DB files, kernel tweaks:
# sudo service mongod stop ; sudo rm -rf /etc/yum.repos.d/mongo* ; sudo rm -rf /var/lib/mongo/* ; sudo sed -i.`date +%Y-%m-%d_%H-%M-%S`.bak '/^#.*$/!d' /etc/rc.d/rc.local ; sudo rm -rf /var/cach/yum ; sudo yum -y clean all ; sudo yum -y remove mongodb*
# Sanity check - are we on a RH family distro?
[ -f "/etc/redhat-release" ] || { echo -e "This script requires RedHat or CentOS. Quitting. \n"; exit 1 ;}
# Version of MongoDB to install
# read -e -p "Install community or enterprise edition? " -i "enterprise" MDB_EDITION
# read -e -p "Mongo version? " -i "3.6" MDB_VER
MDB_EDITION='community'
MDB_VER="3.6"
if grep -i -q "enterprise" <<< $MDB_EDITION
then MDB_EDITION='enterprise' && MDB_REPO="https://repo.mongodb.com/yum/redhat/\$releasever/mongodb-enterprise/$MDB_VER/\$basearch/"
else MDB_EDITION='community' && MDB_REPO="https://repo.mongodb.org/yum/redhat/\$releasever/mongodb-org/$MDB_VER/\$basearch/"
fi
echo Using MongoDB $MDB_EDITION edition, version $MDB_VER
# $releasever is set automatically.
# To query: rpm -q --provides $(rpm -q --whatprovides "system-release(releasever)") | grep "system-release(releasever)" | cut -d ' ' -f 3
# Alternatively: yum version nogroups 2>&1 |grep Install|tr '/' ' ' |cut -d' ' -f2
# Community:
# baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/3.6/x86_64/
# Platform-aware Community:
# baseurl=https://repo.mongodb.org/yum/redhat/\$releasever/mongodb-org/3.6/\$basearch/
# Enterprise:
# baseurl=https://repo.mongodb.com/yum/redhat/7/mongodb-enterprise/3.6/x86_64/
# Platform-aware Enterprise:
# baseurl=https://repo.mongodb.com/yum/redhat/\$releasever/mongodb-enterprise/3.6/\$basearch/
cat <<EOF | sudo tee /etc/yum.repos.d/mongodb-$MDB_EDITION.repo >/dev/null
[mongodb-$MDB_EDITION-$MDB_VER]
name=MongoDB $MDB_EDITION repository
baseurl=$MDB_REPO
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-$MDB_VER.asc
EOF
if [ $MDB_EDITION = "enterprise" ]
then sudo yum install -y mongodb-enterprise
else sudo yum install -y mongodb-org
fi
# Enable recommended new process limits
echo "mongod soft nproc 32000" | sudo tee -a /etc/security/limits.d/20-nproc.conf >/dev/null
# Enable recommended kernel hugepage memory settings
# cat <<EOF | sudo tee -a /etc/rc.d/rc.local >/dev/null
# if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
# echo never > /sys/kernel/mm/transparent_hugepage/enabled
# fi
# if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
# echo never > /sys/kernel/mm/transparent_hugepage/defrag
# fi
# EOF
#
# sudo chmod +x /etc/rc.d/rc.local
# Disable transparent hugepages (and thp defrag)
sudo mkdir -p /etc/tuned/no-thp
cat <<EOF | sudo tee /etc/tuned/no-thp/tuned.conf >/dev/null
[main]
include=virtual-guest
[vm]
transparent_hugepages=never
EOF
sudo tuned-adm profile no-thp
# Enable at boot
sudo systemctl enable mongod
echo -n -e "\nConfirming MongoDB enabled at boot... "; systemctl is-enabled mongod 2>/dev/null
sudo systemctl start mongod # reload or stop as well
echo -n -e "\nChecking for a running mongod server... "
sleep 2
if pgrep "mongod" 2>&1>/dev/null; then echo -e "[OK]\n" ; else echo -e "[FAIL]\n" ; fi
echo -e "To monitor activity, use: sudo tail -f /var/log/mongodb/mongod.log \n\n"
# Fix for first launch warning message on new installs
# ("Error loading history file: FileOpenFailed: Unable to fopen() file /home/XXXX/.dbshell: No such file or directory")
touch $HOME/.dbshell
echo -e "Attempting to connect to mongod server and query local databases... \n"
sleep 3
echo "show dbs" | mongo
echo -e "\nInstall complete. Recommend rebooting for kernel configuration to propagate.\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment