Copying MongoDB Database into Local Machine

Razor picture Razor · Jan 3, 2014 · Viewed 27.4k times · Source

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?

Answer

malla picture malla · Aug 5, 2016

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)