How to Migrate MongoDB Database from one server to another

Hi Guys, Welcome to Proto Coders Point. In this tutorial will learn How to migrate mongodb database to another server- export mongo data to another server instance.

So basically we are going to copy MongoDB Databases to a New Server, or you can say it as export mongodb data to new server.

Suppose you have data in GB’s and want to move the data to latest server, so Instead of taking backup (dump files of mongodb) in your local computer, then copying it again to new aws server. it will consume lots of internet and time is wasted a lot. So here is the solution, simple transfer mongodb files from server 1 to server 2 then use mongorestore command to restore the data to new server database.

Let’s Get Started

Video Tutorial

Export Mongodb from one server to another

I have 2 servers instance created on AMAZON WEB SERVICES (just for this tutorial).

  • server 1
  • server 2
aws server instance ec2

On both the server mongodb is been installed, learn how to installed mongodb on AWS ubuntu server.

In SERVER 1, I have data in mongodb that I want to transfer directly to server 2.

mongodb compass

Follow the below Steps

In Server 1 : Take mongodb backup & send to server 2

So In Server 1, We need to take backup of our mongodb database using mongodump cmd & then create zip/tar file of dump folder & transfer the tar/zip file to server 2.

Follow below Steps

Mongodump – Database backup

Step 1: move to path where you want to take mongodb dump backup files Eg: /var/www/

cd /var/www

Step 2: Mongodump – MongoDb Export Database

mongodump --uri="mongodb://username:password@<ipaddress>:27017/DatabaseName

My DB don’t have any authentication security, this tutorial is just to learn how to migrate database to server, learn How to secure mongodb authorization for remotely access Here.

Eg: without authentication

sudo mongodump --uri="mongodb://" 

Eg: With DB auth security

sudo mongodump --uri="mongodb://username:password@" 

Successfully created database backup dump

#image of dump file created for database backup

Step 3: Create tar.gz file of dump file

Being into the same directory where we have created mongodump backup, create tar.gz of dump folder.

** Create tar **
tar -czvf dump.tar.gz dump

Sending file server to server

To transfer files from one server to another server, we need below details of server 2.

  1. username of server 2
  2. ip address of server 2
  3. .pem file of server 2 (aws/google cloud provides you).

Here .pem file is very important, it helps use in communicating to server system.

Step 1: Keep .pem file to server 1

I have copied .pem file under /var/www folder

Step 2: Change permission of .pem file to read only

chmod -R 400 filename.pem

Step 3: Finally transfer file to server 2

scp -i mykey.pem somefile.txt <UserName>@<IP ADDRESS>:/


Below Screenshot is of server 2 file, as you can see file is been transfer successfully to server 2.

file transfer to server 2

In Server 2 – Mongodb data restoring

Check directory path to /var/www

On server 2 under /var/www you will see a tar/zip file that been transferred from server 1 to server 2, we need to extract it & then import the mongodb data in server 2 database i.e. mongodb restore.

Step 1: Extract tar.gz

sudo tar -xzvf dump.tar.gz

Step 2: Mongodb import database – mongorestore

sudo mongorestore --username rootUser --password abc123 --db mydb dump/mydb/


username & password are auth key of my database.

mydb is name of database.

dump/mydb/ is path of mongodb files.