How To Copy MongoDB Database Files To Another Server? Database

Copy MongoDB Database Files support the volume of data and it comes in handy in apps that need to scale up as per usage. Web Application developers frequently request to copy the Mongo DB from one server to another. We at Velan, help the developers as part of our Server Management Services.

This article describes the steps involved in copying Mongo DB files to production servers

Below are the steps to copy MongoDB database files from development to live server. The steps include the creation of backup, copying of the database to the destination server, restoring the database in the destination folder.

  1. Stop MongoDB on the source server

It is always good to shutdown the database servers to remove any connections before backing up the server. This helps in avoiding data loss.

To stop the MongoDB on the server, we would use the following command –

# servicemongod stop

  1. Creation of MongoDB database backup

To create a database backup, we use the mongodump command
# mongodump –host<source host:port> --ssl –username <username> --password <password> --authenticationDatabase admin –db<sourceDBName>

For eg: To take the dump of the database with name exampledb, the command will be
mongodump -h myservername:27017 --ssl -u user1 -p 123123 --authenticationDatabase admin -d exampledb

  1. Copy MongoDB database files to the destination server

The next step is to copy the backed-up files to the destination server. WE can use the scp or rsync commands to copy the files. WE used scp to transfer files as development and production servers were in AWS.

To make scp work, we need to adjust the security group settings from the AWS console. In normal servers, this requires firewall changes to that the connection between the servers can work.

  1. Restore MongoDB database in the destination server

Once the backup file has been copied to the destination server, mongorestore command is used to restore the database.
# mongorestore --host <target host:port> --ssl --username <username> --password <password> --authenticationDatabase admin --db<targetDbName> --collection <collection-name><dump folder/file>

For eg: To restore the exampledb database, we used
mongorestore --host=mynewserver --port=27017 --username=user --authenticationDatabase=admin --dbexampledb /opt/backup/mongodump-exampledb

  1. Access to users on the Production server

In certain cases, we may have to give access to users on the new servers.

To open access from your own host IP, you need to open port number 27017 (MongoDB server Port) to all connections or to specific IP in the firewall.

At Velan, our database support engineers can help you with your database-related issues. For details, please visit Outsourced Managed IT Services.

Credentials

Quick Connect With Us

captcha reload