How to start MongoDB server on system start?

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.


Solution 1:

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.

Solution 2:

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

Solution 3:

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.

Solution 4:

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".