MongoDB is a leading NoSQL database and popular for its flexibility and scalability. Installing MongoDB on Ubuntu 24.04 serves several purposes, particularly for developers and organizations leveraging modern web applications. MongoDB allows for schema-less data storage which is highly scalable and adaptable to complex data types and structures.
With the release of Ubuntu 24.04, developers and system administrators are looking for reliable methods to install MongoDB on their systems.
This tutorial will teach the step-by-step procedure to install MongoDB on Ubuntu 24.04.
- How to Install MongoDB on Ubuntu 24.04
- How to Use MongoDB on Ubuntu 24.04
- How to Uninstall MongoDB from Ubuntu 24.04
- Conclusion
How to Install MongoDB on Ubuntu 24.04
The most recommended way to install MongoDB is through the official MongoDB repository. It ensures you receive the most recent and latest stable version and updates. These steps install the latest 7.0 version of MongoDB.
To install MongoDB on Ubuntu 24.04, follow the below steps:
Step 1: Import the MongoDB Public GPG Key
To ensure the validation of the MongoDB packages, first, import the official GPG key using the “curl” command:
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | sudo gpg --dearmor -o /usr/share/keyrings/mongodb-server-7.0.gpg
Note: By default, curl is not installed, users can install it via the below command:
sudo apt install curl
Users can verify whether the key is successfully imported or not by going to this directory:
cd /usr/share/keyrings
Step 2: Add the MongoDB Repository
Now, create a list file for MongoDB in your sources list directory with the repository URL:
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
Step 3: Update Package Index
Now, users need to update the local package database after adding the MongoDB repository. This refreshes your local package database and prepares your system to install the latest MongoDB version:
sudo apt update
Step 4: Install MongoDB
After updating the package list, install MongoDB through the “mongodb-org” package name. With the repository in place and your package index updated, proceed to install MongoDB:
sudo apt install mongodb-org
Step 5: Check MongoDB
To check the MongoDB on Ubuntu 24.04, use the “version” utility with the “mongod” command as below:
mongod --version mongosh --eval 'db.runCommand({ connectionStatus: 1 })'
Step 6: Start and Enable MongoDB Service
After installation, start the MongoDB service and enable it to start on boot:
sudo systemctl start mongod
sudo systemctl enable mongod
Furthermore, to stop/halt the MongoDB service, use the “systemctl” command:
sudo systemctl stop mongod
If you want to start it again, type the “systemctl” command with the “restart” utility:
sudo systemctl restart mongod
Step 7: Verify the Installation
Check that MongoDB is installed correctly and is running, and execute the “status” utility as below:
sudo systemctl status mongod
Note: If users face “mongo” not found or command “mono” from deb mono-runtime errors, start and enable the MongoDB services by following Step 4. Or use the “sudo systemctl daemon-reload” command.
In addition, if there is an error during installation or operation, the error log can be found at “/var/log/mongodb/mongod.log”. This file has information about what went wrong.
Once everything is set up, begin using MongoDB by entering the “mongosh” into the terminal:
mongosh
How to Configure/Use MongoDB on Ubuntu 24.04?
To manage the MongoDB service on Ubuntu, use the “sudo systemctl start mongod” command to start the service. Furthermore, utilize the “sudo systemctl status mongod” command to check its status.
Creating a MongoDB database and user is a straightforward process that involves initializing the database environment. After that, accessing the MongoDB shell, and executing the necessary commands to set up the database and user with the desired permissions.
Step 1: Access MongoDB Shell
Access the MongoDB shell using the command line on your local system or through the MongoDB Atlas interface. This shell is where you will execute commands to manage your database.
Step 2: Create the Database
In MongoDB, a database is not explicitly created; it is created automatically when you store data in a collection within that database.
show db
To switch/create a database, use the command “use <database_name>”. It is created when you insert data into a collection. Let’s, create a database called “ilf” by running the command:
use ilf
Step 3: Create a User
Now, creating users with appropriate roles and permissions is crucial. For instance, create a new user with a password and assign roles:
db.createUser({
user: "ilfUser",
pwd: "1212",
roles: [
{ role: "readWrite", db: "ilf" },
{ role: "read", db: "local" }
]
})
Note: Users can replace “ilfUser`, “1212”, “ilf”, and “local” with the desired username, password, and database names.
Step 4: Test the User
After creating the user, users can test the permissions. Ensure that the user can perform the assigned roles without any issues:
db.getUsers();
For basic database operations, use commands
MongoDB Operations | Commands |
---|---|
To switch to a specific database | use <database_name> |
To list all databases | show dbs |
To delete the user | db.dropUser method |
Create a new collection | db.createCollection(“collectionName”) |
Drop an existing one | db.collectionName.drop() |
To insert documents for a single document | db.collectionName.insertOne({key: ‘value’}) |
Querying documents | db.collectionName.find({query}) |
Update documents | db.collectionName.updateOne({query}, {update}) |
For deleting documents | db.collectionName.deleteOne({query}) |
How to Uninstall/Remove MongoDB from Ubuntu 24.04
To uninstall MongoDB from Ubuntu 24.04, you can follow several methods. The most straightforward approach is to use the “apt” command with the “purge” option, which removes the packages along with their configuration files.
Stopping MongoDB Service
Begin by stopping the MongoDB service with the “systemctl” command:
sudo systemctl stop mongod
Remove MongoDB Packages
To remove all MongoDB packages execute the “purge” option by mentioning the “mongodb-org” package name:
sudo apt purge mongodb-org
Remove Data Directories and Logs
Afterward, it’s important to remove the remaining data directories and logs with
sudo rm -r /var/log/mongodbsudo rm -r /var/lib/mongodb
Deleting MongoDB Repository
If you’ve added a MongoDB repository, you should also remove it by deleting the corresponding file in /etc/apt/sources.list.d/.
cd /etc/apt/sources.list.d/
sudo rm mongodb-org-7.0.list
Remove Unused Dependencies
For a thorough cleanup, check for any remaining MongoDB user accounts remove them, and then run the below command:
sudo apt autoremove
Conclusion
To install MongoDB on Ubuntu 24.04, import the MongoDB public GPG key to ensure the validity of the packages. Then, add the MongoDB repository to your system’s software sources. After that, update the package database and install the MongoDB package through the “apt” package manager. Once installed, start and enable the MongoDB service for running on the system boot. Finally, verify the installation and, if necessary, connect to the MongoDB server to begin using the database.
Check our LinkedIn company page