Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

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. 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.

share|improve this question

migrated from stackoverflow.com Jun 11 '12 at 13:46

This question came from our site for professional and enthusiast programmers.

3 Answers 3

up vote 8 down vote accepted

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.

share|improve this answer
1  
+1 - this is a very thorough answer –  jjwchoy Jun 11 '12 at 14:27
    
The link for install-mongodb-on-debian-or-ubuntu-linux was changed to docs.mongodb.org/manual/tutorial/install-mongodb-on-debian and docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu. –  Fernando Kosh Mar 30 at 1:18

upstart scripts are found in /etc/init/

share|improve this answer
    
thnx , there is a mongodb.conf but it is default conf file for default running instance –  e2burner Jun 10 '12 at 1:00
    
I've just had a look at my mongodb.conf it sources /etc/default/mongodb presumably for configuration and then starts mongodb using start-stop-daemon –  jjwchoy Jun 10 '12 at 1:04
    
you could copy this file and modify the command to start mongodb with a different port. you probably want to specify a completely new config file though so you can change log files etc –  jjwchoy Jun 10 '12 at 1:05

I ran into a problem with the above answer where stop-start-daemon doesn't start a new process with the same name more than once. However, there is a --name argument that allows you to change the line in your upstart script in /etc/init/ to include a new name so it'll start. Something like this:

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

In this case the --name argument makes this instance unique and allows it to start separately from other mongod instances.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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