How to run mongo db as service using non-default dbpath?

15,452

Solution 1

This doesn't really have anything to do with a "service" it's down to the mongod (MongoDB's process name) parameters to use a dbpath other than /data/db

To find out what paramters are available to you can you simplely run ...

$ mongod --help

And you'll get a list of helpful parameters, there are also extensive docs explaining the various differant parameters you can use when starting up mongod ...

http://www.mongodb.org/display/DOCS/Starting+and+Stopping+Mongo

http://www.mongodb.org/display/DOCS/Command+Line+Parameters

A bit from the docs on your issue ...

Starting mongod

Default Data Directory, Default Port To start Mongo in default mode, where data will be stored in the /data/db directory (or c:\data\db on Windows), and listening on port 27017, just type

$ ./mongod

Alternate Data Directory, Default Port

To specify a directory for Mongo to store files, use the --dbpath option:

$ ./mongod --dbpath /var/lib/mongodb/

Note that you must create the directory and set its permissions appropriately ahead of time -- Mongo will not create the directory if it doesn't exist.

Alternate Port

You can specify a different port for Mongo to listen on for connections from clients using the --port option

$ ./mongod --port 12345

This is useful if you want to run more than one instance of Mongo on a machine (e.g., for running a master-slave pair).

Running as a Daemon

Note: these options are only available in MongoDB version 1.1 and later.

This will fork the Mongo server and redirect its output to a logfile. As with --dbpath, you must create the log path yourself, Mongo will not create parent directories for you.

$ ./mongod --fork --logpath /var/log/mongodb.log --logappend

Solution 2

You can specify it with config file.

I found explanation here : http://blog.ajduke.in/2013/04/10/install-setup-and-start-mongodb-on-windows/

Using the config file

Instead of specifying command line option, we can specify same with use of file, which i call it here as config file Config file is just normal file, containing the parameters in the key=value form and each is on the every line of file In this, we basically provide path to file (which contains the configurations) as command line option as “-f” or “–config” Following is the snippet for the config file

shell > mongod --config I:\Servers\mongodb\config\mongodb.conf

And you get a file where you can specify dbpath :

#This is an example config file for MongoDB
#basic
dbpath = I:\Servers\mongodb\data
port = 27017
logpath = I:\Servers\mongodb\logs\mongo.log

Share:
15,452
easyrider
Author by

easyrider

Updated on June 17, 2022

Comments

  • easyrider
    easyrider almost 2 years

    When i try to start a service I get

    ~$ sudo service mongodb start
    mongodb start/running, process 20221
    

    but it doesn't really start ~$ sudo service mongodb status mongodb stop/waiting

    it's probably because my dbpath is NOT default so how to start a m service using non-default dbpath