
MongoDB is an open-source NoSQL database that stores data in flexible, JSON-like documents. It supports dynamic data structures, ad-hoc queries, indexing, and real-time aggregation for efficient data analysis. Unlike traditional relational databases, which use tables and fixed schemas, MongoDB is highly scalable and suitable for handling unstructured or semi-structured data due to its document-based model with dynamic and hierarchical data structures.
This article explains how to install MongoDB on Ubuntu 24.04. You will configure the MongoDB service, secure the database, and create a sample database to perform essential CRUD operations.
Before you begin, you need to:
MongoDB is not available in the default package repositories on Ubuntu 24.04. Follow the steps below to add the MongoDB repository and install MongoDB.
Visit the MongoDB releases page and verify the latest version to install. For example, MongoDB 8.0.
Import the MongoDB GPG key.
The above command imports the MongoDB version 8.0 GPG key and adds it to your APT keyrings to ensure package authenticity.
Add the MongoDB repository to your APT repository sources.
Update the APT package index to apply the MongoDB repository changes.
Install MongoDB.
Verify the installed MongoDB version.
Your output should be similar to the one below.
MongoDB uses the mongod system service to run the database server and manage its runtime operations. Follow the steps below to enable the MongoDB service to automatically start at boot and manage its status.
Enable MongoDB to start automatically at boot.
Start the MongoDB service.
View the MongoDB service status to verify that it’s running.
Your output should be similar to the one below.
MongoDB does not require authentication by default, which is insecure and exposes your data to unauthorized users. Follow the steps below to create an administrative user, and enable authentication to secure the MongoDB database server.
Access the MongoDB shell.
Switch to the admin database.
admin is the default administrative database for user management and system-level operations.
Create a new administrative user with full privileges to access any database. Replace mongodbadmin with your desired username and enter a strong password when prompted.
Your output should be similar to the one below.
List all MongoDB users and verify that the mongodbadmin user is available.
Your output should be similar to the one below.
Exit the MongoDB shell.
Open the MongoDB configuration file to enable authentication.
Find the #security: section, remove the (#) comment symbol, and change the authorization status to enabled.
Save and close the file.
Restart the MongoDB service to apply the configuration changes.
Accessing MongoDB through its interactive shell allows you to manage databases, users, and data directly. Follow the steps below to log in, create a database, add users, and perform basic CRUD (Create, Read, Update, Delete) operations.
Log in to the MongoDB shell as the mongodbadmin admin user you created earlier.
Enter the administrator password you set earlier when prompted.
Create a new vultr_example_db database.
Create a new vultruser user with full privileges to the vultr_example_db database.
Enter a strong password when prompted.
Your output should be similar to the one below.
Exit the MongoDB shell.
Log in to the MongoDB shell as vultruser and enter the password when prompted.
Switch to the vultr_example_db database.
Insert a new document into the messages collection with Greetings from Vultr message.
Your output should be similar to the one below.
View all documents in the messages collection and verify that the new document is available.
Your output should be similar to the one below.
Update the message from Greetings from Vultr to Greetings from Vultr Docs in the existing document.
Your output should be similar to the one below.
View all documents in the messages collection to verify the update.
Your output should be similar to the one below.
Delete the document from the messages collection.
Your output should be similar to the one below.
Exit the MongoDB shell.
You have installed MongoDB on Ubuntu 24.04, secured the database server with authentication, and performed database operations. You created a database, a user with administrative privileges, and performed CRUD operations, including inserting, updating, and deleting documents. For more information and configuration options, visit the MongoDB Documentation.
0 Comments
Be the first to comment and share your perspective with the community.