How to Install and Secure MongoDB NoSQL Database on Ubuntu 22.04

MongoDB is a free, open-source, and one of the most popular NoSQL database systems. It stores data in JSON rather than the usual table style method found in SQL databases. It can be integrated easily with other programs so it is widely used in web applications. It does not require a predefined schema, and the data structure can be changed over time. It is written in C++ and provides scalability, high performance, and high availability.

In this tutorial, we will show you how to install MongoDB on Ubuntu 22.04.

Prerequisites

  • A server running Ubuntu 22.04.
  • A root password is configured on the server.

Add MongoDB Repository

By default, the MongoDB package is not included in the Ubuntu 22.04 default repository so you will need to add the MongoDB official repository to the APT.

First, install all the required dependencies using the following command:

apt install wget gnupg2 curl -y

Next, download and add the MongoDB GPG key using the following command:

curl -fsSL https://www.mongodb.org/static/pgp/server-4.4.asc | apt-key add -

Next, add the MongoDB repository to APT using the following command:

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

Once the MongoDB repository is added, update the repository cache using the following command:

apt update -y

Install MongoDB on Ubuntu 22.04

You can now install the MongoDB package using the following command:

apt install mongodb-org -y

Next, start the MongoDB service and enable it to start at system reboot using the following command:

systemctl start mongod

You can check the status of the MongoDB using the following command:

systemctl status mongod

You should see the following output:

? mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-05-28 15:04:49 UTC; 7s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 27422 (mongod)
     Memory: 63.3M
        CPU: 967ms
     CGroup: /system.slice/mongod.service
             ??27422 /usr/bin/mongod --config /etc/mongod.conf

May 28 15:04:49 ubuntu2204 systemd[1]: Started MongoDB Database Server.

To check the MongoDB version, use the following command:

mongo --version

You should see the MongoDB version in the following output:

MongoDB shell version v4.4.14
Build Info: {
    "version": "4.4.14",
    "gitVersion": "0b0843af97c3ec9d2c0995152d96d2aad725aab7",
    "openSSLVersion": "OpenSSL 1.1.1l  24 Aug 2021",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "ubuntu2004",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

You can also check the MongoDB connection information with the following command:

mongo --eval 'db.runCommand({ connectionStatus: 1 })'

You should get the following output:

MongoDB shell version v4.4.14
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("44560246-9673-418a-95c9-112bba345f4f") }
MongoDB server version: 4.4.14
{
	"authInfo" : {
		"authenticatedUsers" : [ ],
		"authenticatedUserRoles" : [ ]
	},
	"ok" : 1
}

Create MongoDB Admin User

By default, MongoDB can be accessed without any username and password. For security reasons, it is recommended to create an admin user to authenticate MongoDB.

First, log in to the MongoDB shell with the following command:

mongo

Once you are log in, change the database to admin using the following command:

use admin

Next, create an admin user and set a password with the following command:

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

You should see the following output:

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

Next, edit the MongoDB configuration file and enable the MongoDB authentication. You can do it by editing the MongoDB configuration file:

nano /etc/mongod.conf

Add the following line to enable the MongoDB authentication:

security:
  authorization: enabled

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

systemctl restart mongod

Verify MongoDB Authentication

Now, you can verify the MongoDB authentication using the following command:

mongo -u admin -p --authenticationDatabase admin

You will be asked to provide a MongoDB admin password:

MongoDB shell version v4.4.14
Enter password: 

Provide your admin password and press the Enter key. You should see the following output:

connecting to: mongodb://127.0.0.1:27017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("05106893-c382-4895-a55b-7560858b31eb") }
MongoDB server version: 4.4.14
> 

To list all MongoDB databases, run the following command:

> show dbs

You should see the following output:

admin   0.000GB
config  0.000GB
local   0.000GB

Conclusion

Congratulations! you have successfully installed MongoDB on Ubuntu 22.04. You can now use MongoDB as a database backend for a high-performance web applications. Feel free to ask me if you have any questions.

Share this page:

Suggested articles

1 Comment(s)

Add comment

Comments

By: Navjot Singh at: 2022-08-24 07:48:15

There are two issues. 1: apt-key is deprecated in Ubuntu 22.042: The tutorial won't work because MongoDB depends on libssl1.1 which is not available in Ubuntu 22.04.