MongoDB tutoria...login
MongoDB tutorial
author:php.cn  update time:2022-04-21 17:49:03

MongoDB backup and recovery



MongoDB data backup

In Mongodb we use the mongodump command to back up MongoDB data. This command can export all data to the specified directory.

The mongodump command can use parameters to specify the server where the exported data will be dumped.

Syntax

The mongodump command script syntax is as follows:

>mongodump -h dbhost -d dbname -o dbdirectory
  • -h:

    The server where MongDB is located Address, for example: 127.0.0.1, of course you can also specify the port number: 127.0.0.1:27017

  • -d:

    needs to be backed up Database instance, for example: test

  • -o:

    The backup data storage location, for example: c:\data\dump, of course The directory needs to be established in advance. After the backup is completed, the system automatically creates a test directory in the dump directory, which stores the backup data of the database instance.

Instance

Use 27017 locally to start your mongod service. Open the command prompt window, enter the bin directory of the MongoDB installation directory and enter the command mongodump:

>mongodump

After executing the above command, the client will connect to the MongoDB service with IP address 127.0.0.1 and port number 27017, and backup All data to bin/dump/ directory. The command output is as follows:

MongoDB数据备份

The mongodump command optional parameter list is as follows:

##mongodump --host HOST_NAME --port PORT_NUMBERThis command will back up all MongoDB data mongodump --host w3cschool.cc --port 27017mongodump --dbpath DB_PATH --out BACKUP_DIRECTORYmongodump -- dbpath /data/db/ --out /data/backup/mongodump --collection COLLECTION --db DB_NAMEThis command will back up the collection of the specified database. mongodump --collection mycol --db test

MongoDB Data Recovery

mongodb uses the mongorerstore command to restore backed up data.

Syntax

The mongorestore command script syntax is as follows:

>mongorestore -h dbhost -d dbname --directoryperdb dbdirectory
  • -h:

    The server where MongoDB is located Address

  • -d:

    The database instance that needs to be restored, for example: test. Of course, this name can also be different from the one during backup. For example, test2

  • --directoryperdb:

    The location of the backup data, for example: c:\data\dump\test, why do you need to add more here? A test, not a dump during backup, readers can check the tips themselves!

  • --drop:

    When restoring, first delete the current data, and then restore the backed-up data. That is to say, after restoration, any data added and modified after backup will be deleted, so use with caution!

Next we execute the following command:

>mongorestore

The output result of executing the above command is as follows:

MongoDB数据恢复

SyntaxDescriptionInstance