Changing MongoDB data store directory
Asked Answered
E

11

220

Until now I have not been specifying a MongoDB data directory and have had only one 30 GB primary partition.

I just ran out of space and added a new hard disk. How can I transfer my data (that is apparently in /var/lib/mongodb/) and configure MongoDB so that everything runs off of the new disk without affecting my existing installation?

Enamor answered 11/5, 2011 at 8:10 Comment(1)
You should also make sure your permissions on the new mount point have r+x: sudo chmod o+rx /media for example if your mount point is media. The r+x needs to be available for the 'other' groups and users from the first mount point.Masterson
H
251

The short answer is that the --dbpath parameter in MongoDB will allow you to control what directory MongoDB reads and writes it's data from.

mongod --dbpath /usr/local/mongodb-data

Would start mongodb and put the files in /usr/local/mongodb-data.

Depending on your distribution and MongoDB installation, you can also configure the mongod.conf file to do this automatically:

# Store data in /usr/local/var/mongodb instead of the default /data/db
dbpath = /usr/local/var/mongodb

The official 10gen Linux packages (Ubuntu/Debian or CentOS/Fedora) ship with a basic configuration file which is placed in /etc/mongodb.conf, and the MongoDB service reads this when it starts up. You could make your change here.

Heptode answered 11/5, 2011 at 8:25 Comment(8)
On my debian box it's mongodb.conf, not mongod.confDragline
I updated /etc/mongodb.conf per this solution. Then, after starting mongod, I saw that /data/db was used. Why did updating mongodb.conf not affect where mongod stored the db? (Note that using the --dbpath argument worked.Serpasil
On my Fedora box it is /etc/mongodb.conf and updating that works fine for me.Bifilar
The new data directory needs to be chown'd to the mongod user for the service start scripts.Ursal
how do I do chown?Anhinga
And what about transferring files , once i changed the dbpath ? I guess I would not be able to use the older data from my old dbpath ?Wendelin
In Windows: don't forget to remove the old mongodb service with sc delete MongoDB_service_name in administrative command. After that reinstall the MongoDB service.Ataman
@KevinMeredith Maybe it's because that MongoDB has no default config file. https://mcmap.net/q/120616/-what-is-the-default-database-path-for-mongodbGaeta
S
123

Resolved it in 2 minutes downtime :)
Just move your folder, add symlink, then tune permissions.

sudo service mongod stop
sudo mv mongodb /new/disk/mongodb/
sudo ln -s /new/disk/mongodb/ /var/lib/mongodb
sudo chown mongodb:mongodb /new/disk/mongodb/
sudo service mongod start

# test if mongodb user can access new location:
sudo -u mongodb -s cd /new/disk/mongodb/
# resolve other permissions issues if necessary
sudo usermod -a -G <newdisk_grp> mongodb
Schaumberger answered 11/7, 2014 at 17:5 Comment(9)
I think this should be the answer. I always find symbolic links to be a very good solution and I promote any solution that uses them over changing configuration etc..Valery
Probably no need to usermod. You're missing the +x executable permission to "other" users on the dbpath's tree chmod -R o+x /<home>. see: https://mcmap.net/q/120617/-mongodb-server-not-starting-up-after-changing-dbpathSoyuz
I follow these instructions (using dbpath in another disk) , i'm still getting Permission denied: "/var/lib/mongodb" This is how /home/../mongodb/mongodb/ looks like: drwxrwxr-x 3 mongodb mongodb 4096 oct 13 09:32 ../ drwxr-xr-x 2 mongodb nogroup 4096 oct 13 09:29 journal/ -rw------x 1 mongodb nogroup 67108864 sep 23 14:44 local.0* -rw------x 1 mongodb nogroup 16777216 sep 23 14:44 local.ns* -rwxr-xr-x 1 mongodb mongodb 0 oct 13 09:29 mongod.lock*Alanis
need to make sure the symlink is correct and mongodb user can indeed operate in the /data/db directory (create if does not exists)Corrode
need clarification to where to what folder you are in when you run the mv command, if you run the mv command from within lib, you no longer have mongodb to ln toChercherbourg
What is newdisk_grp and why would we need to do that last command?Masterson
I have tried your solution but when I am restarting the application it giving following error - 2019-11-18T18:58:45.215+1100 I STORAGE [initandlisten] exception in initAndListen: 28596 Unable to determine status of lock file in the data directory /var/lib/mongo: boost::filesystem::status: Permission denied: "/var/lib/mongo/mongod.lock", terminatingNewfashioned
in ubuntu/debian: sudo service mongod startStupa
To be more clear the second command's first parameter, mongodb means the path of MongoDB installed. Usually it will be: /var/lib/mongodb And the second parameter will be where you want to move. ThanksAton
B
41

