当前位置:网站首页>Summary of import, export, backup and recovery of mongodb
Summary of import, export, backup and recovery of mongodb
2022-07-07 13:11:00 【cui_ yonghua】
The basic chapter ( Can solve the problem of 80% The problem of ):
MongoDB data type 、 Key concepts and shell Commonly used instructions
MongoDB Various additions to documents 、 to update 、 Delete operation summary
Advanced :
Other :
One . Import and export
1.1 Export tool mongoexport
Mongodb Medium mongoexport A tool can turn a collection Export to JSON Format or CSV File format . The exported data items can be specified by parameters , You can also export data according to specified conditions .
mongoexport The specific usage is as follows :
mongoexport -d dbname -c collectionname -o file --type json/csv -f field
Parameter description :
-d Database name
-c collection name
-o Output file name
—type Format of output , The default is json
-f Output field , If —type by csv, You need to add -f “ Field name ”
Example : Export set articles, Field _id,author,dave,score,views
[email protected]:/home/mongodump$ sudo mongoexport -d itcast -c articles -o /home/mongodump/articles.json --type json -f "_id,author,dave,score,views"
2016-09-15T20:33:50.870+0800 connected to: localhost
2016-09-15T20:33:50.871+0800 exported 7 records
1.2 Data import mongoimport
mongoimport The specific usage is as follows :
mongoimport -d dbname -c collectionname --file filename --headerline --type json/csv -f field
Parameter description :
-d Database name
-c collection name
—type Import format , Default json
-f Imported field name
—headerline If the imported format is csv, You can use the title of the first row as the imported field
—file File to import
Example : Import collection articles_import, Field _id,author,dave,score,views
[email protected]:/home/mongodump$ sudo mongoimport -d itcast -c articles_import --file /home/mongodump/articles.json --type json
2016-09-15T20:41:05.682+0800 connected to: localhost
2016-09-15T20:41:05.706+0800 imported 7 documents
Two . Backup recovery
2.1 Why do you need to back up the database regularly
Data backup is to preserve the integrity of data , Prevent power failure , Virus infection and so on , Lose data . If necessary , It's best to back up frequently , Prevent data loss .
The main reason : Need to know , It's dangerous to surf the Internet on earth , Even if safety precautions are taken , Unforeseen problems will inevitably occur .
2.2 MongoDB The data backup
stay Mongodb Use in mongodump Command to back up MongoDB data . This command can export all data to the specified directory .
mongodump --help: Optional parameter list
mongodump The syntax of the command script is as follows :
mongodump -h dbhost -d dbname -o dbdirectory-h:MongDB Address of the server , for example :127.0.0.1, Of course, you can also specify the port number :127.0.0.1:27017-d: Database instances that need to be backed up , for example :test-o: Backup data storage location , for example :/home/mongodump/, Of course, the directory needs to be established in advance , This directory contains the backup data of the database instance .
Case study : Create a backup database storage location , Execute export command
[email protected]:~$ sudo rm -rf /home/mongodump/
[email protected]:~$ sudo mkdir -p /home/mongodump/
[email protected]:~$ sudo mongodump -h 192.168.17.129:27017 -d itcast -o /home/mongodump/
After executing the above order , Clients will connect to ip by 192.168.17.129 The port number is 27017 Of MongoDB Service , And back up all data to /home/mongodump/ Directory .
2.3 MongoDB Data recovery
mongodb Use mongorerstore Command to restore the backed up data .
mongorestore --help: Optional parameter list
grammar : mongorestore The syntax of the command script is as follows :
mongorestore -h dbhost -d dbname --dir dbdirectory
-h:MongoDB Address of the server
-d: Database instances that need to be recovered , for example :test, Of course, this name can also be different from the backup time , such as test2
—dir: Location of backup data , for example :/home/mongodump/itcast/
—drop: When you recover , Delete the current data first , Then restore the backup data . That is to say , After recovery , The data added and modified after backup will be deleted , Use with caution !
Next, let's execute the following command :
mongorestore -h 192.168.17.129:27017 -d itcast_restore --dir /home/mongodump/itcast/
边栏推荐
- MySQL入门尝鲜
- Shortcut key of Bash
- . Net ultimate productivity of efcore sub table sub database fully automated migration codefirst
- 为租客提供帮助
- JNA学习笔记一:概念
- test
- Sequoia China completed the new phase of $9billion fund raising
- MongoDB优化的几点原则
- 【Presto Profile系列】Timeline使用
- Sample chapter of "uncover the secrets of asp.net core 6 framework" [200 pages /5 chapters]
猜你喜欢

达晨与小米投的凌云光上市:市值153亿 为机器植入眼睛和大脑

Vscode编辑器ESP32头文件波浪线不跳转彻底解决

.Net下極限生產力之efcore分錶分庫全自動化遷移CodeFirst

About the problem of APP flash back after appium starts the app - (solved)

Aosikang biological sprint scientific innovation board of Hillhouse Investment: annual revenue of 450million yuan, lost cooperation with kangxinuo

How to continue after handling chain interruption / sub chain error removed from scheduling

ISPRS2021/遥感影像云检测:一种地理信息驱动的方法和一种新的大规模遥感云/雪检测数据集

.Net下极限生产力之efcore分表分库全自动化迁移CodeFirst

Analysis of DHCP dynamic host setting protocol

ESP32构解工程添加组件
随机推荐
抓细抓实抓好安全生产各项工作 全力确保人民群众生命财产安全
What if the xshell evaluation period has expired
服务器到服务器 (S2S) 事件 (Adjust)
为租客提供帮助
error LNK2019: 无法解析的外部符号
《ASP.NET Core 6框架揭秘》样章[200页/5章]
Practical example of propeller easydl: automatic scratch recognition of industrial parts
日本政企员工喝醉丢失46万信息U盘,公开道歉又透露密码规则
Why can basic data types call methods in JS
DETR介绍
ISPRS2021/遥感影像云检测:一种地理信息驱动的方法和一种新的大规模遥感云/雪检测数据集
Per capita Swiss number series, Swiss number 4 generation JS reverse analysis
. Net ultimate productivity of efcore sub table sub database fully automated migration codefirst
How to continue after handling chain interruption / sub chain error removed from scheduling
Cloud detection 2020: self attention generation countermeasure network for cloud detection in high-resolution remote sensing images
JS缓动动画原理教学(超细节)
What kind of methods or functions can you view the laravel version of a project?
Introduce six open source protocols in detail (instructions for programmers)
Common text processing tools
Go语言学习笔记-结构体(Struct)