How To Install MongoDB On Ubuntu 20.04

0 3

MongoDB is a free and open-source document-oriented database that uses JSON-like documents with optional schemas to store information. It designed with both scalability and developer agility in mind.

MongoDB is a cross-platform database and is available for Windows, Linux, OS X, Solaris, and FreeBSD. It is released under Server Side Public License (SSPL).

Here, we will see how to install MongoDB on Ubuntu 20.04.

THIS DOCUMENT IS ALSO AVAILABLE FOR

Add MongoDB Repository

MongoDB Inc releases stable packages for Ubuntu operating systems and shares them via their repository. MongoDB packages available in MongoDB repository are generally fresher than those in the Ubuntu repositories.

You should always use the official MongoDB packages.

Install the below dependent packages.

sudo apt update

sudo apt install -y gnupg

Add the MongoDB’s official repository to the system.

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 focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list

Install MongoDB

Update the repository index.

sudo apt update

Install the MongoDB using the apt command.

sudo apt install -y mongodb-org

Post Installation

Network Binding

By default, MongoDB listens on to 127.0.0.1, a localhost network interface. This means that MongoDB can only accept connections from clients that are running on the same machine. In this case, remote clients will not be able to connect to the MongoDB.

sudo nano /etc/mongod.conf

Update the bindIP with your system IP address.

# network interfaces
net:
  port: 27017
  bindIp: 192.168.0.10

Security CheckList

Create Admin User

By default, MongoDB doesn’t authenticate users to read and modify data, and it is the biggest security concern. To enable authentication, first, create a database administrative user.

Access Control

After creating an administrative user, enable access control to enforce authentication. This setting allows only identified users to perform actions based on their roles.

sudo nano /etc/mongod.conf

Add the below lines.

security:
  authorization: enabled

Manage MongoDB Service

To start MongoDB service, run:

sudo systemctl start mongod

To enable MongoDB service to start automatically at the system startup run:

sudo systemctl enable mongod

Check the status of MongoDB service.

sudo systemctl status mongod

Output:

 mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2020-08-03 13:30:55 EDT; 7s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 4392 (mongod)
     Memory: 61.1M
     CGroup: /system.slice/mongod.service
             └─4392 /usr/bin/mongod --config /etc/mongod.conf

Aug 03 13:30:55 ubuntu2004 systemd[1]: Started MongoDB Database Server.

Confirm the version of the MongoDB server.

mongod --version

Output:

db version v4.4.0
Build Info: {
    "version": "4.4.0",
    "gitVersion": "563487e100c4215e2dce98d0af2a6a5a2d67c5cf",
    "openSSLVersion": "OpenSSL 1.1.1f  31 Mar 2020",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "ubuntu2004",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

Access MongoDB

Connect to MongoDB shell by using the following command.

mongo

OR

mongo 

OR

mongo  --port 27017 --authenticationDatabase "admin" -u "" -p

Output:

connecting to: mongodb://192.168.0.10:27017/test?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("42279415-3014-46b5-b85f-dc83d7bc4bbd") }
MongoDB server version: 4.4.0
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
        https://community.mongodb.com
>

Conclusion

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