I have 2 computers in different places (so it's impossible to use the same wifi network). One contains about 50GBs of data (MongoDB files) that I want to move to the second one which has much more computation power for analysis. But how can I make MongoDB on the second machine recognize that folder?
-
I think you need to provide some more details about your network topology and what you're trying to do. When you say, "recognize that folder", can you provide more context please? – cirrus Oct 06 '12 at 12:08
-
Do you just want to run mongodb on the bigger machine pointing to a different data folder or do you want to transfer the data from your current mongodb instance to the larger machine? – Deep Kapadia Oct 06 '12 at 14:13
5 Answers
When you start mongod
process you provide an argument to it --dbpath /directory
which is how it knows where the data folder is.
All you need to do is:
- stop the
mongod
process on the old computer. wait till it exits. - copy the entire /data/db directory to the new computer
- start
mongod
process on the new computer giving it--dbpath /newdirectory
argument.
The mongod
on the new machine will use the folder you indicate with --dbpath. There is no need to "recognize" as there is nothing machine specific in that folder, it's just data.

- 41,784
- 5
- 109
- 133
-
I did exactly what you said but It didn't work at all, that's why i was so confuse to explain my question. "recognize" means mongodb on new PC use my folder "from the old PC" as its "working database", but when i use "mongostat" it returns nothing. – Nguyễn Hoài Nam Oct 06 '12 at 14:33
-
1if you did exactly as I said then the new mongod would use the folders you copied from your old PC. Did you put them into folder on the new machine and then pass the name of that folder to mongod with --dbpath? and why is mongostat relevant? – Asya Kamsky Oct 06 '12 at 21:20
-
Well this photo is what i'm talking/wondering about https://dl.dropbox.com/u/13792394/Photo/mongo.png – Nguyễn Hoài Nam Oct 08 '12 at 01:28
-
it shows that mongo is up and running. Top window shows it came up successfully and is waiting for connections, then you connected with mongostat which shows that it's up and running (just no queries currently running. What is the problem that you see here? – Asya Kamsky Oct 08 '12 at 03:02
-
I also show that my working filder is not recognized in mongo as well as mongostat (or did i misunderstand what mongostat showing). Pardon me because i couldn't get on well with mongoDB. And i think mongodb should reload and recover those files in the new folder before establishing a new connection... Please comment if something is unclear here because i'm not native Englisg speaker. – Nguyễn Hoài Nam Oct 08 '12 at 07:34
-
Well after couple of hours, i could finally figured out my mistake. -1 reputation is deserved, thank all of you. Best regards! – Nguyễn Hoài Nam Oct 08 '12 at 09:48
-
7Maybe it would help others if you explained your mistake - why you thought the files weren't being used... – Asya Kamsky Oct 10 '12 at 06:08
-
1You can also edit the path in `/etc/mongod.conf` under storage -> dbPath – Luca Steeb Apr 03 '16 at 00:29
-
1@NguyễnHoàiNam I am having a similar issue, I copied a data folder from a colleague and when I try to connect to it, the db/collections are not loading. Please let me know what changes fixed your issue? Thanks. – kvm006 Feb 20 '18 at 18:40
-
Note that this question and answer was with MMAPV1 storage engine. With WiredTiger while you can do this with a single directory you would have to run off-line repair on the (new) dbpath directory to get metadata corrected. – Asya Kamsky Apr 21 '20 at 16:11
I did this myself recently, and I wanted to provide some extra considerations to be aware of, in case readers (like me) run into issues.
The following information is specific to *nix systems, but it may be applicable with very heavy modification to Windows.
If the source data is in a mongo server that you can still run (preferred)
Look into and make use of mongodump
and mongorestore
. That is probably safer, and it's the official way to migrate your database.
If you never made a dump and can't anymore
Yes, the data directory can be directly copied; however, you also need to make sure that the mongodb
user has complete access to the directory after you copy it.
My steps are as follows. On the machine you want to transfer an old database to:
- Edit
/etc/mongod.conf
and change the dbPath field to the desired location. - Use the following script as a reference, or tailor it and run it on your system, at your own risk.
- I do not guarantee this works on every system --> please verify it manually.
- I also cannot guarantee it works perfectly in every case.
- WARNING: will delete everything in the target data directory you specify.
- I can say, however, that it worked on my system, and that it passes
shellcheck
. - The important part is simply copying over the old database directory, and giving
mongodb
access to it throughchown
.
#!/bin/bash
TARGET_DATA_DIRECTORY=/path/to/target/data/directory # modify this
SOURCE_DATA_DIRECTORY=/path/to/old/data/directory # modify this too
echo shutting down mongod...
sudo systemctl stop mongod
if test "$TARGET_DATA_DIRECTORY"; then
echo removing existing data directory...
sudo rm -rf "$TARGET_DATA_DIRECTORY"
fi
echo copying backed up data directory...
sudo cp -r "$SOURCE_DATA_DIRECTORY" "$TARGET_DATA_DIRECTORY"
sudo chown -R mongodb "$TARGET_DATA_DIRECTORY"
echo starting mongod back up...
sudo systemctl start mongod
sudo systemctl status mongod # for verification

- 31
- 4
quite easy for windows, just move the data folder to the target location run cmd "C:\your\mongodb\bin-path\mongod.exe" --dbpath="c:\what\ever\path\data\db"
-
You don't necessarily need to move the data folder, you can change the dbPath in /bin/mongod.conf – Mohit Sharma Aug 24 '21 at 15:51
In case of Windows in case you need just to configure new path for data, all you need to create new folder, for example D:\dev\mongoDb-data
, open C:\Program Files\MongoDB\Server\6.0\bin\mongod.cfg
and change there path :
Then, restart your PC. Check folder - it should contains new files/folders with data.

- 10,289
- 4
- 52
- 53
Maybe what you didn't do was export or dump the database. Databases aren't portable therefore must be exported or created as a dumpfile.
Here is another question where the answer is further explained
-
Mongo database files seem to be quite portable. I just ran `cp -a
/var/lib/mongodb /var/lib/mongodb` and then `sudo chown -R mongodb:mongodb /var/lib/mongodb/*` and `sudo chown -R mongodb:mongodb /var/lib/mongodb` and everything works fine. – Jan 31 '18 at 15:01