Install MongoDB on CentOS

MongoDB provides four packages

Package Name Description
mongodb-org A metapackage that will automatically install the four component packages listed below.
mongodb-org-server Contains the mongod daemon and associated configuration and init scripts.
mongodb-org-mongos Contains the mongos daemon.
mongodb-org-shell Contains the mongo shell.
mongodb-org-tools Contains the following MongoDB tools: mongoimport bsondump, mongodump, mongoexport, mongofiles, mongooplog, mongoperf, mongorestore, mongostat, and mongotop.
  • Configure the package management system (yum)
    Create a /etc/yum.repos.d/mongodb-org-3.4.repo file so that you can install MongoDB directly, using yum. Use the following repository file:
    name=MongoDB Repository
  • Install the MongoDB packages and associated tools
    sudo yum install -y mongodb-org
  • install package policycoreutils-python
    sudo yum install policycoreutils-python
  • Configure SELinux
    Method 1.
    sudo semanage port -a -t mongod_port_t -p tcp 27017

    method 2. Disable SELinux by setting the SELINUX setting to disabled in /etc/selinux/config.


    You must reboot the system for the changes to take effect.
    method 3. Set SELinux to permissive mode in /etc/selinux/config by setting the SELINUX setting to permissive


    You must reboot the system for the changes to take effect.

  • Move data storage to ZFS (optional)
    sudo zfs create -o mountpoint=/var/lib/mongo/ <zpool name>/mongo
    sudo chown -R mongod:mongod /var/lib/mongo
  • Using another dictionary for database (ex. /home/mongo)
    sudo mkdir -p /home/mongo
    sudo chown mongod:mongod /home/mongo
    sudo chcon system_u:object_r:mongod_var_lib_t:s0 /home/mongo
  • run semange fcontext and restorecon
    # sudo semanage fcontext -a -t mongod_db_t "/mongo(/.*)?" # mongod_db_t is not validate. why?
    sudo semanage fcontext -a -t mongod_var_lib_t "/mongo(/.*)?"
    sudo grep -i mongod /etc/selinux/targeted/contexts/files/file_contexts.local
  • Start MongoDB
    sudo systemctl start mongod

    You can verify that the mongod process has started successfully by checking the contents of the log file at /var/log/mongodb/mongod.log for a line reading

    [initandlisten] waiting for connections on port <port>
  • Enable MongoDB start following a system reboot
    sudo chkconfig mongod on
  • Enable authorization and listen on all IPs
    You need create user first before you enable authorization. see: mongodb cheetsheet
    Update the /etc/mongod.conf
# network interfaces
  port: 27017
#  bindIp:  # Listen to local interface only, comment to listen on all interfaces.

  authorization: enabled

Uninstall MongoDB

sudo systemctl stop mongod
sudo yum erase $(rpm -qa | grep mongodb-org)
sudo rm -r /var/log/mongodb /var/lib/mongo
Written on October 19, 2017