[mongodb] Failed to start mongod.service: Unit mongod.service not found

I follow all the steps mention in MongoDB installation documents for Ubuntu 16.04.

Steps 1:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5

Steps 2:

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

Steps 3:

sudo apt-get update

Steps 4:

sudo apt-get install -y mongodb-org

Steps 5:

sudo service mongod start

when I started MongoDB got an error as:

Failed to start mongod.service: Unit mongod.service not found.

This question is related to mongodb ubuntu ubuntu-16.04

The answer is


May be you are using a wrong version of mongodb list file. I faced this problem but it's my mistake when not selecting the right list file for Ubuntu 16.04. The default selected is for Ubuntu 14.04 and it's the reason for my error: "Failed to start mongod.service: Unit mongod.service not found." enter image description here


I just encountered this on my parrot os and this is how I solved it.

sudo service mongodb start

Well.... My answer may be considered naive but in fact it has been the only way MongoDB has work in my case, Ubuntu 19.10. I tried to run the commands from the most voted comments and none worked, when running:

mongod --repair

I got this alert:

The first time I used MongoDB that error appeared

With some research I found out that running the DB in another port could be a solution, then:

mongod --port 27018

And it works great for me every time. Long answer but wanted to give context before giving such a simple solution.

(If I'm doing it wrong or doesn't seem logical, plz tell me! Relevant for me)


For those that run into this and end up on this answer, as I did, where they got this error during uninstall orupgrade and Ubuntu keeps failing to uninstall the previous because the service doesn't exist this one line will get you past that and allow the uninstall or upgrade to continue.

sudo touch /lib/systemd/system/mongod.service

Just try with this command:

$ sudo systemctl enable mongod

Please follow the below steps, it should work.

1 - Uninstall current installation completely

Source - official instructions

sudo service mongod stop

Remove Packages

sudo apt-get purge mongodb-org*

Remove the folders

sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb

2 - Reinstall as described on official site, I will just write down the all steps. enter link description here

Import the public key

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5

Create a list file for Ubuntu 16.04

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

update the list

sudo apt-get update

Install the latest package

sudo apt-get install -y mongodb-org

3 - Now it should work, please try below command

sudo service mongod start

and check the status

mongo

it should appear the mongo shell


The following steps helped me solve the problem of not being able to start mongodb on Ubuntu Server 18.04 LTS

Step 1: First, remove MongoDB from previous if installed:

sudo apt remove --autoremove mongodb-org

Step 2: Remove any mongodb repo list files:

sudo rm /etc/apt/sources.list.d/mongodb*.list
sudo apt update

Step 3: Import the public key used by the package management system:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4

Step 4: Create a list file for MongoDB:

echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list

Step 5: Reload local package database:

sudo apt-get update

Step 6: Install the MongoDB packages:

sudo apt-get install -y mongodb-org

Step 7: Start MongoDB:

sudo service mongod start

Step 8: Begin using MongoDB:

mongo

Hope it helps you.


To solve the problem of not being able to start mongodb on ubuntu 16.04 1) look at mongodb log file enter image description here

2) we find that the error is due to "Failed to unlink socket file /tmp/mongodb-27017"

3) Look at the permission of file /tmp/mongdb-27017.lock and find that the owner is root instead of mongodb

4) Delete the /tmp/mongodb-27017.sock file manually and use the command "sudo chown mongodb:mongodb /tmp/mongodb*"

5) Start the service with systemcl and use netstat to check whther mongdob has been started on port 27017

enter image description here

Credit: https://www.mkyong.com/mongodb/mongodb-failed-to-unlink-socket-file-tmpmongodb-27017/ https://hevodata.com/blog/install-mongodb-on-ubuntu/


Most probably unit mongodb.service is masked. Use following command to unmask it.

sudo systemctl unmask mongod

and re-run

sudo service mongod start


As per documentation:

Run this command to reload the daemon:

sudo systemctl daemon-reload

