"Experienced Lead Developer oriented towards problem-solving and analyzing impacts, both technically and functionally, Enjoy's working with web technologies with a strong focus on quality code and robust system architecture."
HomeBlogWeb DevelopmentHow to Install MongoDB on Ubuntu [Step-by-Step]
Setting up MongoDB on Ubuntu turned out to be more challenging than I expected. If you're like me and still searching for a detailed guide on installing MongoDB on Ubuntu, you're in the right spot.
In this blog, I'll share my experience and guide you through the process of installing MongoDB on Ubuntu. I aim to make the installation as smooth as possible by providing an easy, step-by-step approach..
Let us start with the basics before we dive deeper to understand the tools and techniques involved. If you are looking forward to becoming a Certified MongoDB practitioner, please check out the MongoDB certification price.
In this guide, we will install MongoDB 6.0 Community Edition. In case you want to install a different version, you can use the version drop-down menu in the upper-left corner of the MongoDB community page to select the documentation for the required community version.
To get started, let us refresh our understanding of MongoDB first. MongoDB is a popular NoSQL database that is open-source and document-oriented. 'NoSQL' here implies that it is a non-relational database, i.e., it stores the data in a different format other than the relational tables and therefore does not require a fixed schema. The storage format in which the data is stored is known as BSON (Binary JavaScript Object Notation). This binary structure encodes the information allowing it to be parsed much more quickly, thus allowing a highly scalable and flexible document structure.
Though there is more to it than meets the eye, if you want to learn more about this powerful database you can check out MongoDB DBA online training.
Ubuntu is a Linux-based operating system that is designed for computers, smartphones, and network servers.
It is developed by a UK-based company called Canonical Ltd. All the principles used to develop the Ubuntu software are based on Open-Source software development.
The following are some of the significant features of Ubuntu −
Some Advantages of using Ubuntu in your system are as follows:
Some considerations before getting into the installation of MongoDB on Ubuntu.
MongoDB 6.0 Community Edition supports the following 64-bit Ubuntu LTS releases:
MongoDB only supports the 64-bit versions of these platforms.
MongoDB 6.0 Community Edition also supports the ARM64 architecture on Ubuntu.
It is highly recommended to consider the Production Notes document before deploying MongoDB in a production environment; they provide performance considerations and configuration recommendations for production deployments.
These instructions will use the official MongoDB-org package to install MongoDB Community on your Ubuntu system, which is maintained and supported by MongoDB Inc. The most recent version of MongoDB is always included in the official MongoDB-org package, which is accessible from a dedicated repository.
Some Prerequisite knowledge is needed for you to easily carry out the installation, like,
Now let us dive into the step-by-step process of installing MongoDB on Ubuntu, we will try to analyze every step to understand what it is used for. MongoDB certification can give you a deeper understanding on the workings of MongoDB, check out here KnowledgeHut’s MongoDB certification price.
The first step is to import the MongoDB public GPG Key from mongoDB.
On a terminal, issue the following command for the same:
> wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
The operation should respond with an OK.
However, if you get an error implying that GnuPG is not installed, you can follow the below steps:
1. Use the following command to Install GnuPG and its required libraries:
sudo apt-get install gnupg
2. Once installation is done, retry importing the key:
> wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
Step two is to create the list file /etc/apt/sources.list.d/mongodb-org-6.0.list for your version of Ubuntu.
Click on the appropriate tab for your version of Ubuntu. In case you are unsure of what Ubuntu version is running on the host, open a terminal or shell on the host machine and execute the command to check mongodb version ubuntu:
> lsb_release -dc.
The following set of instructions is to install MongoDB Ubuntu 20.04 (Focal) as an example
Create the /etc/apt/sources.list.d/mongodb-org-6.0.list file for Ubuntu 20.04 (Focal):
> echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
Here we will try to reload the local package database using the following command on a terminal:
> sudo apt-get update
You can install any specific version of MongoDB, though for now, we will be installing the latest stable version of MongoDB. To install the latest stable version, issue the following command:
> sudo apt-get install -y mongodb-org
Although you can specify any available version of MongoDB, the `apt-get` command will upgrade the packages when a newer version becomes available.
Optionally, if you wish to install a certain release, you will have to specify each component package individually along with the version number, as can be seen in the below example:
> sudo apt-get install -y mongodb-org=6.0.1 mongodb-org-database=6.0.1 mongodb-org-server=6.0.1 mongodb-mongosh=6.0.1 mongodb-org-mongos=6.0.1 mongodb-org-tools=6.0.1
If you exclusively install MongoDB-org=6.0.1 without the component packages, the latest version of each MongoDB package will be installed irrespective of the version specified by you.
The above commands will install several packages containing a stable version of MongoDB and some helpful management tools for the MongoDB server.
The installation process will start the MongoDB server automatically, but it's good to verify that the service is started and that the database is working.
First, check the service's status by using the below command
> sudo systemctl status mongodb
You should see the below output:
According to systemd, the MongoDB server is up and running.
We can verify this practically by connecting to the database server and executing a diagnostic command as below:
> mongo --eval 'db.runCommand({ connectionStatus: 1 })'
The output of this command will be the server address and port, the current database version, and the output of the status command:
Next, let us check out how to manage the server instance.
MongoDB is installed as a systemd service; therefore, you can manage it using standard systemd commands side-by-side with all other system services in Ubuntu.
We can also verify the status of the service by using the below command:
> sudo systemctl status mongodb
You can stop the server anytime by typing:
> sudo systemctl stop mongodb
To start MongoDB ubuntu server when it is stopped, type:
> sudo systemctl start mongodb
Once you've set up MongoDB as a service, you now need to launch your MongoDB installation.
After you've configured MongoDB as a service, you'll need to launch your MongoDB installation. Let us do this by opening up the Mongo Shell and switching to the database admin mode using the following two commands:
> mongo > use admin
Now here we will create a root user for your MongoDB installation and simultaneously exit the Mongo Shell using the below command:
> db.createUser({user:"admin", pwd:"password", roles:[{role:"root", db:"admin"}]})
Let us now connect with the MongoDB server using our newly created credentials, by first restarting the server and then using the following line of code:
> mongo -u admin -p admin123 --authenticationDatabase admin
And if everything works fine, you should be able to see the successful connection and to check the same we can use the "show dbs" command to see all the databases.
> show dbs
To run MongoDB Community Edition on your machine, follow the below instructions. These instructions assume that you are utilizing the official mongodb-org package and the default settings rather than the unofficial MongoDB package given by Ubuntu.
Init System
Here we will use our operating system's built-in init system to run and control the mongod process on the host machine. The recent Linux versions utilize systemd (through the systemctl command), but previous Linux versions use System V init (which uses the service command).
If you're not sure which init system your platform uses, try the below command can help you out:
> ps --no-headers -o comm 1
Then select the appropriate tab below based on the result:
Warning: All databases and their respective configurations will be removed after this process completes. Before initiating this step, create a backup of all your data and configuration information, considering it is irreversible.
To uninstall MongoDB Ubuntu, you first need to remove the MongoDB packages and to do so, and you need to stop any running instance of MongoDB. The following section will guide you through the steps to remove MongoDB from ubuntu.
Stop the MongoDB process by issuing the following command:
> sudo service mongod stop
Remove any MongoDB packages that you had previously installed using the following command.
> sudo apt-get purge mongodb-org*
Remove MongoDB databases and log files.
> sudo rm -r /var/log/mongodb > sudo rm -r /var/lib/mongodb
This is how you can uninstall MongoDB on Ubuntu.
Unlock your coding potential with the best Python programming certification. Master the language that powers AI, web development, and more. Start your journey today!
In this article, I have described the steps on how to install MongoDB on Ubuntu. I also provided information on how to manage the server instance, create a root user and password, run MongoDB Community Edition, and uninstall MongoDB Community Edition. I hope this guide has been helpful. If you have any questions, please feel free to leave a comment below.
Yes, but this MongoDB package provided by Ubuntu isn't maintained by MongoDB Inc. and conflicts with the official MongoDB-org package.
If you have installed via the package manager, the data directory /var/lib/mongodb and the log directory /var/log/mongodb are created during the installation. MongoDB runs using the MongoDB user account by default.
MongoDB is installed as a systemd service, therefore, you can manage it using standard systemd commands side-by-side all other system services in Ubuntu.
To verify the status of the service, type:
sudo systemctl status mongodb
MongoDB stores data on the host disk as BSON, the path directory usually is /data/db. There should be two files per collection, collection. 0, which stores the data (and that integer is then incremented as per need) and collection.
To Start MongoDB command Ubuntu, open up your command prompt and type mongod to start the MongoDB server.
Name | Date | Fee | Know more |
---|