Fixing “failed to start mongod.service: unit mongod.service not found” Error in MongoDB

DigitalOcean Referral Badge
Start your VPS now with FREE $100 credit.

MongoDB is a popular database engine used in conjunction with various scripting languages, particularly Python and Rails. Its strongest point is JSON-like data storage, which allows for the natural processing of data stored in the database.

The query used by MongoDB is also in the form of JSON, which allows users to construct complex queries easily. Although MongoDB is available for Windows, Linux, and Mac OS X, it is most commonly used with Debian or Ubuntu Linux.

However, in Ubuntu 15.10 and 16.04, MongoDB sometimes fails to start with the following error:

system> sudo systemctl start mongodb
Failed to start mongod.service: Unit mongod.service not found

What Caused It the Error?

This error stems from the fact that Ubuntu switched from upstart to systemd, a new and improved system init daemon, from version 15.04 onwards.

The first Ubuntu LTS (Long-Term Support) version that uses systemd is 16.04, causing some compatibility problems with systems upgraded from the previous 14.04 LTS version. Subsequent versions of Ubuntu will not have this compatibility problem as the systemd quirks are already fixed.

How to Fix

Fortunately, you can get MongoDB up and running in your Ubuntu 16.04 system easily by following the following instructions.

READ:  How to Fix Error Code 1054 Unknown Column in MySQL

The first step you need to do is to back up the MongoDB data as you will need to remove MongoDB Community completely. As MongoDB is not running, you cannot use mongodump to back up the data, and you will need to use cp command. Issue this following command:

cp -aRp /var/lib/mongo/ /home/<username>/mongodb_backup

And wait for it to complete. Afterwards, proceed to uninstall MongoDB completely by issuing these commands:

sudo service mongod stop
sudo apt-get purge mongodb-org*
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb

After uninstalling MongoDB, you can install it back again with these following commands:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
sudo apt-get update
sudo apt-get install -y mongodb-org

The next step is to fix and restore MongoDB service to systemd service list. Issue this following command:

sudo systemctl enable mongod

Afterwards, restore the MongoDB database backup you created by issuing this command:

cp -aRp /home/<username>/mongodb_backup /var/lib/mongo/

And voila! You can start MongoDB again easily with the following command:

sudo service mongod restart

In some systems, the daemon mongod is named mongodb, so if the command fails to locate mongod service, try using mongodb instead of mongod.

Leave a Comment