MongoDB is a free and open-source document database. It belongs to a family of databases called NoSQL, which is different from the traditional table-based SQL databases like MySQL and PostgreSQL.
What is MongoDB?
![What is MongoDB?](https://digitaltreed.com/influencer/wp-content/uploads/2023/12/2394312-1024x341.jpg)
MongoDB is a free and open-source document database. It belongs to a family of databases called NoSQL, which is different from the traditional table-based SQL databases like MySQL and PostgreSQL.
In MongoDB, data is stored in flexible, JSON-like documents where fields can vary from document to document. It does not require a predefined schema, and the data structure can be changed over time.
Features and Advantages of MongoDB
- Offers high scalability and flexibility; automatic failover and data redundancy
- Offers an expressive query language that is simple to learn and use
- Ad-hoc queries for real-time analytics
- It supports arrays and nested objects as values and allows for flexible and dynamic schemas.
- It is easy to compose queries that allow sorting and filtering, no matter how nested and supports aggregation, geo-location, time-series, graph search, and more.
- Supports sharding which enables splitting of large datasets across multiple distributed collections which then eases querying.
- Supports multiple storage engines
This tutorial describes how to install and configure MongoDB Community Edition on Ubuntu 20.04.
How to Install and Configure MongoDB on Ubuntu [Step-by-Step]
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 of the workings of MongoDB, check out KnowledgeHut’s MongoDB certification price.
Step 1: Import the public key used by the package management system
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 –
Copy Code
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
Copy Code
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 –
Copy Code
Step 2: Create a list file for MongoDB
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.Â
Copy Code
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
Copy Code
Step 3: Reload the local package database
Here we will try to reload the local package database using the following command on a terminal: > sudo apt-get update
Copy Code
Step 4: Install the MongoDB packages
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
Copy Code
Although you can specify any available version of MongoDB, the `apt-get` command will upgrade the packages when a newer version becomes available.
Install a specific release of MongoDB
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
Copy Code
You should see the below output:
![](https://www.knowledgehut.com/_next/image?url=https%3A%2F%2Fd2o2utebsixu4k.cloudfront.net%2Fmedia%2Fimages%2F1667558612342-install-mongo-on-ubuntu-1.png&w=3840&q=75)
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({ connection status: 1 })’
Copy Code
The output of this command will be the server address and port, the current database version, and the output of the status command:
![](https://www.knowledgehut.com/_next/image?url=https%3A%2F%2Fd2o2utebsixu4k.cloudfront.net%2Fmedia%2Fimages%2F1667558630384-install-mongo-on-ubuntu-2.png&w=3840&q=75)
Next, let us check out how to manage the server instance.
How to Check MongoDB is Installed?
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
Copy Code
You can stop the server anytime by typing: > sudo systemctl stop MongoDB
Copy Code
To start the MongoDB ubuntu server when it is stopped, type: > sudo systemctl start MongoDB
Copy Code
Create MongoDB Database Root User and Password
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
Copy Code
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”}]})
Copy Code
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
Copy Code
![](https://www.knowledgehut.com/_next/image?url=https%3A%2F%2Fd2o2utebsixu4k.cloudfront.net%2Fmedia%2Fimages%2F1667558647373-install-mongo-on-ubuntu-3.png&w=3840&q=75)
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
Copy Code
Run MongoDB Community Edition
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
Copy Code
Then select the appropriate tab below based on the result:
- systemd – select the systemd (systemctl) tab below.
- init – select the System V Init (service) tab below.
Uninstall MongoDB Community Edition
![Uninstall MongoDB Community Edition](https://digitaltreed.com/influencer/wp-content/uploads/2023/12/2899901-1024x682.jpg)
Warning: All databases and their respective configurations will be removed after this process is completed. 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, you need to stop any running instance of MongoDB. The following section will guide you through the steps to remove MongoDB from Ubuntu.Â
1. Stop MongoDB
Stop the MongoDB process by issuing the following command: > sudo service mongod stop
Copy Code
2. Remove Packages
Remove any MongoDB packages that you had previously installed using the following command. > sudo apt-get purge mongodb-org*
Copy Code
3. Remove Data Directories
Remove MongoDB databases and log files. > sudo rm -r /var/log/MongoDB > sudo rm -r /var/lib/MongoDB
Copy Code
![](https://www.knowledgehut.com/_next/image?url=https%3A%2F%2Fd2o2utebsixu4k.cloudfront.net%2Fmedia%2Fimages%2F1667558666473-install-mongo-on-ubuntu-4.png&w=3840&q=75)
This is how you can uninstall MongoDB on Ubuntu.
Conclusion
This article guides on how MongoDB can be installed on Ubuntu with ease. Moreover, I hope it provides you the in-depth knowledge about the principles underlying each step to help you understand and implement them efficiently.
Leave a Reply