After this you need to restart the mongod service:

sudo systemctl start mongod

To verify that MongoDB has started, run:

sudo systemctl status mongod

To ensure that MongoDB will start following a system reboot, run:

sudo systemctl enable mongod

It worked for me on ubuntu 20.04: In my case, mongod.service file was locked so it was giving me the same error. To resolve the issue:- Step 1: Use following command to check if the mongod.service is present there

cd /usr/bin/systemd/system
ls

Step 2: If the file is present there then Run the following command to unlock the file mongod.service

sudo chmod 777 /usr/bin/systemd/system/mongod.service -R

Step 3: Now run the following commands:

sudo systemctl daemon-reload
sudo systemctl start mongod
sudo systemctl enable mongod

For Ubuntu 16.04.5, it is noticed that MongoDB installations does not auto enable the mongod.service file after installation in my case after I have installed on several servers, so we need to enable it like below:

Issue below to check whether mongod is enabled

systemctl list-unit-files --type=service

If it shows as "disabled", then you need to enable it.

sudo systemctl enable mongod.service

If we want to see whether mongod.service file exists in case this file is missing, check in

ls /lib/systemd/system

You will see a file

/lib/systemd/system/mongod.service

Note that if using the Windows Subsystem for Linux, systemd isn't supported and therefore commands like systemctl won't work:

Failed to connect to bus: No such file or directory

See Blockers for systemd? #994 on GitHub, Microsoft/WSL.

The mongo server can still be started manual via mondgod for development of course.


$service mongodb start
$service mongodb status

the status is active when I started using above command


Do not remove your db if you already have some data you found useful. Just run the command below and you're good.

sudo systemctl restart mongodb.service

Try this: sudo systemctl start mongod and to check whether its running use sudo systemctl status mongod


You can repair

$ sudo rm /var/lib/mongodb/mongod.lock
$ mongod --repair
$ sudo service mongodb start

For Stop

$ sudo service mongodb stop

For Restart-

$ sudo service mongodb restart

For Status-

$ sudo service mongodb status

This happened to me while adding users in admin db.

sudo systemctl daemon-reload 

is recommended from the official mongo website, but did not help.

If you get

Job for mongod.service failed because the control process exited with error code. See "systemctl status mongod.service" and "journalctl -xe" for details.

While executing

sudo systemctl start mongod

run journalctl -xe and if the issue is

Failed to start MongoDB Database Server.
-- Subject: Unit mongod.service has failed

Then run sudo mongod --> mongo This will fix the issue and if you want to run with systemctl, then terminate the windows for mongod and mongo and run

sudo systemctl start mongod 

and then mongo


In some cases for some security reasons the unit would be marked as masked. This state is much stronger than being disabled in which you cannot even start the service manually.

to check this, run the following command:

    systemctl list-unit-files | grep mongod

if you find out something like this:

    mongod.service                         masked

then you can unmask the unit by:

    sudo systemctl unmask mongod

then you may want to start the service:

    sudo systemctl start mongod

and to enable auto-start during system boot:

    sudo systemctl enable mongod

However if mongodb did not start again or was not masked at all, you have the option to reinstall it this way:

    sudo apt-get --purge mongo*
    sudo apt-get install mongodb-org

thanks to @jehanzeb-malik


The second step of mongo installation is

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list

Instead of this command do it manually

  1. cd /etc/apt/
  2. nano sources.list
  3. Write it deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4 multiverse

And save the file,

Continue all process as in installation docs

It works for me:


