问题
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?
回答1:
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.
回答2:
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
回答3:
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.
回答4:
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,
回答5:
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.
回答6:
Here is what I did, hope it is helpful to anyone else :
Steps:
- Stop your services that are using mongodb
- 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 - Create a new directory on the fresh harddisk -
mkdir /mnt/database
- Make sure that mongodb has privileges to read / write from that directory ( usually
chown mongodb:mongodb -R /mnt/database/mongodb
) - thanks @DanailGabenski. - Copy the data folder of your mongodb to the new location -
cp -R /var/lib/mongodb/ /mnt/database/
- Remove the old database folder -
rm -rf /var/lib/mongodb/
- Create symbolic link to the new database folder -
ln -s /mnt/database/mongodb /var/lib/mongodb
- Start mongod -
/etc/rc.d/rc.mongod start
- 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 ) - 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.
回答7:
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 :)
回答8:
user is mongod instead of mongodb
sudo chown mongod:mongod /newlocation
You can see logs for errors if service fails :-
/var/log/mongodb/mongod.log
回答9:
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"}
来源:https://stackoverflow.com/questions/5961145/changing-mongodb-data-store-directory