Introducing MongoDB database backup and restore a single table derived
Address reprint:
https://www.cnblogs.com/dingjiaoyang/p/9663205.html
-------------------
MongoDB backup and recovery
-------------------
1, MongoDB database backup
1, grammar:
mongodump -h dbhost -d dbname -o dbdirectory
Parameter Description:
-h: MongDB where the server address, for example: 127.0.0.1, of course, you can specify the port number: 127.0.0.1: 27017
-d: database instance to be backed up, for example: test
-o: Backup data storage location, for example: / home / mongodump /, of course, need to establish in advance the directory, the directory backup data stored inside the database instance.
2. Example:
sudo rm -rf /home/momgodump/
sudo mkdir -p /home/momgodump
sudo mongodump -h 192.168.17.129:27017 -d itcast -o /home/mongodump/
-
2, MongoDB database recovery
1, grammar:
mongorestore -h dbhost -d dbname --dir dbdirectory
Parameters or name:
-h: MongoDB server address where
-d: database instance needs to be restored, for example: test, of course, the name can also be backed up and the time is not the same, such as test2
--dir: the location of the backup data, for example: / home / mongodump / itcast /
--drop: recovery time, first delete the current data, and then restore the backup data. That is, after the resumption, after adding backup modified data will be deleted, with caution!
2. Example:
mongorestore -h 192.168.17.129:27017 -d itcast_restore --dir /home/mongodump/itcast/
-------------------
MongoDB Data import and export
-------------------
1, export tool: mongoexport
1, concepts:
mongoDB in a collection mongoexport tool can be exported into a file format JSON or CSV format. Derived parameters can be specified by the data item, the data may be derived in accordance with the specified conditions.
2, the syntax:
mongoexport -d dbname -c collectionname -o file --type json/csv -f field
Parameter Description:
-d: database name
-c :collection名
-o: output file name
--type: output format, the default is json
-f: field of the output, if -type as csv, you need to add -f "field name"
3. Example:
sudo mongoexport -d mongotest -c users -o /home/python/Desktop/mongoDB/users.json --type json -f "_id,user_id,user_name,age,status"
2, data import: mongoimport
1, grammar:
mongoimport -d dbname -c collectionname --file filename --headerline --type json/csv -f field
Parameter Description:
-d: database name
-c :collection名
--type: the default format for importing json
-f: Field name Imported
--headerline: if the format is introduced csv, it may be used as the title of the first row of field introduced
--file: file to be imported
2. Example:
sudo mongoimport -d mongotest -c users --file /home/mongodump/articles.json --type json
-------------------
MongoDB backup and recovery
-------------------
1, MongoDB database backup
1, grammar:
mongodump -h dbhost -d dbname -o dbdirectory
Parameter Description:
-h: MongDB where the server address, for example: 127.0.0.1, of course, you can specify the port number: 127.0.0.1: 27017
-d: database instance to be backed up, for example: test
-o: Backup data storage location, for example: / home / mongodump /, of course, need to establish in advance the directory, the directory backup data stored inside the database instance.
2. Example:
sudo rm -rf /home/momgodump/
sudo mkdir -p /home/momgodump
sudo mongodump -h 192.168.17.129:27017 -d itcast -o /home/mongodump/
-
2, MongoDB database recovery
1, grammar:
mongorestore -h dbhost -d dbname --dir dbdirectory
Parameters or name:
-h: MongoDB server address where
-d: database instance needs to be restored, for example: test, of course, the name can also be backed up and the time is not the same, such as test2
--dir: the location of the backup data, for example: / home / mongodump / itcast /
--drop: recovery time, first delete the current data, and then restore the backup data. That is, after the resumption, after adding backup modified data will be deleted, with caution!
2. Example:
mongorestore -h 192.168.17.129:27017 -d itcast_restore --dir /home/mongodump/itcast/
-------------------
MongoDB Data import and export
-------------------
1, export tool: mongoexport
1, concepts:
mongoDB in a collection mongoexport tool can be exported into a file format JSON or CSV format. Derived parameters can be specified by the data item, the data may be derived in accordance with the specified conditions.
2, the syntax:
mongoexport -d dbname -c collectionname -o file --type json/csv -f field
Parameter Description:
-d: database name
-c :collection名
-o: output file name
--type: output format, the default is json
-f: field of the output, if -type as csv, you need to add -f "field name"
3. Example:
sudo mongoexport -d mongotest -c users -o /home/python/Desktop/mongoDB/users.json --type json -f "_id,user_id,user_name,age,status"
2, data import: mongoimport
1, grammar:
mongoimport -d dbname -c collectionname --file filename --headerline --type json/csv -f field
Parameter Description:
-d: database name
-c :collection名
--type: the default format for importing json
-f: Field name Imported
--headerline: if the format is introduced csv, it may be used as the title of the first row of field introduced
--file: file to be imported
2. Example:
sudo mongoimport -d mongotest -c users --file /home/mongodump/articles.json --type json