Just follow the below commands. This has worked for me.

  1. Uninstall your mongo completely from your system:

    sudo service mongod stop
    sudo apt-get purge mongodb-org
    sudo rm -r /var/log/mongodb
    sudo rm -r /var/lib/mongodb
    
  2. Now reinstall mongodb using following commands:

    sudo apt update
    sudo apt install -y mongodb**
    

    Note: The database server is automatically started after installation.

  3. Next, let's verify that the server is running and works correctly.

    sudo systemctl status mongodb
    

    You'll see this output:

    mongodb.service - An object/document-oriented database
    Loaded: loaded (/lib/systemd/system/mongodb.service; enabled; vendor preset: enabled)
    Active: active (running) since Sat 2018-05-26 07:48:04 UTC; 2min 17s ago
    Docs: man:mongod(1)
    Main PID: 2312 (mongod)
    Tasks: 23 (limit: 1153)
    CGroup: /system.slice/mongodb.service
       +-2312 /usr/bin/mongod --unixSocketPrefix=/run/mongodb --config /etc/mongodb.conf**
    

[Solution]

Just type

sudo mongod

It will work.


I got the same error too .. my error is unmet dependencies /var/cache/apt/archives/mongodb-org-server_4.4.2_amd64.deb and I ran this:

sudo dpkg -i --force-all /var/cache/apt/archives/mongodb-org-server_4.4.2_amd64.deb

and it worked


I got the same error for a long while, tried almost all installation isntruciton on google but could not find te answer...

I followed the instructions from https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/ but keep getting the error Failed to start mongod.service: Unit mongod.service not found

I firstly installed using: sudo apt-get install -y mongodb-org Then noticed that wher I run:

$ sudo apt-get install -y mongodb-org
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  mongodb-org-mongos mongodb-org-shell mongodb-org-tools
The following NEW packages will be installed
  mongodb-org mongodb-org-mongos mongodb-org-shell mongodb-org-tools
0 to upgrade, 4 to newly install, 0 to remove and 46 not to upgrade.
Need to get 0 B/40.1 MB of archives.
After this operation, 178 MB of additional disk space will be used.
Selecting previously unselected package mongodb-org-shell.
(Reading database ... 379307 files and directories currently installed.)
Preparing to unpack .../mongodb-org-shell_4.0.3_amd64.deb ...
Unpacking mongodb-org-shell (4.0.3) ...
Selecting previously unselected package mongodb-org-mongos.
Preparing to unpack .../mongodb-org-mongos_4.0.3_amd64.deb ...
Unpacking mongodb-org-mongos (4.0.3) ...
Selecting previously unselected package mongodb-org-tools.
Preparing to unpack .../mongodb-org-tools_4.0.3_amd64.deb ...
Unpacking mongodb-org-tools (4.0.3) ...
Preparing to unpack .../mongodb-org_4.0.3_amd64.deb ...
Unpacking mongodb-org (4.0.3) ...
Setting up mongodb-org-shell (4.0.3) ...
Setting up mongodb-org-mongos (4.0.3) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Setting up mongodb-org-tools (4.0.3) ...
Setting up mongodb-org (4.0.3) ...


$ mongod --version
db version v2.6.12
2018-10-26T12:03:12.362+0200 git version: d73c92b1c85703828b55c2916a5dd4ad46535f6a

The db version was 2.6.12 instead of the latest one 4.0.3. So make sure you dont have mongodb-org-server already installed, if yes, then make sure you uupdate it to your target version (as indicated @https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/ -> Step 4 - Install a specific release of MongoDB.)

target_version = 4.0.3

sudo apt-get install -y mongodb-org=target_version mongodb-org-server=target_version mongodb-org-shell=target_version mongodb-org-mongos=target_version mongodb-org-tools=target_version

$ sudo apt-get install -y mongodb-org=4.0.3 mongodb-org-server=4.0.3 mongodb-org-shell=4.0.3 mongodb-org-mongos=4.0.3 mongodb-org-tools=4.0.3
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed
  mongodb-org mongodb-org-mongos mongodb-org-shell mongodb-org-tools
The following packages will be upgraded:
  mongodb-org-server
