MondoDB is a very popular Database Management system which is used by Large Organizations based on its capability to manage large amount of data.
We have seen a rising number of Linux Support requests from our clients regarding moving their MongoDB databases as part of our Linux Support Services.
In this context, we will look into how to move MongoDB database files to a production server.
Why do we move MongoDB database files?
Most programmers usually work on a database on a local environment for testing purposes before moving it to a production server. This is a very good method of development.
Of recent, one of our client sent us a support email as stated below;
Dear Sirs, How can i setup MongoDB on my Linux Server and move the databases.
Kindly do the configuration of the mongo database.
How to move MongoDB database files
To successfully migrate a database to a production server, care needs to be taken in taking a backup of an existing database, then restoring it into the destination server.
Follow the following steps;
i. Stop MongoDB on the original server
Before taking a backup of the original server, it is recommended that you stop the mongoDB service connection to avoid any loss of data.
Use the command below to stop the service;
service mongod stop
ii. Make a Mongodb database Backup
Next, make a backup of the database by using the command below;
mongodump --host <source host:port> --ssl --username <username> --password <password> --authenticationDatabase admin --db <sourceDbName>
For example, to do a backup of a database with name "sampledb", the command would be;
mongodump -h myservername:27017 --ssl -u user1 -p 123123 --authenticationDatabase admin -d sampledb
iii. Move the MongoDB database files to another production server
Now you can copy the database files to the production server. You can use the scp or rsync function to make this action.
iv. Restoration of the MongoDB database
After the backup process, the backup file will be created. You can restore it to the production server by using the command below;
mongorestore --host <target host:port> --ssl --username <username> --password <password> --authenticationDatabase admin --db <targetDbName> --collection <collection-name> <dump folder/file>
For example if you want to restore a database with name "livedb" , use the command below;
mongorestore --host=mynewserver --port=27017 --username=user --authenticationDatabase=admin --db livedb /opt/backup/mongodump-livedb
v. Granting access to users on the Production server
Finally, you have to grant access to users on the new server.
This is where assigning full permission in the database comes to play.
Additionally, you have to open the Mongodb default port 27017 to allow inbound and outbound traffic.