How To Install MongoDB 4.4 / 4.2 On Ubuntu 18.04 / Ubuntu 16.04

0
Install MongoDB On Ubuntu 18.04
Install MongoDB On Ubuntu 18.04

MongoDB is a free and open-source cross-platform, document-oriented database designed with both scalability and developer agility in mind. It is written in C++ and JavaScript, uses JSON-like documents with dynamic schemas to store data.

MongoDB is available for Windows, Linux, OS X, and Solaris. It is released under Server Side Public License (SSPL).

Here, we will see how to install MongoDB on Ubuntu 18.04 / Ubuntu 16.04.

THIS DOCUMENT IS ALSO AVAILABLE FOR

Add MongoDB Repository

MongoDB Inc releases stable packages for Ubuntu operating systems such as Ubuntu 18.04 and Ubuntu 16.04, and their packages are generally fresher than those in the Ubuntu repositories. You should always use the official MongoDB packages

Install the below packages.

sudo apt update

sudo apt install -y gnupg

Add the official MongoDB repository to the system.

Ubuntu 18.04

MongoDB 4.4

wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -

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

MongoDB 4.2

wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list

MongoDB 4.0

wget -qO - https://www.mongodb.org/static/pgp/server-4.0.asc | sudo apt-key add -

echo "deb [ arch=amd64,arm64 ] 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

MongoDB 4.4

wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -

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

MongoDB 4.2

wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -

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

MongoDB 4.0

wget -qO - https://www.mongodb.org/static/pgp/server-4.0.asc | sudo apt-key add -

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

Install MongoDB

Update the repository index.

sudo apt update

Install the MongoDB package using the apt command.

sudo apt install -y mongodb-org

Post Installation

File System (Optional)

You can either use XFS or EXT4 type filesystem to hold the MongoDB database (/var/lib/mongodb). If possible, use the XFS filesystem for better performance.

Security Checklist

Admin User

By default, MongoDB doesn’t authenticate users to read and modify data.  So, create an admin user for daily operation.

Access Control

Noe, enable access control to enforce authentication to allow only identified users to perform actions based on their roles.

sudo nano /etc/mongod.conf

Add the below lines.

security:
  authorization: enabled

Start Service

MongoDB has its controlling system, and it can be started/stopped by the easy known commands.

Start MongoDB server, run:

sudo systemctl start mongod

Enable MongoDB service to start automatically at the system startup, run:

sudo systemctl enable mongod

Check the status of MongoDB service, run.

sudo systemctl status mongod

Output:

 mongod.service - MongoDB Database Server
   Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: e
   Active: active (running) since Tue 2020-08-04 00:10:01 IST; 8s ago
     Docs: https://docs.mongodb.org/manual
 Main PID: 2731 (mongod)
   CGroup: /system.slice/mongod.service
           └─2731 /usr/bin/mongod --config /etc/mongod.conf

Aug 04 00:10:01 ubuntu-18-04 systemd[1]: Started MongoDB Database Server.

Confirm the version of MongoDB.

mongod --version

Output:

db version v4.4.0
Build Info: {
    "version": "4.4.0",
    "gitVersion": "563487e100c4215e2dce98d0af2a6a5a2d67c5cf",
    "openSSLVersion": "OpenSSL 1.1.1  11 Sep 2018",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "ubuntu1804",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

Access MongoDB

Connect to MongoDB shell by using the following command.

mongo

Output:

MongoDB shell version v4.4.0
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("793ac2b0-ad8e-4de7-bb09-3ab53295bc1d") }
MongoDB server version: 4.4.0
>

Conclusion

That’s All. I hope you have learned how to install MongoDB on Ubuntu 18.04 / Ubuntu 16.04. Please share your feedback in the comments section.

You might also like