13

I have separate development and production MongoDB servers and I want to keep actual data in development server for sometime. What I should use for it: mongodump, mongoimport or something else?

Clarification: I want to copy data from production to development.

4 Answers 4

13

If it's a one time-thing

and you want fine control over parameters such as which collections to sync, you should use:

  • mongodump to dump bson files of your Production DB to your local machine
  • mongorestore to then, retrieve the dumped BSON files in your Local DB

Otherwise you should check out mongo-sync

It's a script I wrote for my self when I had to constantly copy my Local MongoDB database to and from my Production DB for a Project (I know it's stupid).

Once you put your DB details in config.yml, you can start syncing using two simple commands:

./mongo-sync push       # Push DB to Remote
./mongo-sync pull       # Pull DB to Local

If you use it inside some project, it's a good idea to add config.yml to .gitignore

mongo-sync demo gif

6
  • 2
    Out of interest why do you say it's stupid? I'm looking at how to handle this situation before I start a project - is there a better way to handle the development/production side of a mongodb? Mar 13, 2015 at 11:59
  • It's stupid because its not a good practice to test/develop on a production database. I wrote that script (in two different languages btw) because that was the requirement of the project and the client; that the data be entered into the db manually by the dev and to be only readable by the users.
    – Sheharyar
    Mar 13, 2015 at 12:05
  • Is that simply due to the inevitable growth and complexity a production database brings, or is there an angle I'm not considering? I'm actually more thinking the other way round though, I'll be developing locally but will want the data to go up with the site (rather than populate the data once in place) Mar 13, 2015 at 12:14
  • 1
    That depends on the project requirements I guess, if its an app with users it's usually a good idea to keep dev and prod dbs separate. But if its something like a blog, you can simply populate the db in dev and later push it to prod.
    – Sheharyar
    Mar 13, 2015 at 12:29
  • 1
    Replication of data from prod to dev makes total sense. If a bug occurs in production and you can't replicate it locally, it's wise to copy the prod data to your dev environment so you have an exact copy of that environment.
    – ffxsam
    Jun 16, 2016 at 22:23
8

You can use the db.copyDatabase(...) or db.cloneDatabase(...) commands:

http://www.mongodb.org/display/DOCS/Copy+Database+Commands

This is faster than mongodump / mongorestore because it skips creating the bson representation on disk.

1
3

When you want the dev database to look exactly like the production database, you can just copy the files. I am currently running a setup where I synchronize my MongoDB database between my desktop and my notebook with dropbox - even that works flawless.

4
  • Where is this database files are located in file system?
    – acelot
    Sep 15, 2012 at 9:45
  • The default is /data/db relative to the mongod binary. But you usually set the location of the database files with the --dbpath parameter when you start MongoDB.
    – Philipp
    Sep 15, 2012 at 13:25
  • Interesting idea. Does this even work if two developers work simultaneously on the same db synchronized over dropbox? Gonna develop an application with a friend over Internet. I thought of having one Master in the web and two local slaves(/masters) for me and my body. However this seems not to work since mongo cannot do a master/master sync or? What happens if i go offline and continue working? My slave becomes master on my local instance. What happens if I go back online? It is not like couch db i guess from reading docs. What happens if you put mongo into dropbox and run it simultaneous?
    – Manuel
    Jan 27, 2013 at 14:14
  • 1
    Using dropbox with two people doesn't sound like a good idea. In my setup I was never using both devices simultaneously.
    – Philipp
    Jan 28, 2013 at 9:06
0

Fix Mongosync Atlas to Atlas documentation examples to avoid confusion

Description

The documentation for Atlas to Atlas migration via mongosync has confusing examples (generic for on-prem servers) and a wrong example for SRV connection. Documentation

  • First example:
cluster0:
mongodb://clusterAdmin:[email protected]:20020,clusterOne02.fancyCorp.com:20020,clusterOne03.fancyCorp.com:20020
cluster1:
mongodb://clusterAdmin:[email protected]:20020,clusterTwo02.fancyCorp.com:20020,clusterTwo03.fancyCorp.com:20020 
  • Correction proposal: We should use cluster1Name.ab123.mongodb.net:27017 and cluster2Name.ab123.mongodb.net:27017 as generic name for an Atlas server name for each case and ensure we replace 27017 as default port instead of 20020 for all the examples on the linked documentation.

Using a generic server and a port different from the Atlas default connection port may cause confusion at the time to connect.

  • SRV final example:
 mongosync \
   --cluster0 'mongodb+srv://clusterAdmin:[email protected]:20020/' \
   --cluster1 'mongodb+srv://clusterAdmin:[email protected]:20020/'

Correction proposal:

This command is incorrect, the SRV connection string doesn't includes the port in Atlas. The correct command should be:

mongosync \    
--cluster0 'mongodb+srv://clusterAdmin:[email protected]/'\   
--cluster1 'mongodb+srv://clusterAdmin:[email protected]/' 

commands

mongosync --config mongosync.conf
 curl localhost:27182/api/v1/progress -XGET
 curl localhost:27182/api/v1/start -XPOST --data '{"source": "cluster0", "destiantion": "cluster1"}'

Note: run the first command in the background or use another terminal.

Let me know if you need any further clarifications on this.

Form more information: link here

Official Documentation click link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.