Introduction
MongoDB is a popular, open-source NoSQL database known for its high performance, high availability, and easy scalability. It stores data in JSON-like documents, making it a flexible and scalable choice for many developers and organizations. MongoDB is suitable for a variety of applications, from web development to big data projects and other data-intensive tasks. It provides a robust solution for your data management needs.
This guide shows you how to install MongoDB on Ubuntu 24.04
Prerequisites
Before you begin, ensure you've:
- An Ubuntu 24.04 server. We recommend a Digital Ocean VPS server.
- A non-root user with sudo privileges.
Install MongoDB
In this section, you will update your package index to ensure you have the latest information on the newest versions of packages and their dependencies.
- SSH to your Ubuntu server.
-
Update the package information index.
CONSOLE$ sudo apt update
-
Download and install the MongoDB packages.
CONSOLE$ sudo apt install -y mongodb
-
Verify the installation by checking the MongoDB version.
CONSOLE$ mongod --version
Manage MongoDB Service
In this section, you will manage the MongoDB service using systemctl
.
-
Start the MongoDB service.
CONSOLE$ sudo systemctl start mongodb
-
Enable MongoDB to start on boot.
CONSOLE$ sudo systemctl enable mongodb
-
Check the status of the MongoDB service.
CONSOLE$ sudo systemctl status mongodb
Understand MongoDB Directory Structure
This section provides an overview of the MongoDB directory structure, including configuration, error, and log files.
- MongoDB stores its data files in the
/var/lib/mongodb
directory. - Configuration files are located in the
/etc/mongodb.conf
file. - Log files are stored in the
/var/log/mongodb
directory.
Create Sample Database
In this section, you will create a sample database named company
, set up a products
collection, and insert sample documents into the collection.
-
Open the MongoDB shell.
CONSOLE$ mongo
-
Create a sample
company
database .MQLuse company
-
Create a sample
products
collection.MQLdb.createCollection("products")
-
Insert sample documents into the
products
collection.MQLdb.products.insertMany([ { item: "laptop", qty: 50, price: 799 }, { item: "phone", qty: 100, price: 599 }, { item: "tablet", qty: 80, price: 399 } ])
-
Query the products collection to retrieve the inserted documents.
MQLdb.products.find()
Conclusion
You have successfully installed MongoDB on Ubuntu 24.04 and created a sample database. You have also learned how to manage the MongoDB service, reviewed the directory structure, and inserted sample documents into a collection.