MongoDB: Install on Ubuntu, CentOS

What is MongoDB? MongoDB is an open-source document-oriented database.

It is one of the most powerful NoSQL databases and is commonly used in modern web applications.

From the following article you will find out how to install the latest (or specific) version of MongoDB on Ubuntu and CentOS.

Specific Release: To install MongoDB from a specific release, check the official MongoDB repository and appropriately change the names and repo URLs.

Install MongoDB on Ubuntu

Add the official MongoDB repository:

# Ubuntu-18.04:
$ echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" \
    | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list

# Ubuntu-16.04:
$ echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.0 multiverse" \
    | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list

# Ubuntu-14.04:
$ echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/4.0 multiverse" \
    | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list

Import the public key:

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 \
    --recv 9DA31620334BD75D9DCB49F368818C72E52529D4

Install the latest version of MongoDB:

$ sudo apt-get update
$ sudo apt-get install -y mongodb-org

To install a specific version of MongoDB, run:

$ sudo apt-get install -y mongodb-org=4.0.3

Start the mongod service and add it to auto-start:

$ sudo systemctl start mongod
$ sudo systemctl enable mongod

Install MongoDB on CentOS

Add the official MongoDB repository:

$ echo '[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' \
    | sudo tee /etc/yum.repos.d/mongodb-org-4.0.repo

Import the public key:

$ sudo rpm --import https://www.mongodb.org/static/pgp/server-4.0.asc

Install the latest version of MongoDB:

$ sudo yum install -y mongodb-org

To install a specific version of MongoDB, run:

$ sudo yum install -y mongodb-org-4.0.3

Start the mongod service and add it to auto-start:

$ sudo systemctl start mongod
$ sudo systemctl enable mongod

Connect to MongoDB

Out of the box, MongoDB has no authentication and is listening only on localhost on the default MongoDB port 27017:

$ sudo netstat -tnlp
Proto  Recv-Q  Send-Q  Local Address    Foreign Address  State   PID/Program name
tcp         0       0  127.0.0.1:27017  0.0.0.0:*        LISTEN  1106/mongod

To connect to MongoDB, start the mongo shell:

$ mongo

For more details check the official MongoDB installation guide.

Was it useful? Share this post with the world!

Leave a Reply