19

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?

serv-inc
  • 35,772
  • 9
  • 166
  • 188
Razor
  • 2,581
  • 6
  • 21
  • 27

7 Answers7

43

I do this by creating a dump of the remote db to my local machine, which I then restore:

  1. 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)

  2. 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.)

  3. 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)

qed
  • 22,298
  • 21
  • 125
  • 196
malla
  • 1,618
  • 1
  • 17
  • 23
  • 1
    Best way to do. The db.copyDatabase() didnt copy data and skip some collections. I'm working with small vm, and one just for mongo. I cant dump from these vm and doing it from my computer with this: `mongodump -h 00.00.00.00 -d nameOfRemoteDB mongorestore -h 00.00.00.00 -d nameOfRemoteDB ./dump/db` is the best manipulation. – Georgio Nov 10 '16 at 13:52
  • Do you know how will it work with a database that has replica sets? – Joseph Oct 24 '19 at 08:02
  • The first part of this answer was helpful, so +1. I'm new to Mongo. I get this error when trying the mongorestore step: `The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION}` https://docs.mongodb.com/database-tools/mongorestore/#examples – Ryan Jan 20 '22 at 17:42
  • Just to complete the answer I had to set `--authenticationDatabase admin` as indicated on this [stack overflow post](https://stackoverflow.com/a/32013726/3123338) – Mister Q May 25 '22 at 13:54
12

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 the clone commands and their mongo shell helpers db.copyDatabase() and db.cloneDatabase().

As alternatives, users can use mongodump and mongorestore (with the mongorestore options --nsFrom and --nsTo) or write a script using the drivers.

Reference here

sas
  • 2,563
  • 1
  • 20
  • 28
  • Thank you very much for replying. Where exactly do I run this command? Sorry for sounding like a noob as I am a rookie in MongoDB and have just been learning it from the past 24 hours. – Razor Jan 03 '14 at 11:05
  • 1
    in terminal, first start mongo in terminal then execute these command – sas Jan 03 '14 at 11:44
  • I get ```/* 1 */ { "ok" : 0.0, "errmsg" : "unauthorized" }``` – SSH This May 13 '16 at 22:36
  • Please check credentials and roles – sas May 18 '16 at 10:06
  • { "ok": 0, "errmsg": "not master", "code": 10107 } error – prayagupa Aug 14 '16 at 03:12
  • 2
    I think it's better to use mongodump for this – iwein Feb 03 '17 at 09:01
  • And how we can copy local database to mongo AWS ec2 instance? I have a Node app hosted in ec2 and I have used Bitnami MEAN. – WhoAmI Jan 23 '19 at 05:45
  • 3
    MongoDB 4.0 deprecates the copydb and the clone commands and their mongo shell helpers db.copyDatabase() and db.cloneDatabase(). See https://docs.mongodb.com/manual/release-notes/4.0-compatibility/#copydb-and-clone-commands – whallz Mar 08 '19 at 20:52
7

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
Carol.Z
  • 133
  • 1
  • 6
1

The mongoexport command: http://docs.mongodb.org/manual/core/import-export/

Or, mongodump command: http://docs.mongodb.org/manual/reference/program/mongodump/

10 cls
  • 1,325
  • 2
  • 17
  • 31
1

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

Dominik Goltermann
  • 4,276
  • 2
  • 26
  • 32
0

You can use the mongoexport command to copy the database to your local machine.

Munim
  • 6,310
  • 2
  • 35
  • 44
0

Server to local machine

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

Mr Random
  • 1,992
  • 7
  • 20