Setting up MongoDB

Installation

Install latest STABLE MongoDB version, e.g.,

vi /etc/yum.repos.d/mongodb-org-3.2.repo

[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

yum update
yum install mongodb-org

Note. those packages get installed:

mongodb-org-shell
mongodb-org-tools
mongodb-org-mongos
mongodb-org-server
mongodb-org

(undeeded) just for the record, if you need to pin the version so it won’t get upgraded automaticly when doing yum upgrade,

#cd /etc/
#cp yum.conf yum.conf.dist
#vi yum.conf
#exclude=mongodb-org,mongodb-org-server,mongodb-org-shell,mongodb-org-mongos,mongodb-org-tools

Ref. https://docs.mongodb.com/master/tutorial/install-mongodb-on-red-hat/

Warning: service gets enabled by default at boot time. That SysV script is backward compatible on RHEL/CentOS version 7,

service mongod status
service mongod start
chkconfig --list | grep mongo
chkconfig mongod on

Increase the process & file limits for mongodb user only, hardcoded system-wide,

vi /etc/security/limits.d/99-mongodb-nproc.conf

mongod - fsize unlimited
mongod - cpu unlimited
mongod - as unlimited
mongod - nofile 64000
mongod - rss unlimited
mongod - nproc 64000
mongod - memlock unlimited

Not sure a server reboot is needed, try restarting mongodb first and if you still got the warnings with mongo command line, simply restart the server.

Refs.

Improve database performance by disabling Transparent Huge Pages (THP) using an additional init script loaded at startup,

vi /etc/init.d/disable-transparent-hugepages

#!/bin/sh
### BEGIN INIT INFO
# Provides:          disable-transparent-hugepages
# Required-Start:    $local_fs
# Required-Stop:
# X-Start-Before:    mongod mongodb-mms-automation-agent
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Disable Linux transparent huge pages
# Description:       Disable Linux transparent huge pages, to improve
#                    database performance.
### END INIT INFO

case $1 in
  start)
    if [ -d /sys/kernel/mm/transparent_hugepage ]; then
      thp_path=/sys/kernel/mm/transparent_hugepage
    elif [ -d /sys/kernel/mm/redhat_transparent_hugepage ]; then
      thp_path=/sys/kernel/mm/redhat_transparent_hugepage
    else
      return 0
    fi

    echo 'never' > ${thp_path}/enabled
    echo 'never' > ${thp_path}/defrag

    unset thp_path
    ;;
esac

chmod 755 /etc/init.d/disable-transparent-hugepages
chkconfig --add disable-transparent-hugepages
chkconfig --list | grep huge
/etc/init.d/disable-transparent-hugepages

restart the system as we’ve changed the limits (keep it simple),

shutdown -r now

and check,

cat /sys/kernel/mm/transparent_hugepage/enabled
cat /sys/kernel/mm/transparent_hugepage/defrag

both should return,

always madvise [**never**]

Ref. https://docs.mongodb.com/manual/tutorial/transparent-huge-pages/

Alternatively, just for the record, it’s also possible to proceed directly at kernel load time,

#cd /etc/sysconfig/
#cp grub grub.dist
#vi grub
#GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=VG0/root rhgb quiet net.ifnames=0 biosdevname=0 **transparent_hugepage=never**"
#(no need to reload with those sysconfig settings?)
#and reboot to check

Alternate pathes

If you need to locate the data & log folders on a specific location,

cd /data/
service mongod status
service mongod stop
mv /var/lib/mongo/ ./
mv /var/log/mongodb/ ./mongo.logs/
ls -al mongo/
ls -al mongo.logs/
#check the perms

cd /etc/
cp mongod.conf mongod.conf.dist
vi mongod.conf

systemLog:
  path: /data/mongo.logs/mongod.log

storage:
  dbPath: /data/mongo

net:
    #bindIp: 127.0.0.1  # Listen to local interface only, comment to listen on all interfaces.

Note. no need to change the pid location (/var/run/mongodb/mongod.pid). Note. bindIP can stay commented out unless you really want it to become a network service, you can ssh tunnel anyway against the localhost:27017 to use MongoChef or whatever to debug the db.

Double check the path (it’s normal the pid won’t show up), ownerships and permissions,

cd /etc/
grep -i path mongod.conf | awk '{print $2}' | xargs ls -lhFd
#chown -R mongod:mongod /data/databases/ /data/logs/mongodb/
#chmod 755 /data/mongo/ /data/mongo.logs/
#chmod 640 /data/mongo.logs/mongod.log

Ready to go

Let’s start the nifty daemon and watch the logs,

service mongod start
tail -F /data/mongo.logs/mongod.log

the last line as for daemon’s startup should report,

2016-<time> I NETWORK  [initandlisten] waiting for connections on port 27017

Maintenance / Operations

exclude package from conflicting repositories

e.g. make sure nodejs and mongodb are excluded from EPEL upgrades (those two package sets have their own repo),

cd /etc/yum.repos.d/
vi epel.repo

[epel]
...
exclude=libuv* nodejs* npm* mongodb-org*

then to proceed with the MongoDB upgrades, simply,

yum upgrade

and if you see that MongoDB was indeed upgraded, schedule a database restart,

service mongod restart

as long as those are minor updates (repo is pointing to minor release changes), no database migration/update process is required. For major upgrades see the official doc.

also if NodeJS was upgraded, schedule an application restart,

cd /data/...
npm stop
npm start

NodeJS repo is also setup to stick to a major release so there should not be any code breaks.


HOME | GUIDES | LECTURES | LAB | SMTP HEALTH | HTML5 | CONTACT
Copyright © 2024 Pierre-Philipp Braun