mongodb – remote copy of database

Connect with

MongoDB remote copy
There are different ways for MongoDB remote copy database from one server to another or from one cluster to another cluster. Using db.copyDatabase()

by using mongodump utility tool you can view another article : How to move data from staging server to production server of mongodb

1. By using db.copyDatabase() on terminal

Usedb.copyDatabase() for mongoDB remote copy database after logged-in into mongodb server.

file: mongoDB_sync.sh

#!/bin/bash

## fileName: mongoDB_sync.sh ####

#Sync mongodb database from remote to local where you run this.
#NOTE: be careful this overwrites the local copy (from where run) of the database if you have.
## here local means where you run this script, and remote means from where you want to get data.

remoteHost='mongodb.mysoftkey.com'
remoteUserDB='mysoftkeyUser'
remoteDBPasswd='password123'
remoteMongoDB='orderdb'
localMongoDB='orderdb'

mongo $localDb --eval "use '$localMongoDB'; db.dropDatabase(); db.copyDatabase('$remoteMongoDB', '$localMongoDB', '$remoteHost', '$remoteUserDB', '$remoteDBPasswd')"

2. By using db.copyDatabase() on mongos terminal

For mongoDB remote copy database the syntax to copy database as:

>db.copyDatabase(fromdb, todb, fromhost, username, password, mechanism);

db.copyDatabase() example:

> db.copyDatabase('orderdb', 'orderdb', '10.72.17.16:27017')

3.By using db.cloneCollection() on mongos terminal

db.cloneCollection() is used to mongoDB remote copy of collection and its meta data within a database.

> db.cloneCollection('10.72.17.16:27017', 'orderdb.orderColl');

4. Reference

You can visit docs.mongodb.com for more details.

Thanks for visiting this post for MongoDB remote copy database. You can also visit MongoDB Tutorial Listing page for more articles on MongoDB document-oriented database.
Happy Learning 🙂 for MongoDB remote copy of database using db.copyDatabase() and cloning of collection using db.cloneCollection().
Write your comment to improve this post. Happy Learning 🙂


Connect with

Leave a Comment

Your email address will not be published. Required fields are marked *