The following command will work for you, if you want to change default path. Just type this in bin directory of mongodb.

mongod --dbpath=yourdirectory\data\db

In case you want to move existing data too, then just copy all the folders from existing data\db directory to new directory before you execute the command.

And also stop existing mongodb services which are running.

Bowra answered 8/3, 2017 at 4:34 Comment(1)
This works on Linux too. I've created a directory called "data" next to mondod script and run it like this: ./mongod --dbpath=dataSmokeless
C
29

Create a file called mongod.cfg in MongoDB folder if you dont have it. In my case: C:\Users\ivanbtrujillo\MongoDB

Then, edit mongod.cfg with notepad and add a line with the following (our custom dbpath):

dbpath=C:\Users\ivanbtrujillo\MongoDB\data\db

In this file you should especify the logpath too. My mongod.cfg file is:

logpath=C:\Users\ivanbtrujillo\MongoDB\log\mongo.log
dbpath=C:\Users\ivanbtrujillo\MongoDB\data\db

If you uses mongoDB as a windows service, you have to change this key and especify the mongod.cfg file.

To install mongodb as a windows service run this command:

**"C:\Users\ivanbtrujillo\MongoDB\bin\mongod.exe" --config "C:\Users\ivanbtrujillo\MongoDB\mongod.cfg" –install**

Open regedit.exe and go to the following route:

HKEYLOCALMACHINE\SYSTEM\CurrentControlSet\services\MongoDB

MongoDB service does not work, we have to edit the ImagePath key, delete its content and put the following:

**"C:\Users\ivanbtrujillo\MongoDB\bin\mongod.exe" --config "C:\Users\ivanbtrujillo\MongoDB\mongod.cfg" 
--logpath="C:\Users\ivanbtrujillo\MongoDB\log\mongo.log" –service**

We indicates to mongo it's config file and its logpath.

Then when you init the mongodb service, it works.

Here is a full tutorial to install mongoDB in windows: http://ivanbtrujillo.herokuapp.com/2014/07/24/installing-mongodb-as-a-service-windows/

Hope it helps,

Carbide answered 14/8, 2014 at 8:25 Comment(2)
MongoDB 2.6 and newer use YAML config files. Old format is still supported for backwards compatibility, but you may want to use the new one. Details here.Queenstown
dbpath variable also exists for linux as well. Look in /etc/mongod.conf.Tartarus
L
25

Copy the contents of /var/lib/mongodb to /data/db. The files you should be looking for should have names like your_db_name.ns and your_dbname.n where n is a number starting with 0. If you do not see such files under /var/lib/mongodb, search for them on your filesystem.

Once copied over, use --dbpath=/data/db when starting MongoDB via the mongod command.

Lactescent answered 11/5, 2011 at 8:24 Comment(2)
is there a way to get this to happen every time the server restart?Onestep
@JimThio you can add this switch to the shell script that starts mongodb. Typically it is /etc/init.d/mongod or /etc/init.d/mongodb.Lactescent
C
21

Here is what I did, hope it is helpful to anyone else :

Steps:

  1. Stop your services that are using mongodb
  2. Stop mongod - my way of doing this was with my rc file /etc/rc.d/rc.mongod stop, if you use something else, like systemd you should check your documentation how to do that
  3. Create a new directory on the fresh harddisk - mkdir /mnt/database
  4. Make sure that mongodb has privileges to read / write from that directory ( usually chown mongodb:mongodb -R /mnt/database/mongodb ) - thanks @DanailGabenski.
  5. Copy the data folder of your mongodb to the new location - cp -R /var/lib/mongodb/ /mnt/database/
  6. Remove the old database folder - rm -rf /var/lib/mongodb/
  7. Create symbolic link to the new database folder - ln -s /mnt/database/mongodb /var/lib/mongodb
  8. Start mongod - /etc/rc.d/rc.mongod start
  9. Check the log of your mongod and do some sanity checking ( try mongo to connect to your database to see if everything is all right )
  10. Start your services that you stopped in point 1

There is no need to tell that you should be careful when you do this, especialy with rm -rf but I think this is the best way to do it.

You should never try to copy database dir while mongod is running, because there might be services that write / read from it which will change the content of your database.

