Hello Guys, Today in this article we will learn how to dump and restore the database in MongoDB.
This article was suggested by one of my friend’s Ravi. Dumping and restoring your database is very easy and fast too. So let’s start our tutorial.
whether you want to dump or restore a database the first step you need to follow is starting your mongo server.If you don’t know how to start your server follow the below steps.
TABLE OF CONTENTS
#Start Mongo server on the local server
- Open your CMD(Command Prompt) or use window+R and type cmd, then enter CMD will be opened.
- write mongod This will start your mongo server.
- Again open another CMD and write mongo This will give access to use the mongo database.
- If you are getting any error like no such file found or any other error after executing mongod, it means either you have not installed mongo or you didn’t set path in environment variables.
- If you didn’t set a path then go to your program files or program files (x86) there if you find the MongoDB folder then open it up to bin.
- It may be something like this C:\Program Files\MongoDB\Server\3.2\bin or C:\Program Files (x86)\MongoDB\Server\3.2\bin and there you can open CMD by selecting path on the top and enter cmd, then CMD is opened and follow the above process.
#Start Mongo server on your live server
- Open your Terminal which can be done via Putty.
- Write mongo This will give access to use the mongo database. In this you no need to use mongod as the server is already running.
- If you are getting any error like no such file found or any other error after executing mongo, it means you have not installed mongo in your server.
- Install it and then follow the process.
#Dump Database to the specified path#
To dump the database, as I said first start your server with mongod & mongo and again open another CMD then go to the path where you want to download your database. Like if i want to dump it to desktop i use cd desktop, then the path is to desktop.
#Dump all Database#
Write mongodump To download all databases, with this all the database present are downloaded to your specified path.
#Dump single Database#
Write mongodump -d yourdatabsename To download all databases, with this all the database present are downloaded to your specified path.
#Restore your database#
Write mongorestore To import all databases present in dump folder, with this all the database present are imported to mongo.
You can check if database are imported or not using show dbs which shows all the databases present in your mongo.
This is the way you can dump and restore your database in mongo