init script for Mongo / Ubuntu

1,144 views
Skip to first unread message

Wouter

unread,
Sep 22, 2009, 5:00:44 PM9/22/09
to mongodb-user
Does anyone have an init script for MongoDB so that I can start/
restart/stop it easily and add it to update-rc so Mongo is started on
reboot?!

I am using Ubuntu Hardy (if that matters :-))

John Wyles

unread,
Sep 22, 2009, 5:06:25 PM9/22/09
to mongod...@googlegroups.com
This would also be equally awesome for CentOS (if there are even any
discrepancies) :)

Michael Stillwell

unread,
Sep 22, 2009, 6:16:55 PM9/22/09
to mongodb-user
I am far from an expert in such things, but I handle this with an
upstart script. My /etc/event.d/mongodb is:

pre-start script
mkdir -p /var/lib/mongodb
end script

exec /usr/local/bin/mongod --quiet --dbpath /var/lib/mongodb >> /
var/log/mongodb.log

start on runlevel 2

respawn

Upstart is somewhat documented at

http://upstart.ubuntu.com/

With this file in place (you don't need to do anything else), MongoDB
starts on boot, and can be controlled with:

$ sudo start mongodb # start MongoDB
$ sudo stop mongodb # stop MongoDB
$ sudo initctl list mongodb # check whether it's running or not

The respawn supposedly restarts MongoDB in the event that it fails.
(I think there is some logic in upstart to prevent it respawning
MongoDB endlessly in the event of a problem with MongoDB.)





Michael

--
http://beebo.org

Erwan A.

unread,
Sep 23, 2009, 3:12:48 AM9/23/09
to mongodb-user
Here's what i'm using on FC9, which should work on CentOS

http://pastebin.com/f5f4b909c

and /etc/sysconfig/mongodb

http://pastebin.com/f69149abd

I remember finding the bases for these in some "contrib" sections of
mongodb.org

Erwan

monkeyhelper

unread,
Sep 23, 2009, 5:17:20 AM9/23/09
to mongodb-user
On Sep 22, 10:00 pm, Wouter <wiegm...@gmail.com> wrote:
You can build debian/ubuntu packages from source with this built in,
there are some notes in the comments here :

http://www.mongodb.org/display/DOCS/Quickstart

Here is the init script from github :

http://github.com/mongodb/mongo/blob/master/debian/init.d

Wouter

unread,
Sep 23, 2009, 12:37:19 PM9/23/09
to mongodb-user
Thanks! Initially it didn't work, but after I made some modifications,
it did.

I installed MongoDB in /usr/local/mongodb-1.0. The DB is stored at /
data/db.

I did the following to get it to work


In Ubuntu:

1) Create file to store PID
sudo touch /var/run/MongoDB.pid

2) Create init.d
sudo nano /etc/init.d/mongo

3) Paste the init.d (+ modifications, see below) & Save

4) Make executable
sudo chmod +x /etc/init.d/mongo

Updates to init.d:
1) set DAEMON to /usr/local/mongodb-1.0/bin/mongod

2) set DATA to /data/db

3) set DAEMON_OPTS to "--dbpath $DATA" (I removed --logpath, that
doesn't seem to be supported?? How am I logging now?)

4) comment DAEMONUSER as I did not have a mongodb user. Is it
important or smart to have a separate MongoDB user?

5) Save & exit

Now

sudo /etc/init.d/mongo start/stop etc.

To make it run on a reboot

sudo /usr/sbin/update-rc.d -f mongo defaults


Please let me know if I made any errors. I'm not too experienced with
all this system administration :-)

monkeyhelper

unread,
Sep 23, 2009, 1:41:31 PM9/23/09
to mongodb-user


On Sep 23, 5:37 pm, Wouter <wiegm...@gmail.com> wrote:
> Thanks! Initially it didn't work, but after I made some modifications,
> it did.
>
> I installed MongoDB in /usr/local/mongodb-1.0. The DB is stored at /
> data/db.
>
> I did the following to get it to work
>
> In Ubuntu:
>
> 1) Create file to store PID
> sudo touch /var/run/MongoDB.pid
>
> 2) Create init.d
> sudo nano /etc/init.d/mongo
>
> 3) Paste the init.d (+ modifications, see below) & Save
>
> 4) Make executable
> sudo chmod +x /etc/init.d/mongo
>
> Updates to init.d:
> 1) set DAEMON to /usr/local/mongodb-1.0/bin/mongod
>
> 2) set DATA to /data/db
>
> 3) set DAEMON_OPTS to "--dbpath $DATA" (I removed --logpath, that
> doesn't seem to be supported?? How am I logging now?)
>

To enable logging, you'll need to

mkdir /var/log/mongodb

and make that path read/writable by the user that mongod is running
as.

then add --logpath /var/log/mongodb/MongoDB.log to the init script
again.

> 4) comment DAEMONUSER as I did not have a mongodb user. Is it
> important or smart to have a separate MongoDB user?
>

It's generally considered good security practice to run daemons with
their own user, so it's a good idea to do so if this is a production
environment.

> 5) Save & exit
>
> Now
>
> sudo /etc/init.d/mongo start/stop etc.
>
> To make it run on a reboot
>
> sudo /usr/sbin/update-rc.d -f mongo defaults
>
> Please let me know if I made any errors. I'm not too experienced with
> all this system administration :-)
>

Those changes seem fine, obviously I'd personally recommend building
a .deb package so that you don't have to make all those changes and it
just works 'out of the box'.

Wouter

unread,
Sep 23, 2009, 2:10:49 PM9/23/09
to mongodb-user
Thanks for your help!

www-test ~: sudo /usr/local/mongodb-1.0/bin/mongod --logpath /var/log/
MongoDB.log
ERROR: unknown option logpath

I'm using MongoDB 1.0. Did this option somehow disappear?!

To add a user for MongoDB, is

sudo add user mongodb

enough?!

Eliot Horowitz

unread,
Sep 23, 2009, 2:13:27 PM9/23/09
to mongod...@googlegroups.com
--logpath and --fork are new in 1.1

monkeyhelper

unread,
Sep 24, 2009, 5:17:02 PM9/24/09
to mongodb-user


On Sep 23, 7:13 pm, Eliot Horowitz <eliothorow...@gmail.com> wrote:
> --logpath and --fork are new in 1.1
>

Ah yes, sorry. I'm running at the bleeding edge !
Reply all
Reply to author
Forward
0 new messages