Reputation: 2641
I have a MongoDB database that resides on a remote server machine whose IP address is 192.168.1.20 on a local network. For development and testing purposes, and since I am not allowed to modify or delete the database on the server for security purposes, I want to copy the database on my local machine for my personal use.
Can anyone please tell me, how do I achieve this?
Upvotes: 20
Views: 48180
Reputation: 2218
Use the ssh command to connect to your remote server:
ssh user@serverip
Once you are logged in to the server, use the mongodump command to create a backup of your database:
mongodump --db dbname --out backupdir
To copy the backup from your server to your local system using the "scp" command, you can follow these steps:
scp -r user@serverip:/path/to/backupdir /path/to/localdir
Upvotes: 0
Reputation: 2597
There is copy database command which I guess should be good fit for your need.
db.copyDatabase("DATABASENAME", "DATABASENAME", "localhost:27018");
Alternatively, you can just stop MongoDb, copy the database files to another server and run an instance of MongoDb there.
EDIT 2020-04-25
Quote from MongoDB documentation
MongoDB 4.0 deprecates the
copydb
and theclone
commands and their mongo shell helpersdb.copyDatabase()
anddb.cloneDatabase()
.As alternatives, users can use
mongodump
andmongorestore
(with themongorestore
options--nsFrom
and--nsTo
) or write a script using the drivers.
Reference here
Upvotes: 12
Reputation: 143
This should be a comment to the answer of @malla, but I don't have enough reputation to comment so I'm posting it here for other's reference.
In step 2, When you are trying to dump file from a remote server, remember to add out option so that you can restore locally later: (in my first try, I didn't add it and it failed, saying dump\db_name was not found).I'm not sure whether my way efficient or not. But it worked for me.
Step 2:
mongodump -h example.host.com --port 21018 -d dbname --username username --password yourpass --out <path_you_want_to_dump>
Step 3:
mongorestore -d theNameYouWantForYourLocalDB \<path_you_want_to_dump> + nameOfRemoteDB
Upvotes: 9
Reputation: 1708
I do this by creating a dump of the remote db to my local machine, which I then restore:
Make sure you have a mongo instance up and running (eg. run mongod.exe
from your bin folder in a terminal window. On my windows computer that's C:\mongodb\bin)
Make a dump from remote db: Open a new terminal window, move to the bin folder again, run:
mongodump -h example.host.com --port 21018 -d dbname --username username --password yourpass
(Change the parameters to suit your own situation.)
Restore the dumped database: Once the dump has been made, run the following command so that you have a local db:
mongorestore -d theNameYouWantForYourLocalDB dump\nameOfRemoteDB
(replace nameOfRemoteDB with the name of the remote db, the same as in previous command, and replace theNameYouWantForYourLocalDB with the name that you want your new local db to have)
Upvotes: 44
Reputation: 6510
You can use the mongoexport command to copy the database to your local machine.
Upvotes: 0
Reputation: 4306
mongodb has commandline tools for importing and exporting. Take a look at mongodump --collection collection --db test
and mongorestore --collection people --db accounts dump/accounts/
http://docs.mongodb.org/v2.2/reference/mongodump/ http://docs.mongodb.org/v2.2/reference/mongorestore/
this even works over the network
Upvotes: 1
Reputation: 1425
The mongoexport command: http://docs.mongodb.org/manual/core/import-export/
Or, mongodump command: http://docs.mongodb.org/manual/reference/program/mongodump/
Upvotes: 1