Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I transfer a mongodb database to another machine that cannot see the first one

I have a server that is having trouble talking to the outside world. I would like to get its mongodb contents to another server--but since the servers cannot talk to eachother db.copyDatabase() won't do.

Is there something like mysqldump where I could dump the db into a binary file, scp that somewhere, and then use that to populate another mongodb server?

like image 918
Aaron Silverman Avatar asked Aug 29 '11 15:08

Aaron Silverman


People also ask

What are the other two ways of importing data to MongoDB?

You have two options. One is to write a script to restructure the data before using mongoimport to import the data. Another approach could be to import the data into MongoDB and then run an aggregation pipeline to transform the data into your required structure.


2 Answers

If you're using Ubuntu/Linux, run the following commands. First, mongodump on the origin server:

mongodump --db DataBaseName 

Copy the generated dump/DataBaseName folder to the new machine. Then, import using mongorestore:

mongorestore --db DataBaseName /path/to/DataBaseName  

Note that /path/to/DataBaseName should be a directory filled with .json and .bson representations of your data.

like image 56
Tom G Avatar answered Oct 06 '22 10:10

Tom G


Use the mongodump and mongorestore commands.

mongodump --db test --collection collection mongorestore --collection collection --db test dump/ 

You can also gzip. The documentation has more examples.

like image 21
Amir Raminfar Avatar answered Oct 06 '22 12:10

Amir Raminfar