103

I have to start my MongoDB server every time the system restarts. How do I configure it to start with my OS? I am on Ubuntu 11.04.

1
  • Use this command to run after boot : sudo systemctl enable mongod Aug 11, 2021 at 7:01

10 Answers 10

170

According to the comments, on Ubuntu 18.04 LTS this seems to be the solution:

systemctl enable mongodb.service

Thanks to @Adam. I had the same "problem" on Debian jessie, and my solution there was:

systemctl enable mongod.service

Maybe they changed the name of the service. I think in Ubuntu it's the same.

11
  • 11
    Worked for me on Ubuntu Mate 16.04.2 LTS Feb 18, 2017 at 12:50
  • 2
    Pity there is no info in official documentation about starting the service.
    – UpTheCreek
    Jan 9, 2018 at 12:31
  • 4
    may need to (sudo) systemctl start mongod to start the mongd service
    – XoXo
    Apr 3, 2018 at 11:47
  • 2
    Also works on ubuntu 18.04 LTS
    – 89n3ur0n
    Aug 21, 2018 at 16:08
  • 2
    Only works for me if I change mongod.service to mongodb.service on Ubuntu 18.04 LTS - strange?
    – Adam
    Jul 14, 2019 at 19:32
41

If you install MongoDB using the Advanced Packaging Tool (apt) then it'll configure your startup scripts to automatically run Mongo when the system boots.

The steps are as follows, first configure apt to be able to download the Mongo package:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
sudo nano /etc/apt/sources.list

Add this line to sources.list then save:

deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen

Then download and install Mongo with the apt-get utility:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install mongodb-10gen

If you want to make any changes to config, edit your mongodb.conf and restart: 

sudo nano /etc/mongodb.conf
sudo service mongod restart
1
  • 5
    If you want to later change it to not automatically start MongoDB on startup, edit /etc/init/mongodb.conf.
    – kynan
    Dec 5, 2012 at 12:08
12

Controlling all the init.d service links should be done with the update-rc.d tool

i.e. to turn on the mongod daemon in the default runlevels (i.e. turn it on at boot):

update-rc.d mongodb defaults

See https://help.ubuntu.com/community/UbuntuBootupHowto for more information. This link tells you everything you want to know about how to set programs at boot.

2
  • 2
    The 10gen MongoDB distribution does not use System-V style init scripts, you need to edit /etc/init/mongodb.conf.
    – kynan
    Dec 5, 2012 at 12:04
  • do i need to do this?
    – tofutim
    Mar 11, 2018 at 1:53
8

I am using crontab for Ubuntu. It works fine. To be able to edit file

Sudo crontab –e 

Add this line to the file

@reboot sudo service mongod start &

The "&" sigh at the end help it to work background.

Ctrl + x for exit, press "Y" once prompted. And keep the file name as "crontab".

1
  • Start up at boot : sudo systemctl enable mongod To verify mongo status, auth and version : mongo --eval 'db.runCommand({ connectionStatus: 1 })' Aug 11, 2021 at 7:06
7

If you have Ubuntu 16.04 LTS, you can enable mongo to start on boot typing this in your console:
sudo systemctl enable mongod
I have used this approach with MongoDB Community Edition 3.6 and it works. Reboot your machine and test if mongo is running typing:
sudo service mongod status

6

If you have installed the MongoDB Community Edition (which is the recommended way since it receives more frequent updates than the package distributed in the Ubuntu package repository) you configure the start / stop behaviour of mongod via the upstart init script /etc/init/mongod.conf, which defaults to start the daemon automatically on boot

start on runlevel [2345]
stop on runlevel [06]

If you do not want it to start automatically, replace those 2 lines with

stop on runlevel [023456]
2
  • Will this start the server before anyone logs into the system? Mar 17, 2018 at 16:13
  • @GabrielFair Yes
    – kynan
    Aug 5, 2018 at 11:24
3

You can use systemctl command to enable your mongo service at run at system boot.

Create a service such that

sudo nano /etc/systemd/system/mongodb.service

Place content in the file

[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

after that you will be able to use service commands like

sudo service mongod start|stop|restart

and then if you want to make it up at machine boot, you can create mongod file under /etc/init.d/

2

chkconfig --levels 235 mongod on?

where mongodb is the name of your service

3
  • 2
    The 10gen MongoDB distribution does not use System-V style init scripts by default (though one is provided), edit /etc/init/mongodb.conf instead.
    – kynan
    Dec 5, 2012 at 12:16
  • @kynan Edit it how? What do I change / add to the mongod.conf file?
    – Kieveli
    Apr 28, 2016 at 13:48
  • @Kieveli: see this answer
    – kynan
    Apr 29, 2016 at 22:46
2

If you install MongoDB with apt-get as described in the MongoDB Ubuntu installation guide, it will come with a basic startup script and config file. (use of a config file is highly recommended)

You can also take a look here for an old post that links to an init.d script.

In either case, the basic premise is that you're setting up a service and then configuring to start-stop with the computer. This is pretty common technique for servers, there are lots of tutorials around for doing exactly this.

1
  • 2
    no longer the case in 16.04
    – chovy
    Dec 10, 2016 at 6:59
1

For a default installation using apt, you can start it with following command

sudo service mongod start