当前位置:首页 > 工具 > 正文内容

MongoDB 实验——数据备份和恢复

IM2年前 (2024-08-25)工具3


第1关:数据备份

mongodump 备份工具

mongodump 的参数与 mongoexport(数据导出)的参数基本一致:

00c9dee236bb50dff1c23f57e5582dce_6df8ce288f015d6e5b3f295235578541.png

使用 mongodump 备份数据

备份工具同导入导出工具类似,都是在命令行进行操作,无需进入客户端。


编程测试代码:


mkdir /opt/mongodb


mkdir /opt/mongodb_1


mkdir /opt/mongodb_2


mkdir /opt/collection_1


mkdir /opt/collection_2


1、导入集合

mongoimport -d test2 -c student -file /home/example/student.csv --type csv


mongoimport -d test1 -c person --type json --file /home/example/person.json


2、全库备份

(如果数据库未设置用户和密码,可以省略 -uroot -proot 参数)


将所有数据库备份到 /opt/mongodb 目录下


mongodump -h 127.0.0.1:27017 --authenticationDatabase admin  -o /opt/mongodb

4312826bcea57b3635830c19c85f0d0d_d2d14f8c254ddf7435088bae8c413206.png

3、单个数据库备份:

将 test1 数据库备份到 /opt/mongodb_1 目录下


mongodump -h 127.0.0.1:27017 --authenticationDatabase admin  -d test1 -o /opt/mongodb_1

a90ddb5b2f175d8ef546f3dc3ac91c46_f58c87389c9fbb2461681665274abe1b.png

4、集合备份:

将 person 集合备份到 /opt/collection_1 目录下


mongodump -h 127.0.0.1:27017  --authenticationDatabase admin  -d test1 -c person -o /opt/collection_1

f00f0d42d7d2851292222e2165a8c850_5143a8b7a8468ef29c4412a4a955a7e8.png

5、压缩备份集合:

将 student 集合压缩备份到 /opt/collection_2 目录下


mongodump -h 127.0.0.1:27017 --authenticationDatabase admin  -d test2 -c student -o /opt/collection_2 --gzip

f09a26fb5971d478af74b6ca88200aea_c34031b657d349a5c10583785c229c3f.png

6、压缩备份库:

将 test2 数据库压缩备份到 /opt/mongodb_2 目录下


mongodump -h 127.0.0.1:27017 --authenticationDatabase admin  -d test2 -o /opt/mongodb_2 --gzip

e83d6332c89aa7ef2e87fae9c37c394c_8e0bc05688cbbbb2e127d085212c8679.png

第2关:数据恢复

93657264c68104f0fbe023020a519769_bda4b9292f85554893452416e2a54429.png

1、全库备份中恢复单库

将 /opt/mongodb 目录下的数据恢复到 MongoDB 中;


mongorestore -h 127.0.0.1:27017 --authenticationDatabase admin --drop  /opt/mongodb

0ef7058745aed2fc32e6076ea36527d2_935bac4d98a0db339dd4b79b8f0d359c.png

2、恢复到数据库下

将 /opt/mongodb_1 目录下的数据恢复到 mytest1 数据库中


mongorestore -h 127.0.0.1:27017 --authenticationDatabase admin -d mytest1 /opt/mongodb_1/test1

76f0c538597765dc0cbb7cf83983ce4e_79f0bf665e54d58e8328c7a7f61028eb.png

3、恢复到数据库的集合下

将 /opt/collection_1 目录下的数据恢复到 mytest2 数据库的 person 集合中


mongorestore -h 127.0.0.1:27017 --authenticationDatabase admin -d mytest2 -c person /opt/collection_1/test1/person.bson

bc9ca8b00768d3419a276a01bc34ec3e_9821e30201582ec31fb8d99badacd108.png

4、--gzip 、--drop 参数实践恢复

将 /opt/collection_2 目录下的数据恢复到 mytest3 数据库的 student 集合中,并删除之前备份的表;


mongorestore -h 127.0.0.1:27017 --authenticationDatabase admin -d mytest3 -c student --gzip --drop /opt/collection_2/test2/student.bson.gz

0f0acc96e6046a71d5fbc9d87c53f331_f188c900287685e4aa433e524e36fc0d.png

5、--gzip 、--drop 参数实践恢复

将/opt/mongodb_2目录下的数据恢复到mytest4 的数据库中,并删除之前的备份的数据库


mongorestore -h 127.0.0.1:27017 --authenticationDatabase admin -d mytest4  --gzip --drop /opt/mongodb_2/test2/student.bson.gz

a4e3db15f440476f06cad63e96af1e20_5fe3a734862e00c58845b5c9a29b8175.png

扫描二维码推送至手机访问。

版权声明:本文由柒凌轩发布,如需转载请注明出处。

本文链接:http://x.zx.cdqlx.top/?id=23

分享给朋友:

“MongoDB 实验——数据备份和恢复” 的相关文章

6月保姆级知识付费平台搭建

6月保姆级知识付费平台搭建

源码简介:2024 最新彩虹晴天多功能系统源码 知识付费 虚拟商城 完美可用无需授权、国内外服务器皆可搭建、是不是备案域名也皆可部署、可以商业运营本系统有本人亲自修复更新后所发布出来,全网独家首发,且包含众多功能本程序并非适用于知识付费也可以做虚拟商城、实物商城、虚拟卡网等扩展性...

使用nginx让nps内网穿透的网站支持https

使用nginx让nps内网穿透的网站支持https

00.nps配置因为nginx需要监听80与443端口,而nps的http和https默认代理端口也是80与443端口,所以我们需要修改nps的配置进入nps的根目录,本人这里是/opt/nps/,但是很多人是/etc/nps进入conf文件夹,编辑nps.conf文件,找到以下代码块#HTTP(S...

Centos7安装MySQL8.0详细教程(压缩包安装方式)

Centos7安装MySQL8.0详细教程(压缩包安装方式)

下载MySQL下载地址: https://downloads.mysql.com/archives/community/这里需要知道自己的linux的glibc版本,不然安装的时候会提示glibc版本过低。查询方法如下:rpm -q glibc下载之后,将压缩包上传到服务器上,准备安装MySQL。三...

Linux下安装mysql8.0

Linux下安装mysql8.0

一、下载mysql8.0安装包1、在local创建mysql文件夹cd /usr/local mkdir mysql cd mysql2、使用wget下载mysql8.0的xz安装包wget https://dev.mysql.com/get/Downloa...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。