MongoDB Install

MongoDB install Docs

Ubuntu

sudo wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
  • Create a list file for MongoDB '/etc/apt/sources.list.d/mongodb-org-4.4.list'
//if ubuntu 20.04
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
//if ubuntu 18.04
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
  • Reload local package database
sudo apt-get update
  • Install the MongoDB packages
sudo apt-get install -y mongodb-org
  • To prevent unintended upgrades, you can pin the package at the currently installed version
echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-org-shell hold" | sudo dpkg --set-selections
echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo "mongodb-org-tools hold" | sudo dpkg --set-selections

Redhat/ centos

-install Docs

Configure Package Management System

Create a /etc/yum.repos.d/mongodb-org-4.0.repo file with following content so that you can install MongoDB directly using yum

[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc

Install the MongoDB packages

To install the latest stable version of MongoDB, issue the following command:

sudo yum install -y mongodb-org

Note : mongodb-org is a meta package

Exclude MongoDB packages

(from auto update)

You can specify any available version of MongoDB. However yum upgrades the packages when a newer version becomes available.

To prevent unintended upgrades, pin the package. To pin a package, add the following exclude directive to your /etc/yum.conf file:

exclude=mongodb-org,
mongodb-org-server,
mongodb-org-shell,
mongodb-org-mongos,mongodb-org-tools
mongod --version

Settings

Increase user limit settings

Docs

To increase processes limit

(for mongod server)

  • Go to following folder

    cd /etc/security/limits.d
  • Create following new file

    cat > 99-mongodb-nproc.conf
  • Add following content (soft is like >= lower limit, hard is upper limit)

    mongod soft nofile 64000
    mongod hard nofile 64000
    mongod soft nproc 64000
    mongod hard nproc 64000

    Note : these limits.d settings will override ulimit settings

  • For example in limits.d, there is one more file 20-nproc.conf (which set process limit for user accounts), so similar syntax for mongod also

    * soft nproc 4096
    root soft nproc unlimited

To increase user resources limit

To check per-user limitations for various resources

ulimit -a
output
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 30896
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 30896
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited

To modify paticular user limit settings (say open files)

ulimit -n <value>
  • Run following for recommended ulimit Settings

    #file size
    ulimit -f unlimited
    #cpu time
    ulimit -t unlimited
    #virtual memory
    ulimit -v unlimited
    #locked-in-memory size
    ulimit -l unlimited
    #memory size
    ulimit -m unlimited
    #open files
    ulimit -n 64000
    #processes/threads
    ulimit -u 64000

Always remember to restart your mongod and mongos instances after changing the ulimit settings to ensure that the changes take effect.

To change Default Directories

By default
/var/lib/mongo (the data directory)
/var/log/mongodb/mongod.log (the log directory)
  • Edit the the configuration file /etc/mongod.conf

    storage.dbPath=/some/data/directory
    systemLog.path=/some/log/directory/mongod.log

Config file options Docs

Note: (mongod user is created by MONGO DB installation, this user must have access to the path defined by you for storage)

  • configured db path folder must exist
  • also configure port & ip address

Start/ stop MongoDB

sudo service mongod start

Verify that MongoDB has started successfully

check (configured) log file
/var/log/mongodb/mongod.log

Optionally ensure that MongoDB will start following a system reboot by issuing the following command:

sudo chkconfig mongod on //it internally does 'systemctl enable mongod.service'

Stop MongoDB

sudo service mongod stop

Restart MongoDB

sudo service mongod restart