6

I installed MongoDB through the software centre in Ubuntu 16.04:

mongodb 1:2.6.10-0ubuntu1

I believe this currently contains MongoDB 2.6.10.

I am running an application on apache2 localhost setup and have to start mongod manually each time the system starts with:

mongod

I want it to start automatically.

I've come across two main methods to do this:

update-rc.d mongodb defaults

from: https://askubuntu.com/a/89914/367134

which results in:

update-rc.d mongodb defaults
insserv: fopen(.depend.stop): Permission denied

But this didn't produce error:

sudo update-rc.d mongodb defaults

I've also seen a few references to edit the "MongoDB config file", but I'm not sure what this refers to as I can see config files in more than one location:

  • /etc/mongodb.conf
  • /etc/init/mongodb.conf
  • /etc/init.d/mongodb

And, once in the correct file, I'm not sure what to change in there.

In /etc/init/mongodb.conf I can see:

start on runlevel [2345]
stop on runlevel [!2345]

What is the correct way to ensure mongod starts on system startup in 16.04?

I've been reading this, and looked in /etc/rc2.d and can see:

S01mongodb@                       --> /etc/init.d/mongodb

Update:

After running suggestion:

sudo systemctl enable mongodb

and then restarting, running systemctl status mongodb returns:

* mongodb.service - An object/document-oriented database
   Loaded: loaded (/lib/systemd/system/mongodb.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Mon 2016-04-18 23:42:28 AEST; 7min ago
     Docs: man:mongod(1)
  Process: 655 ExecStart=/usr/bin/mongod --config /etc/mongodb.conf (code=exited, status=100)
 Main PID: 655 (code=exited, status=100)

Apr 18 23:42:27 me-comp systemd[1]: Started An object/document-oriented database
Apr 18 23:42:28 me-comp systemd[1]: mongodb.service: Main process exited, code=exited, status=100/n/a
Apr 18 23:42:28 me-comp systemd[1]: mongodb.service: Unit entered failed state.
Apr 18 23:42:28 me-comp systemd[1]: mongodb.service: Failed with result 'exit-code'.  

Permission Troubleshooting

/var/lib/mongodb = drwxr-xr-x mongodb mongodb.
/var/log/mongodb = drwxr-xr-x mongodb mongodb.
/var/log/mongodb/mongodb.log = -rw-r--r-- mongodb nogroup.
/var/log/mongodb/mongod.log = -rw-r--r-- mongodb mongodb.
/data/db/mongod.lock = -rwxrwxr-x me me and 0 bytes.
/data = drwxr-xr-x root root.
/data/db = drwxr-xr-x me root.

Config File Contents

/etc/mongodb.conf:

# Where to store the data.
dbpath=/var/lib/mongodb

#where to log
logpath=/var/log/mongodb/mongodb.log

logappend=true

bind_ip = 127.0.0.1
#port = 27017 ...
user1063287
  • 2,265
  • 16.,04 uses systemd, not upstart. Create a systemd service: http://unix.stackexchange.com/questions/15348/writing-basic-systemd-service-files – muru Apr 18 '16 at 13:29
  • @EdiD - systemd status mongodb returns Excess arguments. and sudo systemctl enable mongodb returns Synchronizing state of mongodb.service with SysV init with /lib/systemd/systemd-sysv-install... Executing /lib/systemd/systemd-sysv-install enable mongodb. – user1063287 Apr 18 '16 at 13:40
  • and after executing... systemctl status mongodb returns something ? – EdiD Apr 18 '16 at 13:44
  • See update in original post for results. – user1063287 Apr 18 '16 at 13:57
  • When you execute /usr/bin/mongod --config /etc/mongodb.conf manually does it start ? – EdiD Apr 18 '16 at 14:21
  • returns: 2016-04-19T00:28:17.310+1000 SEVERE: Failed global initialization: FileNotOpen Failed to open "/var/log/mongodb/mongodb.log" – user1063287 Apr 18 '16 at 14:28
  • Permissions info added to original post. – user1063287 Apr 18 '16 at 14:56
  • I think by starting it manually you have now messed up the permissions for the log file (and probably others) - same fix as here required: http://stackoverflow.com/a/12232668/1148648 – Adam C Apr 20 '16 at 12:17
  • 1
    Also, 2.2 is ancient, see here for using more up to date versions on 16.04: http://askubuntu.com/a/757385/130555 – Adam C Apr 20 '16 at 12:18

4 Answers4

6

Installing

This is the method I ended up using (from user Adam C's comment re: using more up to date version of mongodb). It worked for me but I'm not an authority on the matter.

https://askubuntu.com/a/757385/367134

Then I had to make a service file, see:

https://askubuntu.com/a/694226/367134

And edit the path in that service file from:

/etc/mongodb.conf 

to:

/etc/mongod.conf

And then make mongodb start on system startup with:

sudo systemctl enable mongodb

see:

https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-systemd-services-and-units

Now mongodb is starting on 16.04 system startup.

Uninstalling Old MongoDB

Before I did that however, I had to remove the existing installs and directories, which i did like this (steps mainly from https://askubuntu.com/a/497144/367134):

# go to /etc/apt/sources.list.d and remove any mongodb lists, then:

# sanity check - see what is installed
sudo dpkg -l | grep mongo

# remove all packages 
sudo apt-get remove mongodb* --purge
sudo apt-get autoremove

# remove old directories
sudo rm -r -f /var/lib/mongodb/
sudo rm -r -f /var/log/mongodb/
user1063287
  • 2,265
  • If anyone is getting mongodb.service: Main process exited stuff, make sure you don't have both mongodb and mongod services trying to run. Use systemctl list-unit-files | grep enabled to list enabled services. You may have accidentally enabled both (like I did), or the wrong one. –  Feb 06 '17 at 14:27
0

While you're developing you may also want the opposite sometimes: to have multiple local separable instances that you start individually, rather than one global install...

mongod --dbpath ./mongo_db_data/ --logpath ./mongo_logs/mongodb.log --port 12345
Videonauth
  • 33,355
  • 17
  • 105
  • 120
0

For me works just doing sudo systemctl enable mongod

0

For me, it is because of the wrong permission of db path. just

chown mongodb:mongodb /var/lib/mongodb
tim
  • 101