wheresrhys
wheresrhys

Reputation: 23560

Moving mongodb database to new location

When initially setting up mongodb I must've done something wrong as my data isn't being written to /data/db as I intended. I have no idea where my data is being stored (doing a search in finder for the db naem returns no results and I can't find in the docs how I can find out the current directory used). How can I safely point mongodb to use my desired directory while making sure it moves all my existing data to the new location?

Upvotes: 0

Views: 259

Answers (1)

david.storch
david.storch

Reputation: 691

The way to change the dbpath is to stop your mongod instance, copy the contents of the dbpath directory to their new location, and then restart it with the new dbpath parameter set properly.

First, in order to determine the current setting of the dbpath, you can use the getCmdLineOpts command. Here's how to use it from the mongo shell:

> db.serverCmdLineOpts()
{
    "argv" : [
        "mongod",
        "--config=/Users/dstorch/foo.conf"
    ],
    "parsed" : {
        "config" : "/Users/dstorch/foo.conf",
        "dbpath" : "/data/db"
    },
    "ok" : 1
}

As you can see, "parsed.dbpath" will give the current value of the dbpath.

Say you wanted to change the dbpath from /data/db to /data/db2. First you need to shutdown mongod. You do this by sending SIGTERM, either with Control-C or with kill <pid>. A third option is to run the shutdown command.

When mongod has safely exited, copy your data to its new location, and restart with the new dbpath:

cp -r /data/db /data/db2
mongod --dbpath /data/db2

Upvotes: 1

Related Questions