Crustaceous answered 21/12, 2014 at 11:31 Comment(3)
Although an old answer, it did work partially for me. You also need to make sure the mongodb user owns the new directory and the symlik in order for it to work. chown mongodb:mongodb -R /mnt/database/mongodbShellieshellproof
At point 5, make command to look like cp -rp /var/lib/mongodb/ /mnt/database/ This will keep ownership and permission of folder intact.Triplicity
I have tried your solution but when I am restarting the application it giving following error - 2019-11-18T18:58:45.215+1100 I STORAGE [initandlisten] exception in initAndListen: 28596 Unable to determine status of lock file in the data directory /var/lib/mongo: boost::filesystem::status: Permission denied: "/var/lib/mongo/mongod.lock", terminatingNewfashioned
J
10

If installed via apt-get in Ubuntu 12.04, don't forget to chown -R mongodb:nogroup /path/to/new/directory. Also, change the configuration at /etc/mongodb.conf.

As a reminder, the mongodb-10gen package is now started via upstart, so the config script is in /etc/init/mongodb.conf

I just went through this, hope googlers find it useful :)

Jannelle answered 21/5, 2013 at 1:26 Comment(0)
C
1

For me, the user was mongod instead of mongodb

sudo chown mongod:mongod /newlocation

You can see the logs for errors if the service fails:

/var/log/mongodb/mongod.log

Chilon answered 16/1, 2017 at 9:26 Comment(0)
W
0

In debian/ubuntu, you'll need to edit the /etc/init.d/mongodb script. Really, this file should be pulling the settings from /etc/mongodb.conf but it doesn't seem to pull the default directory (probably a bug)

This is a bit of a hack, but adding these to the script made it start correctly:

add:

DBDIR=/database/mongodb

change:

DAEMON_OPTS=${DAEMON_OPTS:-"--unixSocketPrefix=$RUNDIR --config $CONF run"}

to:

DAEMON_OPTS=${DAEMON_OPTS:-"--unixSocketPrefix=$RUNDIR --dbpath $DBDIR --config $CONF run"}
Whitcomb answered 30/7, 2014 at 5:9 Comment(1)
what if you want to put the database in your home directory instead of root?Sind
A
0

I found a special case that causes symlinks to appear to fail:

I did a standard enterprise install of mongodb but changed the /var/lib/mongodb to a symlink as I wanted to use an XFS filesystem for my database folder and a third filesystem for the log folder.

$sudo systemctl start mongod (fails with a message no permission to write to mongodb.log).. but it succceded if I started with the same configuration file:

.. as the owner of the external drives (ziggy) I was able to start $mongod --config /etc/mongodb.conf --fork

I eventually discovered that .. the symlinks pointed to a different filesystem and the mongodb (user) did not have permission to browse the folder that the symlink referred. Both the symlinks and the folders the symlinks referred had expansive rights to the mongod user so it made no sense?

/var/log/mongodb was changed (from the std ent install) to a symlink AND I had checked before:

$ ll /var/log/mongodb lrwxrwxrwx 1 mongodb mongodb 38 Oct 28 21:58 /var/log/mongodb -> /media/ziggy/XFS_DB/mongodb/log/

$ ll -d /media/ziggy/Ext4DataBase/mongodb/log drwxrwxrwx 2 mongodb mongodb 4096 Nov 1 12:05 /media/ashley/XFS_DB/mongodb/log/

.. But so it seemed to make no sense.. of course user mongodb had rwx access to the link, the folder and to the file mongodb.log .. but it couldnt find it via the symlink because the BASE folder of the media couldnt be searched by mongodb.

SO.. I EVENTUALLY DID THIS: $ ll /media/ziggy/ . . drwx------ 5 ziggy ziggy 4096 Oct 28 21:49 XFS_DB/

and found the offending missing x permissions..

$chmod a+x /media/ziggy/XFS_DB solved the problem

Seems stupid in hindsight but no searches turned up anything useful.

Actinouranium answered 1/11, 2020 at 17:57 Comment(1)
check your selinux flags of the symlink and filesPathway
T
0

Here's how i changed mine

Open the mongodb config file using vim or nano (The default mongodb config file is located at /usr/local/etc/mongod.conf)

Add the following

storage:
    dbPath: "/usr/local/var/mongodb"

It's a YAML-based configuration file format, so pay attention no tabs only spaces, and space after ": "

Save the file and restart your mongodb

Tother answered 27/12, 2023 at 13:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.