18

I recently updated mongodb, and I run Linux Mint (an Ubuntu based system) and every time I start it up, i cannot use Robomongo because the service mongod is not automatically started; every time I need to open up a terminal and use

sudo service mongod start

Is there a way to start mongod automatically at system start?

masterxilo
  • 2,503
  • 1
  • 30
  • 35
Don Diego
  • 1,309
  • 3
  • 23
  • 46
  • 2
    `sudo systemctl enable mongod && sudo systemctl start mongod` – molaga Nov 15 '16 at 09:11
  • there's no systemctl on ubuntu (mint), I tried the equivalent `sudo update-rc.d -f foo_service remove` and `sudo update-rc.d foo_service defaults` that's strange, it works on reboot, but ater turning off the pc and restarting it the day after, it doesn't work anymore. – Don Diego Nov 17 '16 at 09:30

3 Answers3

52

Use the following command to autostart mongodb when system start.

systemctl enable mongod.service
Yeshu Kochher
  • 650
  • 8
  • 10
  • there's no systemctl on ubuntu (mint) – Don Diego Apr 17 '18 at 07:40
  • 4
    Works for debian – Harshal Yeole Dec 18 '18 at 07:34
  • Yeah, but I asked for linux mint, not for debian. – Don Diego Mar 19 '20 at 12:14
  • I used this command but it seems run without any error message, however I typed "mongo" I got this error:MongoDB shell version v4.2.8 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb 2020-07-12T19:33:16.990+0430 E QUERY [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused : – Navid Jul 12 '20 at 15:07
  • @PredatorX ```systemctl start mongod``` and after that: ```mongo``` – hdoitc Mar 26 '21 at 15:00
  • @Don Diego I'm using ubuntu (mint), it has the `systemctl` command. – Eric May 06 '21 at 10:24
  • @Eric yeah thank you, I asked it 5 years ago. – Don Diego May 21 '22 at 11:32
2

You can either put the command in your /etc/bashrc script under and if condition i.e. if the mongod process is not already running, then start it.

Other way is to modify your /etc/rc.local and add the command to start mongod in that file. It will start at bootup.

Farhad
  • 388
  • 2
  • 13
  • at last I used the second option, since I opened `/etc/bash.bashrc` (I don't have a `bashrc` file) and I found it way too complex to edit. Thanks! – Don Diego Nov 15 '16 at 09:31
  • that's strange, it works on reboot, but ater turning off the pc and restarting it the day after, it doesn't work anymore. Don't know why! – Don Diego Nov 17 '16 at 09:31
0

Using crontab on Ubuntu 20 you can try this.

crontab -e

And input this as a crontab entry

@reboot service mongod start

Lelouch
  • 549
  • 6
  • 6