How to install MongoDB in Ubuntu server
Introduction
MongoDB is a well-known open-source NoSQL database for storing data. MongoDB uses flexible documents to store data; these documents are usually in the BSON (Binary JSON) format, which supports arrays and nested columns. Because of its horizontal scalability design, it can manage massive data volumes by dividing them over several servers or clusters. This scalability is essential for modern applications that need to handle large amounts of data. So MongoDB is commonly used in applications including content management systems, real-time analytics, mobile apps, and IoT applications where performance, scalability, and flexibility are crucial.
How to install MongoDB on the Ubuntu server.
Basic requirements:
- Minimum 4GB RAM
- Minimum 10GB free disk space
- Install only in 64-bit architectures.
- Use a minimum m5.large instance, if you are using AWS EC2.
Step 1. Installing cURL and GnuPG
Install curl and gnupg if they are not already installed.
sudo apt install curl gnupg
In the next step, import the public key to the Ubuntu server from https://pgp.mongodb.com/server-7.0.asc
curl -fsSL https://pgp.mongodb.com/server-7.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \
--dearmor
Add the MongoDB repository to the source list file. /etc/apt/sources.list.d/mongodb-org-7.0.list
echo "deb [ arch=amd64,arm64
signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ]
https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
Check whether the repository has been added using
cat /etc/apt/sources.list.d/mongodb-org-7.0.list
If this is correct, reload the local package database using
sudo apt update
Step 2. Install MongoDB
MongoDB can be installed using the following command:
sudo apt install -y mongodb-org
To confirm the installation, check the MongoDB version using:
mongod --version
Step 3. Start and Enable MongoDB
Firstly, check the status of MongoDB service; by default, the status will be disabled.
Status can be checked using the following command:
sudo systemctl status mongod
As the service is inactive and disabled, it needs to be started and enabled.
To start MongoDB use the command:
sudo systemctl start mongod
Now check the status again. So the status will show as active (running).
Now enable the service using the command below:
sudo systemctl enable mongod
A service that is enabled will launch upon boot. When a service is disabled, it won’t launch automatically but can instead be manually launched or made dependent on another service.
Now you can verify whether the service is enabled or not by checking the status.
Step 4. Accessing the MongoDB shell (mongosh) and creating a database
The official MongoDB shell is called ‘mongosh’. You can access mongosh by entering the command:
mongosh
Creating a Database
Use the below command to list all databases:
show dbs
You can create a database using the ‘use’ command. Syntax: use <database_name> For example:
use newdb
Using the ‘show dbs’ command, you can verify if the database was created or not. Here we could see that the database we have just created is not showing up in the database list.
Sometimes databases without contents inside won’t show up on the list.
So add some content to the database. Here, for example, we use the following:
db.userdata.insertOne({ name: "example" })
Now we can see that the newly created database is showing up in the database list.
You can use the following commands:
db.dropDatabase(): To delete the current database
use <database_name>: If you need to switch to another database.
db: To display the name of the database you are currently in
exit: To exit the MongoDB shell
Conclusion
Developers and system administrators may easily harness the power of a stable, adaptable NoSQL database system by installing MongoDB on an Ubuntu server. You can easily set up MongoDB by following the instructions in this article, which include adding the official repository, installing the package, adding a database, and more. MongoDB’s robust features and simple installation process make it a vital tool in the modern tech stack, regardless of your needs—building applications, managing data at scale, or exploring the world of NoSQL databases. Accept the scalability and versatility that MongoDB provides, and give your projects the boost they need with a database solution made to tackle the demands of today’s data-driven environments.