In this article, we are going to cover How To Install MongoDB Server on Ubuntu 24.04 LTS and create a database in MongoDB server.
MongoDB is a flexible and scalable NoSQL database that uses JSON-like documents, ideal for modern web apps. This guide covers installing MongoDB on Ubuntu 24.04, from adding the official repository to managing the service, ensuring a robust setup for your development needs. By the end, you’ll have a fully functional MongoDB installation.
Table of Contents
Prerequisites:
- SSH Access with admin privileges
- Ubuntu 24.04 LTS with minimal installation
Update system and add Repository
update the system packages on Ubuntu 24.04 LTS
sudo apt update
Install curl on Ubuntu 24.04 LTS
sudo apt-get install gnupg curl
import MongoDB GPG Key
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor
Create the MongoDB Source List in Ubuntu 24.04 LTS
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
Update the Package Database
sudo apt-get update
Install MongoDB Server on Ubuntu 24.04 LTS
Install MongoDB server on Ubuntu 24.04 LTS
sudo apt-get install -y mongodb-org
Start MongoDB Service
sudo systemctl start mongod
Enable MongoDB Service
sudo systemctl enable mongod
Verify the Installation
mongod --version
To restart Mongodb server on Ubuntu 24.04 LTS
sudo systemctl restart mongod
To stop Mongodb server on Ubuntu 24.04 LTS
sudo systemctl stop mongod
Check error logs in mongodb server
sudo tail -f /var/log/mongodb/mongod.log
Create Database in MongoDB Server
login mongodb on Ubuntu 24.04 LTS using command line
mongosh
Let’s create a sample database called Numbers:
use numbers
Check the currently selected database
db
Insert Data in MongoDB database
You can add a document to your database using below command
db.collection.insert()
To list all created databases , use the following command
show dbs
Use below command to drop an existing database.
db.dropDatabase()
Now check list of databases by using below command
show dbs
We can confirm the database was deleted.
Conclusion:
Installing MongoDB on Ubuntu 24.04 is a straightforward process that involves adding the official MongoDB repository, updating the package list, and installing the MongoDB package. Once installed, you can start the MongoDB service and access the MongoDB shell to interact with the database.
By following these steps, you can set up MongoDB on your Ubuntu 24.04 system and utilize its powerful data storage and management features.
Related Articles:
MySQL Database Backups using Python Script
Reference:
Install MongoDB Community Edition on Ubuntu mongodb official page