Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the default location of mongodb?

I installed MongoDB on Yosemite using brew. I understand that the default storage location is /data/db. I'd like to change this to location /Volumes/Data/mongodb, so that when I run 'mongod', it will choose the databases in /Volumes/Data/mongodb by default. I tried editing the mongod.conf file, and I have

storage:
  dbPath: /Volumes/Data/mongodb

However, whenever I run 'mongod', it gives me the message:

 ERROR: dbpath (/data/db) does not exist.
 Create this directory or give existing directory in --dbpath.
 See http://dochub.mongodb.org/core/startingandstoppingmongo

If I give mongod the --dbpath argument, it works fine, but I want it to work by default. Why does it appear that my mongod.conf file is not obeyed. Searching on google, it seems the conf file in installed in different locations depending on OS and install method. Running db.serverCmdLineOpts() gives:

{
    "argv" : [
        "/usr/local/opt/mongodb/bin/mongod",
        "--config",
        "/usr/local/etc/mongod.conf"
    ],
    "parsed" : {
        "config" : "/usr/local/etc/mongod.conf",
        "net" : {
            "bindIp" : "127.0.0.1"
        },
        "storage" : {
            "dbPath" : "/Volumes/Data/mongodb"
        },
        "systemLog" : {
            "destination" : "file",
            "logAppend" : true,
            "path" : "/usr/local/var/log/mongodb/mongo.log"
        }
    },
    "ok" : 1
}
like image 212
user1080952 Avatar asked Feb 11 '23 17:02

user1080952


2 Answers

Notes

  1. The path must exist. It doesn't get created automagically.
  2. /Volumes is sort of a reserved directory for mounting drives and bundles and shouldn't be worked on directly.
  3. In case you use a volume or (sparse) bundle, you need to make sure it is mounted on boot or you have to do it manually before starting MongoDB.

Basically, there are two places to put MongoDB data in OS X. Sadly, neither brew nor MacPorts obey the File Hierarchy Standard*, which is also adopted by the BSD which is the foundation of OS X.

Solution 1: You want the data accessible by all users of OS X

Since mongod is run from /usr/local subtree, so this is our parent. But since variable data belongs to /var we need to use the local tree there. So our base path for the data is /var/local/lib. It is mongoldb data, so you can either put it into mongo (because of the package basename) or mongodb (because of the vendor name) or even mongod (because of the daemon name). That's basically a matter of taste, but I'd stick with the vendor. So your dbpath would be /var/local/lib/mongodb.

Solution 2: Only you will access the data

Put the data into your home directory.

Well, basically you can do what you want there, but in general I'd put data into a hidden directory (prefixed with a dot) so that it does not clutter your Finder. Something like $HOME/.mongodb

This solution isn't really clean, since the software will run from a public subtree and the data is stored in a user directory.

Extension to both solutions

If you want to put your data into a volume or (sparse) bundle, simply create a symlink from the correct location to the volume, simply create a symlink instead of creating a directory. Example for solution 1:

sudo ln -s /Volumes/YourVolume /var/local/lib/mongodb

* Well, it is to argue that since MacPorts installs under /opt, it technically does (though imho it would rather belong to /usr/local in the first place)

like image 79
Markus W Mahlberg Avatar answered Feb 14 '23 19:02

Markus W Mahlberg


AFAIK, mongod needs to be configured before working. There are 2 ways to specify configurations.

  1. by command line arguments. which is the --dbpath parameter you added. for example:

    mongod --dbpath /Volumes/Data/mongodb
    
  2. by specifying a config file. Usually if you install from source, it comes with one at /etc/mongod.conf (varies depends on different Linux distributions). Where you can specify all the parameters in it.

By saying

I want it to work by default

I assume you mean you want to start daemon on system reboot. So the config file you are actually using is specified in the daemon script. For example in CentOS you can find daemon script in /etc/init.d/mongod, where the line

CONFIGFILE="/etc/mongod.conf"

decides which config file you are using. So you may want to find your daemon script and check it first.

If this is not your situation, maybe you just downloaded mongodb from somewhere and unzipped it to your system, and you start mongodb with nothing but:

mongod

This way I guess the default location of mongod.conf is decided by the compiling parameters. Which means if you want to change it, you'll have to download the source code and compile it yourself.

like image 42
yaoxing Avatar answered Feb 14 '23 21:02

yaoxing