Changing MongoDB data store directory
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?
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.
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
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.
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,