How to dump and restore database in MongoDB

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.

#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

Suggested Articles

What is MongoDB ? What are its benefits & drawbacks

Install MongoDB and connect with PHP

How to use MongoDB in Codeigniter

Thankyou for reaching here,Do follow us on our Facebook & instagram page for more updates.

Leave a Reply

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

The maximum upload file size: 2 MB. You can upload: image, document. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded.