Nethence Newdoc Olddoc Lab Your IP BBDock  

Setting up MongoDB


Install latest STABLE MongoDB version, e.g.,

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

name=MongoDB Repository

yum update
yum install mongodb-org

Note. those packages get installed:


(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


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.


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

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

# 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.

case $1 in
    if [ -d /sys/kernel/mm/transparent_hugepage ]; then
    elif [ -d /sys/kernel/mm/redhat_transparent_hugepage ]; then
      return 0

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

    unset thp_path

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

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**]


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 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

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

  dbPath: /data/mongo

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

Note. no need to change the pid location (/var/run/mongodb/ 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

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 | GitHub | Donate | Contact