MongoDB is a scalable and flexible open-source database that allows you to store and retrieve large amounts of data. It is designed for modern applications to facilitate application development and scaling. It is not based on a traditional table-based relational database structure, instead it uses a document-based database engine that stores data in JSON rather than table format. You can integrate it with different programming languages.

This post will explain how to install MongoDB on Ubuntu 22.04 and make it secure.

Requirements

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

Add MongoDB repository

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

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

apt-get install curl apt-transport-https software-properties-common gnupg2 -y

Next, you also need to install the libssl package on your server. You can download and install the libssl package using the following command:

wget http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2_amd64.deb
dpkg -i libssl1.1_1.1.1f-1ubuntu2_amd64.deb

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

wget -qO - 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 cache with the following command:

apt update

Once your repository is updated, install MongoDB with the following command:

apt install -y mongodb-org

After the successful installation, start the MongoDB service and enable it to start when you restart the system:

systemctl start mongod
systemctl enable mongod

You can also check the status of MongoDB with 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-08-06 09:48:44 UTC; 9s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 72432 (mongod)
     Memory: 61.7M
        CPU: 962ms
     CGroup: /system.slice/mongod.service
             ??72432 /usr/bin/mongod --config /etc/mongod.conf

Aug 06 09:48:44 ubuntu2204 systemd[1]: Started MongoDB Database Server.

To check the MongoDB version, run the following command:

mongod --version

You should get the following output:

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

Secure MongoDB with password

By default, authentication is not enabled in MongoDB. For security reasons, it is recommended to enable MongoDB authentication.

To do this, you need to create an administrative user for MongoDB.

First, connect to the MongoDB shell using the following command:

mongo

Once connected, create a database named admin with the following command:

> use admin

Next, create a user admin and enter a password:

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

You should see the following output:

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

Then press CTRL D to exit the MongoDB shell. Next, you also need to enable MongoDB authentication in the MongoDB configuration file. You can do this by editing the MongoDB configuration file:

nano /etc/mongod.conf

Add the following lines to enable authentication:

security:
 authorization: enabled

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

systemctl restart mongod

Verify MongoDB connection

Now MongoDB authentication is enabled. Now run the following command to connect the MongoDB shell with the username and password:

mongo -u mongoadmin -p

After successful authentication, you will get the following output:

MongoDB shell version v4.4.9
Enter password: 
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("f108c3b4-80bd-4175-80b5-747f2a35f1f8") }
MongoDB server version: 4.4.15

> 

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("d98a22c2-d318-48d2-a95d-abda2685a815"),
	"user" : "mongoadmin",
	"db" : "admin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	],
	"mechanisms" : [
		"SCRAM-SHA-1",
		"SCRAM-SHA-256"
	]
}

Uninstall MongoDB

If you don’t want MongoDB in your system, you can uninstall it.

First, stop the MongoDB service with the following command:

systemctl stop mongod

Next, remove the MongoDB package by running the following command:

apt-get remove mongodb-org --purge

Next, remove the MongoDB logs and data directories by running the following command:

rm -rf /var/lib/mongodb

Conclusion

Congratulations! You have successfully installed and secured MongoDB on Ubuntu 22.04. Now you can use MongoDB to manage large amounts of distributed data. For more information, see the official MongoDB documentation.