Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running mongodb on ubuntu 16.04 LTS

How can I run Mongodb, as a service, on Ubuntu 16.04 LTS? A few days ago I had upgrade my server to Ubuntu 16.04. I have noticed the my MongoDB service does not start when I reboot. Trying to use

sudo initctl start mongod

Did not do the trick. Anyone has an idea how to solve this?

like image 758
Sagi Forbes Avatar asked May 03 '16 20:05

Sagi Forbes


People also ask

How do I start MongoDB from terminal?

To start MongoDB, run mongod.exe from the Command Prompt navigate to your MongoDB Bin folder and run mongod command, it will start MongoDB main process and The waiting for connections message in the console.

Where is MongoDB installed on Ubuntu?

https://repo.mongodb.org/apt/ubuntu : This is a URI representing the location where the APT data can be found. In this case, the URI points to the HTTPS address where the official MongoDB repository is located. focal/mongodb-org/4.4 : Ubuntu repositories can contain several different releases.


4 Answers

Anyone who upgrade or installed Ubuntu 16.04 ( also known as Ubuntu Xenial xerus ) noticed that some of the old services stopped running. This issue is known from version 15.04 but I will focus on the above version.

Such was my case with MongoDB. To make a long story, short, Ubuntu shifted from upstart to systemd. One common solution, to these problems, is to switch back to upstart. I do not consider that option as a real solution, certainly not for the long run.

A real solution ( IMHO ) to the problem is to write systemd script that will start MongodDB. Unfortunately MongoDB guys had yet to supply one.

So I had to write one from scratch. To create one of your own follow these steps:

  1. switch to root using
    sudo su
    

or use sudo for all the following steps.

  1. create a service script (in this example the name of the service is Mongodb)

    nano /lib/systemd/system/mongodb.service
    
  2. File content should be

    [Unit]
    Description=MongoDB Database Service
    Wants=network.target
    After=network.target
    
    [Service]
    ExecStart=/usr/bin/mongod --config /etc/mongod.conf
    ExecReload=/bin/kill -HUP $MAINPID
    Restart=always
    User=mongodb
    Group=mongodb
    StandardOutput=syslog
    StandardError=syslog
    
    [Install]
    WantedBy=multi-user.target
    

You can also download the file from here: mongodb.service
Here is a quick description of the important fields:
ExecStart - Is the command to run. Mongo installs itself under /usr/bin and the configuration file is written at /etc
User - The uid of the mongod process.
Group - The gid of the mongod process. Note that the user and group are created by the installation.

Now to start mongodb:

sudo systemctl start mongodb

To stop mongodb service use:

sudo systemctl stop mongodb

To enable mongodb on startup

sudo systemctl enable mongodb.service

If you need to refresh the services use:

 sudo systemctl daemon-reload
like image 155
Sagi Forbes Avatar answered Oct 16 '22 09:10

Sagi Forbes


The latest version of MongoDB does most of things except one thing for now. After installing the MongoDB on Ubuntu 16.04.x , run the commands as follows:

$ sudo systemctl enable mongod.service
$ sudo systemctl daemon-reload

Now most likely mongod starts on every boot automatically.


This solution also works in case of getting this error:

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

After run the commands above, the commands below start to work:

$ sudo service mongod start
$ mongo

Similarly, the all services installed must be enabled to run. For instance, after installation of Ops Manager (a.k.a MMS) the documentation says to run the commend below.

$ sudo systemctl start mongodb-mms.service

Most likely Ubuntu does not start the service. Because it is not enabled yet. To enable it just run the command below:

$ sudo systemctl enable mongodb-mms.service
$ sudo systemctl daemon-reload

Then try to start the service:

$ sudo systemctl enable mongodb-mms.service

That's all...

like image 31
efkan Avatar answered Oct 16 '22 10:10

efkan


For People That Want A Fresh Install

I have been struggling with this for 1 hour. Then I found this page Installing is as easy as doing:

sudo apt-get update
sudo apt-get install mongodb

Then to check if everything works:

sudo service mongodb status

Let me know if this works for you!

like image 26
Nicola Pedretti Avatar answered Oct 16 '22 09:10

Nicola Pedretti


The packages for 3.2+ contain upstart scripts.

First, follow the official instructions to install:

# If you installed the ubuntu package, remove it, if not skip to key import
sudo apt-get remove mongodb
sudo apt-get autoremove
# import key
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
# add trusty repos
echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
# update apt
sudo apt-get update
# install the package
sudo apt-get install -y mongodb-org

Then unmask the mongodb.service job (and enable and start it). I believe it was masked because the package includes an upstart jobs as well, so you wouldn't want both of them to start. In our case, however, this is clearly what we want.

sudo systemctl unmask mongodb
sudo service mongod start

References:

relevant JIRA issue

related question on Ask Ubuntu

like image 23
Hut8 Avatar answered Oct 16 '22 09:10

Hut8