Steps to Resolve the Error
1. Remove Existing MongoDB Installation
First of all, make sure we completely remove any existing installation of MongoDB so as to avoid conflicts.
sudo apt-get purge mongodb-org*
sudo apt-get autoremove
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb
2. Import the MongoDB Public Key:
- Ensure that the necessary packages are installed by running the following command:
sudo apt-get install gnupg curl
- Then, import the MongoDB public GPG key with the following command:
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \
--dearmor
3. Create a List File for MongoDB:
- Depending on our Ubuntu version, create the appropriate list file. For example, for Ubuntu 22.04 (Jammy), run:
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
4. Reload Local Package Database:
- Update the local package database with the command:
sudo apt-get update
5. Install MongoDB Packages:
- Install the MongoDB package by executing:
sudo apt-get install -y mongodb-org
6. Start MongoDB Service:
- After installation, start the MongoDB service:
sudo systemctl start mongod
- If we encounter the error again “Failed to start mongod.service: Unit mongod.service not found ” run:
sudo systemctl daemon-reload
- Then try starting the service again.
7. Verify MongoDB Service Status:
- Check the status of the MongoDB service to ensure it has started successfully:
sudo systemctl status mongod
8. Optional: Enable Automatic Start on Boot:
- To ensure MongoDB starts automatically on system boot, enable the service:
sudo systemctl enable mongod
9. Additional Operations:
- Stop MongoDB: If needed, stop the MongoDB service using:
sudo systemctl stop mongod
- Restart MongoDB: Restart the MongoDB service with
sudo systemctl restart mongod
- Monitoring MongoDB Logs: Keep an eye on the MongoDB logs for any errors or important messages:
- tail -f /var/log/mongodb/mongod.log
10. Begin Using MongoDB:
- Start a mongosh session to interact with MongoDB:
mongosh
Failed to start mongod.service: Unit mongod.service not found
MongoDB a popular NoSQL database is famous for its flexibility and scalability. However, Ubuntu users may occasionally face difficulties when starting the MongoDB service, often encountering the error message: “Failed to start mongod.service: Unit mongod.service not found.”
In this article, We will learn about mongod.service: Unit mongod.service not found through troubleshooting and reinstalling MongoDB to resolve this issue efficiently.
Understanding the Error
- The message “Failed to start mongod.service: Unit mongod.service not found” means that either MongoDB is incorrectly installed or its configuration files are missing or damaged.
- For that reason, it cannot run which is crucial when working with MongoDB.
Common Causes
- Incomplete Installation: The installation of MongoDB may have been incomplete.
- Missing Service File: Missing or corrupted systemd service file for MongoDB.
- Outdated Package Database: Recognition issues may arise due to outdated local package database.
Contact Us