Data backup:
Open a command prompt and go to the installed MongoDB Bin directory:
The location is as follows:
Input command:mongodump-h 127.0.0.1:27017-d runoob-o D:\mongo\data\dump
which
-H:MONGDB the server address, for example: 127.0.0.1, you can also specify a port number
-D: A database instance that needs to be backed up, for example: Runoob (database name, which can back up all collections under that database)
-O: The data storage location of the backup, for example: D:\mongo\data\dump, after the backup is completed, the system automatically establishes a Runoob directory in the Dump directory, which contains the backup data of the database instance.
The following is a sketch of the execution process:
After viewing the backup, do you have the contents of the sample database to be stored under the Dump folder:
Yes, as we'd like. The data is backed up below the dump folder. Data recovery:
Scenario 1: Directly copy the files in the dump to the data directory and restart the MongoDB service.
Scenario 2: Using the Mongorestore command for data recovery
The first scenario is relatively simple, and we introduce the second scenario, which takes the form of a command
To facilitate testing, we first delete the database
Entering commands in the MongoDB shell
installed, would zero-out data files
MongoDB shell version:3.0.4
connecting to:test
> Use Runoob
swit Ched to DB Runoob
>
> Db.collections
runoob.collections
> Show Collections
col
Flatjson
Leveljson
postcollection
posts
runoob
system.indexes
users
> Db.dropdatabase ()
{"Dropped": "Runoob", "OK": 1}
> show Collections
>
Database deletion succeeded
Restore with backed-up data
Also open a command prompt, go to the installed MongoDB Bin directory, and then enter the command
mongorestore-h 127.0.0.1:27017-d runoob D:\mongo\data\dump\runoob
-h:mongodb server Address
-D: A database instance that needs to be restored, for example: twang, which can also be different from the time of backup, such as Test2
--directoryperdb: Where to back up your data, for example: D:\mongo\data\dump\runoob
Note: There is no--directoryperdb, try to add the command, error, data recovery failed. Remove to recover the data successfully
Execution Process Silhouette:
View Show collections in the shell, and all data collections are restored successfully.