Hey there, data enthusiasts! 👋 Are you ready to dive into the exciting world of NoSQL databases? Trust me, you’ve come to the right place! In this step-by-step guide, we’ll embark on a journey to install MongoDB 8 on your Ubuntu 24.04 LTS system.
Think of MongoDB as a super-organized digital filing cabinet for your data. It’s flexible, scalable, and perfect for handling the massive amounts of information generated in our modern world. Whether you’re a seasoned developer or just starting out, I’m here to make the installation process smooth and dare I say, enjoyable! 😉
Let’s get this show on the road.
1. Prepping Your Ubuntu System: A Solid Foundation is Key
Before we dive into the exciting world of MongoDB, let’s make sure your Ubuntu system is primed and ready for action.
1.1. Updating Your System: Staying Fresh and Secure
First things first, we need to ensure your Ubuntu system is up-to-date. Think of this as giving your system a refreshing spring cleaning. We’ll be using the apt
package manager, your trusty sidekick for managing software on Ubuntu.
Open your terminal and run the following command:
sudo apt update
This command fetches the latest package lists from the repositories, ensuring you have access to the most recent versions of all your software, including MongoDB!
1.2. Installing Essential Packages: Gathering the Building Blocks
MongoDB relies on certain packages for a smooth installation and operation. Let’s grab those right now! Run the following command in your terminal:
sudo apt install gnupg curl software-properties-common apt-transport-https -y
Here’s a quick breakdown of what each package does:
gnupg
: This package provides tools for working with GPG keys, which we’ll use to verify the authenticity of the MongoDB packages.curl
: We’ll be usingcurl
to download files from the web, specifically the MongoDB GPG key.software-properties-common
: This handy package provides tools for managing software repositories on Ubuntu.apt-transport-https
: This package enablesapt
, our package manager, to securely communicate over HTTPS, ensuring the integrity of the packages we download.
2. Importing the MongoDB GPG Key: Safety First 🔐
Security is paramount in the digital world! Before we download and install any software, it’s crucial to verify its authenticity. We’ll do this by importing the MongoDB GPG key.
Run the following command to import the key:
curl -fsSL https://www.mongodb.org/static/pgp/server-6.0.asc | sudo gpg --dearmor -o /usr/share/keyrings/mongodb-server-6.0.gpg
This command retrieves the MongoDB GPG key from the official MongoDB website and adds it to your system’s keyring. Think of this as adding a security badge to your system, allowing it to recognize and verify official MongoDB packages.
3. Creating the MongoDB Repository File: Your Direct Line to MongoDB Updates
To make installing and updating MongoDB as smooth as possible, we’ll be adding the official MongoDB repository to your Ubuntu system. This repository acts as a central hub for all things MongoDB, ensuring you have access to the latest and greatest versions.
3.1 Ubuntu 24.04 (Jammy) and Later:
Execute the following command in your terminal:
echo "deb [signed-by=/usr/share/keyrings/mongodb-server-6.0.gpg] https://repo.mongodb.com/apt/ubuntu jammy/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
This command adds a new file called mongodb-org-6.0.list
to your system’s repository directory (/etc/apt/sources.list.d/
). This file contains the address of the official MongoDB repository for Ubuntu 24.04 (Jammy) and later.
4. Installing MongoDB: Time to Build🔨
With the repository in place, we’re finally ready to install MongoDB! 🎉 Run the following commands in your terminal:
sudo apt update
sudo apt install mongodb-org -y
Let’s break down these commands:
sudo apt update
: This command refreshes your system’s package lists, incorporating the newly added MongoDB repository. Think of it as syncing your system with the latest software information.sudo apt install mongodb-org -y
: This command installs themongodb-org
package, which contains all the essential components of MongoDB. The-y
flag automatically confirms any prompts during the installation, saving you a few keystrokes.
5. Managing the MongoDB Service: Taking Control
Now that MongoDB is successfully installed, let’s explore how to manage the MongoDB service.
5.1 Starting the MongoDB Service
To start the MongoDB service, use the following command:
sudo systemctl start mongod
This command tells systemd
, the service manager on Ubuntu, to start the MongoDB service ( mongod
).
5.2 Verifying the MongoDB Service Status
To check if the MongoDB service is running as expected, use the following command:
sudo systemctl status mongod
This command displays the status of the MongoDB service. You should see a green “active (running)” message, indicating that MongoDB is up and running.
5.3 Enabling MongoDB on System Startup
To ensure that MongoDB automatically starts every time you boot up your system, use the following command:
sudo systemctl enable mongod
This command tells systemd
to automatically start the MongoDB service during the system boot process. Now you won’t have to manually start MongoDB every time you restart your system!
6. Exploring the MongoDB Shell: Your Gateway to Data Exploration
Congratulations! You’ve successfully installed MongoDB. 🎉 Now, let’s dip our toes into the interactive MongoDB shell, your command center for all things MongoDB.
To access the MongoDB shell, type the following command in your terminal:
mongo
You’ll be greeted with a message welcoming you to the MongoDB shell. From here, you can start interacting with your MongoDB database.
7. Mastering Basic MongoDB Commands: Your First Foray into NoSQL
Let’s explore some fundamental MongoDB commands that will have you working with data in no time:
1. Listing Databases: Curious to see what databases are on your MongoDB server? Use the following command:
show dbs
2. Creating a Database: Time to create your first database! Replace <database_name>
with your desired database name:
use <database_name>
3. Switching to a Database: To switch to a specific database, replace <database_name>
with the database you want to work with:
use <database_name>
4. Inserting a Document: Let’s add some data! In MongoDB, data is stored as documents in BSON format, which is very similar to JSON. Replace <collection_name>
with your collection name and insert your desired data within the curly braces:
db.<collection_name>.insertOne({ field1: "value1", field2: "value2" })
5. Finding Documents: Time to retrieve some data! Replace <collection_name>
with your collection name. The following command retrieves all documents in the specified collection:
db.<collection_name>.find({})
6. Updating Documents: Need to modify existing data? Replace <collection_name>
with your collection name and use update operators like $set
to modify specific fields:
db.<collection_name>.updateOne({ field1: "value1" }, { $set: { field2: "new_value" } })
7. Deleting Documents: To remove documents from a collection, replace <collection_name>
with your collection name and specify the criteria for the documents you want to delete:
db.<collection_name>.deleteOne({ field1: "value1" })
8. Exiting the MongoDB Shell: To exit the MongoDB shell and return to your terminal, type:
exit
Conclusion
Congratulations, my friend. You’ve successfully installed MongoDB 8 on your Ubuntu 24.04 LTS system and even dipped your toes into the powerful MongoDB shell! You’re now equipped to explore the dynamic world of NoSQL databases and leverage MongoDB’s incredible flexibility and scalability.
Remember, this is just the beginning of your MongoDB journey. There’s a wealth of knowledge out there waiting to be discovered. Don’t hesitate to dive into the official MongoDB documentation, explore online tutorials, and join the vibrant MongoDB community. Embrace the power of NoSQL and build something truly extraordinary.
Read Also: