
How to Install, Enable, and Use MongoDB on Ubuntu 22.04 LTS?
MongoDB is a NoSQL database that is popularly used to store an extensive amount of data, particularly for advanced web applications. It is free to use and operates across several platforms, such as MacOS, Linux, etc. It is different from traditional/relational databases as it doesn’t use tables to organize data. Instead, it uses documents and collections to store data. These documents keep data in JSON format, which is a simple and organized way to store data.
In simple terms, MongoDB’s flexible structure makes it ideal for developers. However, to use MongoDB, users need to learn how to install it on their desired operating system.
This write-up demonstrates the installation process, starting and stopping MongoDB services, creating an administrator user, and verifying the user creation.
How to Install MongoDB on Ubuntu 22.04 LTS?
Let’s explore the complete process of installing MongoDB on Ubuntu 22.04 using the below-demonstrated steps:
Step 1: Perform System Update
Launch the Terminal by pressing the “CTRL + ALT + T” shortcut key to install the latest version of MongoDB on Ubuntu 22.04. However, before heading into MongoDB’s installation, don’t forget to update the system packages:
sudo apt update |
Step 2: Import the MongoDB Public Key
Before installing the latest version of MongoDB on your Ubuntu 22.04 system, you need to import its public key. For this purpose, execute the below-given command:
curl -fsSL https://www.mongodb.org/static/pgp/server-6.0.asc|sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/mongodb-6.gpg |
Upon successful execution of the above-stated command, the cursor will move to the next line without showing any error in the output:
Step 3: Add MongoDB Repository
Now, execute the below-stated command to add/configure the MongoDB repository on your Ubuntu 22.04 system:
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu (lsb_release -cs)/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list |
Step 4: Perform System Update & Install MongoDB
Now, we are all set to install MongoDB on Ubuntu 22.04. But before that perform the system update by executing the following command:
sudo apt update |
Once you are done with the system update, execute the below-provided “sudo” command to install MongoDB on Ubuntu 22.04:
sudo apt install mongodb-org |
Step 5: Verify MongoDB Installation
After the installation is finished, you can verify the installed version of MongoDB by executing the following command:
mongod --version |
The following screenshot confirms the MongoDB installation:
How to Enable MongoDB on Ubuntu 22.04 LTS?
After successfully installing MongoDB, you can utilize the below-stated commands to start and enable the MongoDB services:
sudo systemctl start mongod.service sudo systemctl enable mongod.service |
Let’s check the status of the MongoDB service to ensure if it is running appropriately:
sudo systemctl status mongod |
How to Use MongoDB on Ubuntu 22.04 LTS?
Now utilize the following command to launch MongoDB Shell:
mongosh |
Switch to Admin Database
Within the mongo shell, enter the below-stated command to switch to the admin database:
use admin |
The below screenshot demonstrates that the database has been successfully switched:
Create Admin User
Let’s proceed to create an admin username and set a corresponding password by executing the given command:
db.createUser({user:"linuxuser", pwd:"mypassword", roles:[{role:"root", db:"admin"}]}) |
The following snippet confirms the successful creation of the admin user:
MongoDB users can also verify the creation of the user by executing the following command:
db.system.users.find() |
That’s all about installing and using MongoDB on Ubuntu 22.04.
Conclusion
To install MongoDB on Ubuntu 22.04 LTS, import the MongoDB public key, add MongoDB repository, and install it by executing the “sudo apt install mongodb-org” command. Use the “mongod –version” command to verify the installed version of MongoDB. This post has provided a comprehensive, step-by-step guide on how to install and utilize MongoDB on Ubuntu 22.04.