Mongodb – Upstart script in ubuntu for mongodb

instancemongodbUbuntu

I am new to linux and mongodb.
I've already installed mongodb on ubuntu but I want to run one more instance on different port.
As I understood from other questions and forums , there must be upstart script.
https://stackoverflow.com/questions/7300109/ubuntu-start-upstart-second-instance-of-mongodb

But I can not find or locate an upstart script on my server.If I create a upstart script , where should I locate it to start as a service.

or is there any other proper and easy way to start another instance of mongodb on the same server but different port.

Best Answer

Are you running from the 10gen repository or from the default Debian/Ubuntu repo? I recommend using the official 10gen repository.

Check this link out - [10gen MongoDB How-To Install on Ubuntu:] http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/. It is best to uninstall the previous mongodb installation prior to this change, which will also require you to modify your repository source (in /etc/apt/source.list) but this is also detailed in the link above. I recommend using upstart over sysvinit and the processed is outlined in the How-To.

With the 10gen Ubuntu set-up, the configuration file is in /etc/mongodb.conf.

There are several ways that you can you can run a separate mongod process, one would simply be running it from the cli -

sudo -u mongodb mongod --dbpath /var/tmp/mongotest --logpath /var/tmp/mongotest_log --port 3001 &

which will produce (using 'ps')

mongodb   2210  3.3  1.5 259012 15300 pts/0    Dl   11:48   0:00 mongod --dbpath /var/tmp/mongotest --logpath /var/tmp/mongotest_log --port 3001

and you can connect via the cli -> mongod --port 3001

Another way is creating a copy of /etc/mongodb.conf -

sudo cp /etc/mongodb.conf /etc/mongodbnew.conf and editing the following lines in the new file - 


# mongodbnew.conf

dbpath=/var/lib/mongodbnew

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

port = 27018

I have changed the dbpath (where the mongodb files are stored) from /var/lib/mongodb to /var/lib/mongodbnew; the log path has changed from mongodb.log to mongodbnew.log and the port has changed from the default of 27017 to 27018 (you will also have to remove the # to uncomment the line).

I also changed the top line to reflect the new name of this configuration file.

You will also have to create the data directory because it won't exist and without it, the mongod process won't start and ensure that the owner and group is mongodb -

sudo mkdir /var/lib/mongodbnew
sudo chown -R mongodb:mongodb mongodbnew/

Additionally, create and change the persmissions of your log file -

sudo touch /var/log/mongodb/mongodbnew.log && sudo chown mongodb:mongodb /var/log/mongodb/mongodbnew.log

To start your new mongod process (in the background) from the cli, type

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

which send the following to the screen -

"all output going to: /var/log/mongodb/mongodbnew.log"

Check that the mongod processes are running with "ps" -

sysadmin@ubuntu:/var/lib$ ps auwx | grep mongo | egrep -v 'grep|sudo'
mongodb    921 11.3  1.5 627672 15608 ?        Ssl  10:56   4:30 /usr/bin/mongod --config /etc/mongodb.conf
mongodb   2137  7.8  1.5 627672 15880 pts/0    Sl   11:36   0:00 /usr/bin/mongod --config /etc/mongodbnew.conf

To verify that you can connect, run the Mongo shell -

$ mongo --port 27018
MongoDB shell version: 2.0.5
connecting to: 127.0.0.1:27018/test
> 

With lsof, you should now see your two mongod processes, the original bound to 27017 and the new one bound to 27018.

$ sudo lsof -i :27017
COMMAND PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
mongod  921 mongodb    6u  IPv4   9066      0t0  TCP *:27017 (LISTEN)
sysadmin@ubuntu:/var/lib$ sudo lsof -i :27018
COMMAND  PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
mongod  2137 mongodb    6r  IPv4  11923      0t0  TCP *:27018 (LISTEN)

To ensure that the two processes run at start-up, copy the original init configuration file -

sudo cp /etc/init/mongodb.conf /etc/init/mongodbnew.conf

which should result in two files such as -

$ ls -lart /etc/init/mongo*
-rw-r--r-- 1 root root 536 May  8 15:51 /etc/init/mongodb.conf
-rw-r--r-- 1 root root 554 Jun 11 11:43 /etc/init/mongodbnew.conf

Edit the new mongodbnew.conf file using vi or whatever you prefer to look like this -


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

limit nofile 20000 20000

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

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

start on runlevel [2345]

stop on runlevel [06]

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

end script

I have tested this on Ubuntu 12.04 and it appears to work well, including after a reboot. I haven't run it in production obviously. Apologies, if there's any typo's above but there's a lot of information and I may have missed something.

Finally, here's a link on Replica Sets that might help you as it has come examples on starting multiple mongod instances from the cli - http://www.mongodb.org/display/DOCS/Replica+Set+Tutorial.