DEV Community

Cover image for A Beginner's Guide: Installing MongoDB on Ubuntu 🔥 in 5 Simple Steps 🚀
rising_segun
rising_segun

Posted on

A Beginner's Guide: Installing MongoDB on Ubuntu 🔥 in 5 Simple Steps 🚀

Looking for a Hassle-Free MongoDB Installation on Ubuntu? Your search ends here! Follow this comprehensive step-by-step guide to effortlessly set up your MongoDB database on any Ubuntu or Linux-based system.

By the end of this article, you’ll have mastered installing MongoDB on Ubuntu effortlessly. This comprehensive guide not only offers a step-by-step walkthrough but also delves into the tools and techniques, providing valuable insights for seamless execution.

Prerequisites

To successfully install MongoDB on your Linux-based system, the following must be done:

  • knowledge of MongoDB
  • A general knowledge of working with command line/shell commands
  • Ubuntu or other Linux-based operating systems on the host workstation.

Steps to Install MongoDB on Ubuntu

MongoDB installation on Ubuntu is a simple process that allows you to set up a powerful and versatile NoSQL database on your system. By following a few simple steps, you can have MongoDB up and running in no time.

Step 1: Import MongoDB Repositories

It is important to ensure the legitimacy and integrity of the packages when installing MongoDB on Ubuntu. Ubuntu's Package Management system uses GPG keys to validate package signatures, adding an extra layer of security. To begin the MongoDB installation, you must first import the MongoDB Public GPG key into your Ubuntu system. The MongoDB Public GPG key can be imported with the following terminal command:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
Enter fullscreen mode Exit fullscreen mode

Create a Source list for your MongoDB installation next. To accomplish this, use the following command to create the "/etc/apt/sources.list.d/mongodb-org-3.4.list" list file:

echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
Enter fullscreen mode Exit fullscreen mode

With your list file now created, you can install the Local Package repository. To do this, you can use the following command:

sudo apt-get update
Enter fullscreen mode Exit fullscreen mode

Step 2: Installing MongoDB Packages

You now need to install the latest stable version of MongoDB on your system. Use the below command:

sudo apt-get install -y mongodb
Enter fullscreen mode Exit fullscreen mode

If you wish to install a certain version of MongoDB on your system, you must specify the version for each component package when you install it. For example, installing a specific version. run the following command:

sudo apt-get install -y mongodb-org=3.4 mongodb-org-server=3.4 mongodb-org-shell=3.4 mongodb-org-mongos=3.4 mongodb-org-tools=3.4
Enter fullscreen mode Exit fullscreen mode

Step 3: Launching MongoDB as a Service on Ubuntu

Now that MongoDB is up and running, you must build a Unit file to assist your system in understanding the resource management process. For example, the most widely used Unit file determines how to start, stop, or manage a service automatically.
To do this, you can create a configuration file, “mongodb.service in /etc/systemd/system”, that will help manage the MongoDB system.

sudo vim /etc/systemd/system/mongodb.service
Enter fullscreen mode Exit fullscreen mode

Next, copy the following information in your configuration file:

config file

Now that the configuration file has been produced, use the following command to update the system service:

systemctl daemon-reload
Enter fullscreen mode Exit fullscreen mode

Next, start the updated systemd service for your MongoDB instance:

sudo systemctl start mongodb
Enter fullscreen mode Exit fullscreen mode

Once the instance is running, check to see if MongoDB launched on port 27017. To accomplish this, use the "netstat" command as follows:

netstat -plntu
Enter fullscreen mode Exit fullscreen mode

To confirm if your MongoDB instance started successfully, you need to use the status command as:

sudo systemctl status mongodb
Enter fullscreen mode Exit fullscreen mode

Now, you can now enable auto-start functionality for your system as follows:

sudo systemctl enable mongodb
Enter fullscreen mode Exit fullscreen mode

For an instance you want to start or restart the MongoDB instance running on your Ubuntu installation, run the below commands:

sudo systemctl stop mongodb
Enter fullscreen mode Exit fullscreen mode
sudo systemctl restart mongodb
Enter fullscreen mode Exit fullscreen mode

Step 4: Configuring and Connecting MongoDB

From the above steps, we have successfully installed MongoDB service. This step shows how to install MongoDB. To do this, open the Mongo Shell and switch to the database admin mode using the following command:

mongo
use admin
Enter fullscreen mode Exit fullscreen mode

Now, create a root user for your MongoDB installation and exit the Mongo Shell as follows:

db.createUser({user:"admin", pwd:”password", roles:[{role:"root", db:"admin"}]})
Enter fullscreen mode Exit fullscreen mode

Note: you can replace the user and pwd with your preferred choice.

with this setup, you can now connect with your MongoDB, by first restarting MongoDB and then using the following command:

mongo -u admin -p admin123 --authenticationDatabase admin
Enter fullscreen mode Exit fullscreen mode

you’ll now be able to see MongoDB set up a connection. You can use the “show dbs” command as follows to open a list of all available databases. With all these, you’ve successfully installed MongoDB on Ubuntu.

Step 5: MongoDB Tuning

Scaling MongoDB is simple and may be done horizontally or vertically. This is critical for the Database's optimal performance. Horizontal scaling involves the addition of server resources such as RAM and CPUs, whereas vertical scaling involves the addition of servers to the configuration. Several factors influence the performance of the MongoDB Database, including memory usage, the number of concurrent connections, and the WiredTiger Cache, among others. MongoDB's default storage engine is WiredTiger, which saves 50% of RAM. This indicates that 8GB of RAM will have a memory preserver of 0.5*(8-1) for WiredTiger. Use the following command to check use statistics and determine whether modifications are needed.

tuning
From the above result, some of the key points to note are listed below.

  • wiredTiger.cache.maximum bytes configure
  • wiredTiger.cache.bytes currently in the cache
  • wiredTiger.cache.pages read into cache
  • wiredTiger.cache.pages written from cache
  • wiredTiger.cache.tracked dirty bytes in the cache

To check the usage of WiredTiger Concurrency Read and Write Ticket, follow the command given below.

db

Conclusion

In just 5 straightforward steps, you've embarked on a journey to harness the power of MongoDB on your Ubuntu system. By following this beginner's guide, you've not only successfully installed MongoDB but also gained valuable insights into managing databases on Linux.
With MongoDB in your toolkit, you're well-equipped to handle diverse data needs and build robust applications. So, go ahead and explore the endless possibilities this NoSQL database offers. Whether you're a developer, a data enthusiast, or a tech enthusiast, MongoDB on Ubuntu is your gateway to efficient data management.
Start your MongoDB adventure today and witness your projects scale and thrive like never before!"

Top comments (0)