ubuntu: start (upstart) second instance of mongodb
Asked Answered
O

6

12

the standard upstart script that comes with mongodb works fine:

# Ubuntu upstart file at /etc/init/mongodb.conf

limit nofile 20000 20000

kill timeout 300 # wait 300s between SIGTERM and SIGKILL.

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

start on runlevel [2345]
stop on runlevel [06]

script
  ENABLE_MONGODB="yes"
  if [ -f /etc/default/mongodb ]; then . /etc/default/mongodb; fi
  if [ "x$ENABLE_MONGODB" = "xyes" ]; then exec start-stop-daemon --start --quiet --chuid mongodb --exec  /usr/bin/mongod -- --config /etc/mongodb.conf; fi
end script

if i want to run a second instance of mongod i thought i just copy both /etc/mongodb.conf -> /etc/mongodb2.conf and /etc/init/mongodb.conf -> /etc/init/mongodb2.conf and change the std port in the first conf-file. then adjust the script above to start with the newly created /etc/mongodb2.conf.

i can then just say start mongodb2and the service starts ... but it is killed right after starting. what do i change, to get both processes up and running?

 # Ubuntu upstart file at /etc/init/mongodb2.conf

limit nofile 20000 20000

kill timeout 300 # wait 300s between SIGTERM and SIGKILL.

pre-start script
    mkdir -p /var/lib/mongodb2/
    mkdir -p /var/log/mongodb2/
end script

start on runlevel [2345]
stop on runlevel [06]

script
  ENABLE_MONGODB="yes"
  if [ -f /etc/default/mongodb ]; then . /etc/default/mongodb; fi
  if [ "x$ENABLE_MONGODB" = "xyes" ]; then exec start-stop-daemon --start --quiet --chuid mongodb --exec  /usr/bin/mongod -- --config /etc/mongodb2.conf; fi
end script
Obstetric answered 4/9, 2011 at 15:10 Comment(0)
O
10

i couldn't get the "standard" upstart script to work (as described above), so i changed it like this:

# Ubuntu upstart file at /etc/init/mongodb.conf

limit nofile 20000 20000

kill timeout 300 # wait 300s between SIGTERM and SIGKILL.

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

start on runlevel [2345]
stop on runlevel [06]

script

  exec sudo -u mongodb /usr/bin/mongod --config /etc/mongodb.conf

end script

and if you want to run other instances of mongodb just copy the *.conf files and make the changes to /etc/mongodb2.conf and /etc/init/mongodb2.conf

# Ubuntu upstart file at /etc/init/mongodb2.conf

limit nofile 20000 20000

kill timeout 300 # wait 300s between SIGTERM and SIGKILL.

pre-start script
    mkdir -p /var/lib/mongodb2/
    mkdir -p /var/log/mongodb2/
end script

start on runlevel [2345]
stop on runlevel [06]

script

  exec sudo -u mongodb /usr/bin/mongod --config /etc/mongodb2.conf

end script

i think the only thing that is not working is restart mongodb - you have to stop and then start again ...

Obstetric answered 13/9, 2011 at 13:5 Comment(0)
F
5

I know there's already an accepted solution but I think this one is more elegant.

The other way is to use start-stop-daemon's pid file creation. For example, I have 2 mongos running on the same server with 2 different upstart scripts, and the two magic lines are:

exec start-stop-daemon --make-pidfile --pidfile /var/run/mongodb-router.pid --start --startas /data/bin/mongos --chuid mongo -- --logappend --logpath /mnt/log/mongos.log --configdb mongo2-config01,mongo2-config02,mongo2-config03


exec start-stop-daemon --make-pidfile --pidfile /var/run/mongodb-routerrt.pid --start --startas /data/bin/mongos --chuid mongo -- --logappend --logpath /mnt/log/mongos-rt.log --configdb mongort-config01,mongort-config02,mongort-config03 --port 27027

Note that one has '--pidfile /var/run/mongodb-router.pid' and the other has '--pidfile /var/run/mongodb-routerrt.pid' and a different port.

Funicular answered 27/10, 2011 at 16:31 Comment(0)
D
2

Yeah I ran into this same issue today. The reason is that the default script uses the start-stop-daemon to start mongo, which is specifically designed to ensure that only one version of a process is running. You already figured out that one way to fix this is to not use start-stop-daemon and to start the binary yourself. That's the way I do it too but I'd be curious to hear if there's a better way.

Daye answered 4/10, 2011 at 20:52 Comment(0)
F
2

This is how I do it. 2 instances of mongodb, with start-stop-daemon, on the same server

that are my start-stop-daemon configs

exec start-stop-daemon --make-pidfile --pidfile /var/lib/mongodb/db1.pid --start --quiet --chuid mongodb --name mongod1 --exec /usr/bin/mongod -- --config etc/mongodb1.conf

exec start-stop-daemon --make-pidfile --pidfile /var/lib/mongodb/db2.pid --start --quiet --chuid mongodb --name mongod2 --exec /usr/bin/mongod -- --config etc/mongodb2.conf

pay attention to the --name option. That did the trick for me

Foolproof answered 15/8, 2013 at 9:22 Comment(1)
Upstart was not allowing me to start and stop my multiple mongod's until I added the --name parameter, thanks!Edithe
P
1

the two daemons cannot listen on the same tcp port, thus you have to change the --port parameter of mongod2 in order to listen to a different port.
the two daemons cannot share the same data dir, thus you have to change the --data-dir parameter of mongod2.

Plumbo answered 5/9, 2011 at 7:12 Comment(3)
yes, i know. that's why i copied the /etc/mongodb2.conf and changed the port and data-folder (as i wrote above). but still not working ...Obstetric
sorry, but you haven't posted your config file, so i guessed. what happens if you start the second daemon by hand via console and not a daemon, which is the cause of the premature exit?Plumbo
it works when i call mongod --config /etc/mognodb2.conf by hand.Obstetric
C
1

I find the below upstart works for me

# Ubuntu upstart file at /etc/init/mongodb.conf

description "manage mongodb instance"

start on runlevel [2345]
stop on runlevel [06]

limit nofile 20000 20000

kill timeout 300 # wait 300s between SIGTERM and SIGKILL.

env MONGODB_USER=mongodb
env MONGODB_DATA=/var/lib/mongodb/
env MONGODB_LOG=/var/log/mongodb/
env MONGODB_PID=/var/run/mongodb.pid

pre-start script
  if [ ! -d $MONGODB_DATA ]; then
    mkdir -p $MONGODB_DATA
    chown $MONGODB_USER:$MONGODB_USER $MONGODB_DATA
  fi

  if [ ! -d $MONGODB_LOG ]; then
    mkdir -p $MONGODB_LOG
    chown $MONGODB_USER:$MOGODB_USER $MONGODB_LOG
  fi
end script

exec start-stop-daemon --start --pidfile $MONGODB_PID --chuid $MONGODB_USER:$MONGODB_USER --exec /usr/bin/mongod -- --config /etc/mongodb/mongodb.conf

pre-stop exec start-stop-daemon --signal QUIT --stop --quiet --pidfile $MONGODB_PID --chuid $MONGODB_USER:$MONGODB_USER --exec /usr/bin/mongod -- --config /etc/mongodb/mongodb.conf
Cucumber answered 27/5, 2013 at 18:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.