1 to upgrade, 4 to newly install, 0 to remove and 45 not to upgrade.
Need to get 15.7 MB/55.8 MB of archives.
After this operation, 217 MB of additional disk space will be used.
Get:1 https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0/multiverse amd64 mongodb-org-server amd64 4.0.3 [15.7 MB]
Fetched 15.7 MB in 2s (6,289 kB/s)                 
Selecting previously unselected package mongodb-org-shell.
(Reading database ... 379306 files and directories currently installed.)
Preparing to unpack .../mongodb-org-shell_4.0.3_amd64.deb ...
Unpacking mongodb-org-shell (4.0.3) ...
**Preparing to unpack .../mongodb-org-server_4.0.3_amd64.deb ...
Failed to stop mongod.service: Unit mongod.service not loaded.
invoke-rc.d: initscript mongod, action "stop" failed.
dpkg: warning: old mongodb-org-server package pre-removal script subprocess returned error exit status 5
dpkg: trying script from the new package instead ...
dpkg: ... it looks like that went OK
Unpacking mongodb-org-server (4.0.3) over (2.6.12) ...**
Selecting previously unselected package mongodb-org-mongos.
Preparing to unpack .../mongodb-org-mongos_4.0.3_amd64.deb ...
Unpacking mongodb-org-mongos (4.0.3) ...
Selecting previously unselected package mongodb-org-tools.
Preparing to unpack .../mongodb-org-tools_4.0.3_amd64.deb ...
Unpacking mongodb-org-tools (4.0.3) ...
Preparing to unpack .../mongodb-org_4.0.3_amd64.deb ...
Unpacking mongodb-org (4.0.3) ...
Processing triggers for ureadahead (0.100.0-20) ...
Setting up mongodb-org-shell (4.0.3) ...
Setting up mongodb-org-mongos (4.0.3) ...
Processing triggers for systemd (237-3ubuntu10.3) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Setting up mongodb-org-tools (4.0.3) ...
Setting up mongodb-org-server (4.0.3) ...
Installing new version of config file /etc/mongod.conf ...
Setting up mongodb-org (4.0.3) ...

and after doing this its all working for me :D


Failed to start mongod.service: Unit mongod.service not found

If you are following the official doc and are coming across with the error above that means mongod.service is not enabled yet on you machine (I am talking about Ubuntu 16.04). You need to do that using following command

sudo systemctl enable mongod.service

Now you can start mongodb using the following command

sudo service mongod start

You are missing a 'b' I think?

sudo service mongod start

should be

sudo service mongodb start

I think this is the case?


Examples related to mongodb

Server Discovery And Monitoring engine is deprecated Avoid "current URL string parser is deprecated" warning by setting useNewUrlParser to true MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017] Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified Failed to start mongod.service: Unit mongod.service not found db.collection is not a function when using MongoClient v3.0 MongoError: connect ECONNREFUSED 127.0.0.1:27017 MongoDB: How To Delete All Records Of A Collection in MongoDB Shell? How to resolve Nodejs: Error: ENOENT: no such file or directory How to create a DB for MongoDB container on start up?

Examples related to ubuntu

grep's at sign caught as whitespace "E: Unable to locate package python-pip" on Ubuntu 18.04 How to Install pip for python 3.7 on Ubuntu 18? "Repository does not have a release file" error ping: google.com: Temporary failure in name resolution How to install JDK 11 under Ubuntu? How to upgrade Python version to 3.7? Issue in installing php7.2-mcrypt Install Qt on Ubuntu Failed to start mongod.service: Unit mongod.service not found

Examples related to ubuntu-16.04

Yarn: How to upgrade yarn version using terminal? pull access denied repository does not exist or may require docker login Failed to start mongod.service: Unit mongod.service not found How to completely uninstall python 2.7.13 on Ubuntu 16.04 Anaconda-Navigator - Ubuntu16.04 Default password of mysql in ubuntu server 16.04 Unable to set default python version to python3 in ubuntu "pip install json" fails on Ubuntu Unable to start the mysql server in ubuntu How to install php-curl in Ubuntu 16.04