How to Install MongoDB on Ubuntu 20.04 and CentOS 8

how to install mongodb on ubuntu 20.04 and centos 8
installing mongodb on ubuntu 20.04 and centos 8

MongoDB is an open-source and cross-platform NoSQL database system developed by MongoDB Inc. It is 100 times faster than a traditional database system like RDMS. It uses JSON-like documents to store its data. It is used in many modern web applications because it can be easily integrated with several programming languages. It is used by many big companies including, Uber, Stack, Lyft, Accenture, and many more.

In this tutorial, we will show you how to install MongoDB on Ubuntu 20.04 and CentOS 8.

Prerequisites

  • A CentOS 8 VPS (we’ll be using our SSD 2 VPS plan)
  • Access to the root user account (or access to an admin account with root privileges)

Step 1: Log in to the Server & Update the Server OS Packages

First, log in to your CentOS 8 server via SSH as the root user:

ssh root@IP_Address -p Port_number

You will need to replace ‘IP_Address‘ and ‘Port_number‘ with your server’s respective IP address and SSH port number. Additionally, replace ‘root’ with the username of the admin account if necessary.

Before starting, you have to make sure that all CentOS packages installed on the server are up to date. You can do this by running the following commands:

dnf update -y

Step 2: Install MongoDB on CentOS 8

By default, MongoDB does not included in the CentOS 8 standard repo. So you will need to create a MongoDB repo for that. You can create it with the following command:

nano /etc/yum.repos.d/mongodb-org.repo

Add the following lines:

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

Save and close the file. Then, install the MongoDB by running the following command:

dnf install mongodb-org -y

After installing MongoDB, enable the MongoDB service to start at system reboot:

systemctl enable mongod --now

Next, you can verify the status of MongoDB with the following command:

systemctl status mongod

You should see the following output:

● mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2020-12-12 02:04:43 EST; 4s ago
     Docs: https://docs.mongodb.org/manual
  Process: 3265 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
  Process: 3264 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 3262 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 3260 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
 Main PID: 3268 (mongod)
   Memory: 59.8M
   CGroup: /system.slice/mongod.service
           └─3268 /usr/bin/mongod -f /etc/mongod.conf

Dec 12 02:04:42 centos8 systemd[1]: Starting MongoDB Database Server...
Dec 12 02:04:42 centos8 mongod[3265]: about to fork child process, waiting until server is ready for connections.
Dec 12 02:04:42 centos8 mongod[3265]: forked process: 3268
Dec 12 02:04:43 centos8 mongod[3265]: child process started successfully, parent exiting
Dec 12 02:04:43 centos8 systemd[1]: Started MongoDB Database Server.

Next, connect to the MongoDB console with the following command:

mongo

Next, verify the MongoDB version with the following command:

> db.version()

Output:

4.4.2

Next, exit from the MongoDB console with the following command:

> quit()

Step 3: Install MongoDB on Ubuntu 20.04

First, install the required dependencies using the following command:

apt-get install curl gnupg2 -y

Next, add the MongoDB GPG key and repository with the following command:

curl -fsSL https://www.mongodb.org/static/pgp/server-4.4.asc | apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-4.4.list

Next, update the repository and install the MongoDB with the following command:

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

Once the installation is finished, start the MongoDB service with the following command:

systemctl start mongod

Step 4: Enable Authentication

By default, you can connect to the MongoDB console without any authorization. So it is a good idea to enable authentication to secure access to database resources and operations. You can enable it by editing the file /etc/mongod.conf:

nano /etc/mongod.conf

Add the following lines:

security:
  authorization: enabled

Save and close the file then restart the MongoDB service to apply the changes:

systemctl restart mongod

Step 4: Create an Administrative User<

After enabling the MongoDB authorization, you will also need to create an administrative user to access and manage the MongoDB database.

First, connect to the MongoDB console with the following command:

mongo

Once connected, change the database to admin:

> use admin

Next, create an administrative user with the following command:

db.createUser(
  {
    user: "mongoadmin", 
    pwd: "securepassword", 
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

You should get the following output:

Successfully added user: {
    "user" : "mongoadmin",
    "roles" : [
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        }
    ]
}

Next, exit from the MongoDB with the following command:

> quit()

Step 5: Verify MongoDB Authentication

Now, use the following command to connect to the MongoDB with username and password:

mongo -u mongoadmin -p --authenticationDatabase admin

You will be asked to provide a password as shown below:

MongoDB shell version v4.4.2
Enter password: 

Provide your administrator password and hit enter to connect the MongoDB. You should get the following output:

connecting to: mongodb://127.0.0.1:27017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("a8c30363-ea44-4efb-b2c0-90b348279402") }
MongoDB server version: 4.4.2

Next, change the database to admin and list all users with the following command:

> use admin
> show users

You should get the following output:

{
    "_id" : "admin.mongoadmin",
    "userId" : UUID("c93a3f53-6b5f-49f5-8051-eeb34ba0db5c"),
    "user" : "mongoadmin",
    "db" : "admin",
    "roles" : [
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        }
    ],
    "mechanisms" : [
        "SCRAM-SHA-1",
        "SCRAM-SHA-256"
    ]
}

Next, exit with the following command:

> quit()

Congratulations! you have successfully installed MongoDB on Ubuntu 20.04 and CentOS 8.

installing mongodb on ubuntu 20.04 and centos 8

Of course, you don’t have to do any of this if you use one of our MongoDB VPS Hosting services, in which case you can simply ask our expert Linux admins to setup this for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

Leave a Comment