How to Install MongoDB on Ubuntu 20.04 (Community Edition Tutorial)

How to Install MongoDB on Ubuntu 20.04 (Community Edition Tutorial). MongoDB server is an open-source NoSQL database that has been gaining popularity in recent years.

MongoDB is a cross-platform document-oriented database program. It can be used on different platforms and languages like Perl, Java, C++, Ruby, Python, etc.

In this article, we will introduce MongoDB, its features, and benefits, and then we will move on to its installation of its Community Edition on Ubuntu 20.04.

What is MongoDB

MongoDB is a free and open-source cross platform and document oriented database program that is classified as a NoSQL or non relational database program. It has several advantages over relational databases, including dynamic schemas, native JSON support and rich aggregation operations.

It is a scalable, high performance database that stores data in JSON like documents with dynamic schemas. Documents are stored in collections and each collection can have a different schema.

MongoDB is written in C++ and has its own query language, called JavaScript Object Notation (JSON). This makes it easy to manipulate data in the database. It also supports indexing and geospatial queries.

It provides the platform for adding new functionality through its plugin architecture. The developers of MongoDB also provide a rich set of drivers for integrating with other programming languages such as C#, Java and Python.

MongoDB platform is easy to scale, provides high availability and offers rich features to build applications. MongoDB is an open source database that allows developers to work with data in real time, store and access it from any device, and run several queries at once.

It runs on Linux, OS X, Windows, Solaris, FreeBSD and OpenBSD.

Features of MongoDB

MongoDB features include:

Benefits of MongoDB

MongoDB’s popularity can be attributed to its following benefits:

  • It’s free and open-source software (FOSS).
  • A complete data platform for cloud based applications.
  • Flexible design and document schemas.
  • Widely supported and code-native data access.
  • Supports indexing, rich queries and analytics.
  • Simple to use and scale.
  • Great availability and high performance.
  • Built-in sharding and replication.
  • Installation is simple and inexpensive.
  • Technical assistance and documentation are available.

Follow this post to learn how to install MongoDB Community Edition on Ubuntu 20.04.

How to Install MongoDB on Ubuntu 20.04 (Community Edition Tutorial)

If you have already installed MongoDB by its default package in the Ubuntu repository, you will need to remove it because MongoDB in the Ubuntu repository is not maintained by MongoDB Inc.

Then you can proceed with the following process to install the MongoDB community edition directly from its official repository.

Import Public GPG Key of MongoDB Repository

Run the following wget command to import the MongoDB public GPG key.

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

Next, verify the GPG key using the following command.

				
					sudo apt-key list
				
			

Output:

				
					/etc/apt/trusted.gpg
--------------------
pub   rsa4096 2021-02-16 [SC] [expires: 2026-02-15]
      F567 9A22 2C64 7C87 527C  2F8C B00A 0BD1 E2C6 3C11
uid           [ unknown] MongoDB 5.0 Release Signing Key 

/etc/apt/trusted.gpg.d/ubuntu-keyring-2012-archive.gpg
...
				
			

Add the MongoDB Repository to the Source List

Add the MongoDB repository to the source list file with the following command. This will ensure that the latest official mongodb-org package will be installed on your system.

				
					sudo touch /etc/apt/sources.list.d/mongodb-org-5.0.list
				
			

Next, add the repository source for Ubuntu 20.04 using the following command.

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

Output:

				
					deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse

				
			

Finally, update the system to ensure that the source list is updated.

				
					sudo apt-get update
				
			

Install MongoDB on Ubuntu 20.04 (Community Edition Tutorial)

Now that the official repository has been added to the source list, you can run the simple apt command to install the latest version of MongoDB on your system.

Run the following command to install the mongodb-org package.

				
					sudo apt-get install mongodb-org -y
				
			

Output:

				
					Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libfwupdplugin1 linux-headers-5.11.0-36-generic linux-hwe-5.11-headers-5.11.0-36
  linux-image-5.11.0-36-generic linux-modules-5.11.0-36-generic
  linux-modules-extra-5.11.0-36-generic
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  mongodb-database-tools mongodb-mongosh mongodb-org-database mongodb-org-database-tools-extra
…
				
			

Start and Manage the MongoDB Service

Start and Stop the MongoDB Service

You can start the MongoDB service using the following command.

				
					sudo systemctl start mongod
				
			

To stop the MongoDB service, run the following command.

				
					sudo systemctl stop mongod
				
			

Check the MongoDB Service Status

Run the following command to check and verify the status of the MongoDB service.

				
					sudo systemctl status mongod
				
			

Output:

				
					● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
     Active: active (running) since Thu 2022-06-09 04:51:49 PDT; 12s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 57105 (mongod)
     Memory: 63.1M
     CGroup: /system.slice/mongod.service
             └─57105 /usr/bin/mongod --config /etc/mongod.conf
Jun 09 04:51:49 ubuntu-VirtualBox systemd[1]: Started MongoDB Database Server.
…

				
			

Enable and Disable the MongoDB Service

You can also enable the MongoDB service on boot with the following command.

				
					sudo systemctl enable mongod
				
			

Output:

				
					Created symlink /etc/systemd/system/multi-user.target.wants/mongod.service → /lib/systemd/system/mongod.service
				
			

Similarly, you can disable the MongoDB service using the following command.

				
					sudo systemctl disable mongod
				
			

Verify MongoDB Operability

Run the following diagnostic command to check if the database can connect to the database server.

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

Output:

				
					MongoDB shell version v5.0.9
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("070cbbd7-30e4-4e2b-be86-e95a7ac62fcb") }
MongoDB server version: 5.0.9
{
	"authInfo" : {
		"authenticatedUsers" : [ ],
		"authenticatedUserRoles" : [ ]
	},
	"ok" : 1
}
				
			

Run MongoDB

To start MongoDB service, run the following command.

				
					mongo
				
			

or

				
					mongosh
				
			

Output:

				
					Current Mongosh Log ID:	62a1df5ce7b9f2ebcf431a16
Connecting to:		mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.5.0
Using MongoDB:		5.0.9
Using Mongosh:		1.5.0
...

				
			

or Run the following command.

				
					mongosh "mongodb://localhost:27017" 
				
			

Configure Remote Access

To allow MongoDB to be accessed remotely, you need to edit the /etc/mongod.conf configuration file.

Open the /etc/mongod.conf configuration file in the nano editor.

				
					sudo nano /etc/mongod.conf
				
			

You will see the following network interfaces section in the file.

				
					network interfaces
 net:
   port: 27017
   bindIp: 127.0.0.1,

				
			

Replace <mongodb_server_ip> with your MongoDB server’s IP address. And run your MongoDB remotely with the following command.

				
					mongosh "mongodb://:27017"
				
			

That’s all for the MongoDB installation on Ubuntu 20.04.

How to Install MongoDB on Ubuntu 20.04 (Community Edition Tutorial) Conclusion

In this guide, we have shown you how to install MongoDB Community Edition on Ubuntu 20.04. MongoDB is an open source document database that provides high performance, high availability, and easy scalability.

MongoDB can be used to power next generation applications through its robust features and native read/write interface for JavaScript, making it a powerful choice for web developers and entrepreneurs.

Explore our other content on MongoDB if you want to know more.

Avatar for Sobia Arshad
Sobia Arshad

Information Security professional with 4+ years of experience. I am interested in learning about new technologies and loves working with all kinds of infrastructures.

5 1 vote
Article Rating
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x