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 ...
  • 16.,04 uses systemd, not upstart. Create a systemd service: unix.stackexchange.com/questions/15348/… – 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
  • 1
    Also, 2.2 is ancient, see here for using more up to date versions on 16.04: askubuntu.com/a/757385/130555 – Adam C Apr 20 '16 at 12:18
4

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/
  • 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. – Joe Feb 6 '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
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

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.