Wednesday, March 26, 2014

Quicktip: Run multiple mongos on Ubuntu with upstart

If you're using Ubuntu with the 10Gen supplied packages for mongodb that use upstart, you may have noticed that running two mognod's on the same host doesn't work out of the box.  There are several threads where people suggest things like making copies of mongod and doing other wacky actions that may or may not involve a chicken.

Here's the easier way (trivial if your configs are templates generated by configuration management):
  • Add pidfilepath to all of your mongodb.conf files, ensuring the pidfilepath is different for each instance you want to run.  (IMO: This should be a default in the config file.)
  • You should already have different config files since you'll need to change the value of the listen port and db directory.  If you specify all of this on the command line you're a bad person and should feel bad about yourself.
  • Edit /etc/init/mongodb.conf and add "-p /pidfilepath/from/config/file.pid" to the start-stop-daemon arguments, before the --exec
  • Copy the init file for each mongod instance you want to run, adjust the pidfile to te correct
  • Run stuff
This forces the stop-start-daemon in the upstart script to check the pidfile to see if your process is running instead of the default of traversing the process list looking for identical commands already running.  Note you don't need to add "-m" to make start-stop-daemon create the pidfile, the mongo setting will do that for us.

Why would you want to do this?  In my case I want to run a replicaset arbiter on a node also running a database so I can test the setup teardown of replicas in vagrant without having to create a third machine.  In other cases, I want to use a separate mongo database on the same machine my 3rd arbiter is running on.