mongoDB4.2.8 Backup recovery and export import of recommended

  • 2021-01-03 21:08:54
  • OfStack

The backup

Data backup is to preserve the integrity of the data, to prevent power outages, virus infections and so on, so that the data is lost. If necessary, it is best to back up frequently to prevent data loss.

[

Create a backup directory:
mkdir -p /bigdata/mongodb-4.2.8/mongodump

Grammar:
mongodump -h localhost:27017 -d dmp_phone -o /bigdata/mongodb-4.2.8/mongodump

-ES14en: MongDB server address, for example: 127.0.0.1, of course you can also specify port number: 127.0.0.1:27017
-ES17en: Database instance that needs to be backed up, for example: test
-ES20en: The location of the backup data, for example: /home/mongodump/, of course, this directory needs to be established in advance, this directory holds the backup data of the database instance.

]

restore

[

Grammar:
mongorestore -h localhost:27017 -d dmp_phone --dir /bigdata/mongodb-4.2.8/mongodump/dmp_phone/

-ES33en: Server address where MongoDB is located
-ES36en: The database instance that needs to be restored, such as test, but this name can also be different from the one used at the time of backup, such as test2
-- dir: Where the data is backed up, for example: /home/mongodump/itcast/
drop: When restoring, first delete the current data, then restore the backup data. That is to say, after the recovery, backup added modified data will be deleted, caution!

]

export

[

mongoexport -d dmp_phone -c orders -o /bigdata/mongodb-4.2.8/mongodump/orders.csv --type csv -f "_id,item,price,quantity"

-ES55en database name
- c collection name
-o output filename
- type output format, default to json
-ES65en output field, if -ES66en is csv, you need to add -ES68en "field name"

]

The import

[

Grammar:
mongoimport -d dmp_phone -c orders --file /wangqingguo/bigdata/mongodb-4.2.8/mongodump/orders.csv --headerline --type csv

-ES79en database name
- c collection name
- type import format, default json
-f imported field name
If the import is in csv format, you can use the title of line 1 as the field for the import
-- The file file wants to import

]

Related articles: