文章目录
第1关:数据备份任务描述相关知识mongodump 备份工具使用 mongodump 备份数据
编程要求测试说明答案代码
第2关:数据恢复任务描述相关知识mongorestore 恢复工具使用 mongorestore 恢复数据
编程要求测试说明答案代码
第1关:数据备份
任务描述
本关任务:按照编程要求备份数据库。
相关知识
为了完成本关任务,你需要掌握: 1.掌握 mongodump 备份工具的参数含义; 2.如何使用 mongodump 备份数据。
mongodump 备份工具
mongodump 的参数与 mongoexport(数据导出)的参数基本一致:
参数参数说明-h指明数据库宿主机的IP-u指明数据库的用户名-p指明数据库的密码-d指明数据库的名字-c指明collection的名字-o指明到要导出的文件名-q指明导出数据的过滤条件–authenticationDatabase验证数据的名称–gzip备份时压缩–oploguse oplog for taking a point-in-time snapshot
使用 mongodump 备份数据
备份工具同导入导出工具类似,都是在命令行进行操作,无需进入客户端。
全库备份(如果数据库未设置用户和密码,可以省略 -uroot -proot 参数) mongodump -h 127.0.0.1:27300 -uroot -proot --authenticationDatabase admin -o /home/mongod
#备份本地27300端口中root用户的所有数据库到/home/mongod目录下
单个数据库备份 mongodump -h 127.0.0.1:27300 -uroot -proot --authenticationDatabase admin -d test -o /home/mongod/test
#备份本地27300端口中root用户的test数据库到/home/mongod/test目录下
集合备份 mongodump -h 127.0.0.1:27300 -uroot -proot --authenticationDatabase admin -d test -c haha -o /home/mongod/test/haha
#备份27300端口中root用户的test数据库的haha集合到/home/mongod/test/haha目录下
压缩备份库 mongodump -h 127.0.0.1:27300 -uroot -proot --authenticationDatabase admin -d test -o /home/mongod/test1 --gzip
#压缩备份本地27300端口中root用户的test数据库到/home/mongod/test1目录下
压缩备份集合 mongodump -h 127.0.0.1:27300 -uroot -proot --authenticationDatabase admin -d test -c haha -o /home/mongod/test1/haha --gzip
#压缩备份27300端口中root用户的test数据库的haha集合到/home/mongod/test1/haha目录下
编程要求
根据提示,在右侧命令行进行操作(以下均在默认端口为27017的客户端进行,无用户和密码;以下 /opt下的路径均 不存在,需要自己先行创建):
分别将 /home/example 目录下的 person.json 和 student.csv 导入到 MongoDB 的 test1 数据库的 person 集合、test2 数据库的 student 集合;将所有数据库备份到 /opt/mongodb 目录下;将 test1 数据库备份到 /opt/mongodb_1 目录下;将 person 集合备份到 /opt/collection_1 目录下;将 student 集合压缩备份到 /opt/collection_2 目录下;将 test2 数据库压缩备份到 /opt/mongodb_2 目录下。
测试说明
平台会对你编写的代码进行测试:
如果操作无误,会显示如 测试集1所示结果。
答案代码
mkdir /opt/mongodb
mkdir /opt/mongodb_1
mkdir /opt/mongodb_2
mkdir /opt/collection_1
mkdir /opt/collection_2
mongoimport -d test2 -c student --type csv --headerline --ignoreBlanks --file /home/example/student.csv
mongoimport -d test1 -c person --type json --file /home/example/person.json
mongodump -h 127.0.0.1:27017 --authenticationDatabase admin -o /opt/mongodb
mongodump -h 127.0.0.1:27017 --authenticationDatabase admin -d test1 -o /opt/mongodb_1
mongodump -h 127.0.0.1:27017 --authenticationDatabase admin -d test1 -c person -o /opt/collection_1
mongodump -h 127.0.0.1:27017 --authenticationDatabase admin -d test2 -c student -o /opt/collection_2 --gzip
mongodump -h 127.0.0.1:27017 --authenticationDatabase admin -d test2 -o /opt/mongodb_2 --gzip
第2关:数据恢复
任务描述
本关任务:按照编程要求恢复数据。
相关知识
为了完成本关任务,你需要掌握: 1.掌握 mongorestore 恢复工具的参数含义; 2.如何使用 mongorestore 恢复数据。
mongorestore 恢复工具
参数参数说明-h指明数据库宿主机的IP-u指明数据库的用户名-p指明数据库的密码-d指明数据库的名字-c指明collection的名字-o指明到要导出的文件名-q指明导出数据的过滤条件–authenticationDatabase验证数据的名称–gzip备份时压缩–oploguse oplog for taking a point-in-time snapshot–drop恢复的时候把之前的集合drop掉
使用 mongorestore 恢复数据
全库备份中恢复单库(基于之前的全库备份) mongorestore -h 127.0.0.1:27017 -uroot -proot --authenticationDatabase admin -d test --drop /home/mongod
#从/home/mongod目录下恢复全部数据库的数据到本地27300端口中root用户中(基于第一关的备份,下同)
恢复 test 库 mongorestore -h 127.0.0.1:27017 -uroot -proot --authenticationDatabase admin -d test /home/mongod/test
#从/home/mongod/test目录下恢复名为test的单个数据库的数据到本地27300端口中root用户中的test数据库
恢复 test 库下的 haha 集合 mongorestore -h 127.0.0.1:27017 -uroot -proot --authenticationDatabase admin -d test -c haha /home/mongod/test/haha/haha.bson
#从/home/mongod/test/haha目录下恢复集合的数据到本地27300端口中root用户的test数据库的haha集合中
–drop 参数实践恢复 # 恢复单库
mongorestore -h 127.0.0.1:27017 -uroot -proot --authenticationDatabase admin -d test --drop /home/mongod/test
# 恢复单表
mongorestore -h 127.0.0.1:27017 -uroot -proot --authenticationDatabase admin -d test -c vast --drop /home/mongod/test/haha/haha.bson
编程要求
根据提示,在右侧命令行进行操作,将第一关备份的数据按以下要求恢复(以下均在默认端口为27017的客户端进行,无用户和密码):
将 /opt/mongodb 目录下的数据恢复到 MongoDB 中;将 /opt/mongodb_1 目录下的数据恢复到 mytest1 数据库中;将 /opt/collection_1 目录下的数据恢复到 mytest2 数据库的 person 集合中;将 /opt/collection_2 目录下的数据恢复到 mytest3 数据库的 student 集合中,并删除之前备份的表;将 /opt/mongodb_2 目录下的数据恢复到 mytest4 的数据库中,并删除之前的备份的数据库。
测试说明
平台会对你编写的代码进行测试:
如果操作无误,会显示如 测试集1 所示结果。
答案代码
mongorestore -h 127.0.0.1:27017 --authenticationDatabase admin --drop /opt/mongodb
mongorestore -h 127.0.0.1:27017 --authenticationDatabase admin -d mytest1 /opt/mongodb_1/test1
mongorestore -h 127.0.0.1:27017 --authenticationDatabase admin -d mytest2 -c person /opt/collection_1/test1/person.bson
mongorestore -h 127.0.0.1:27017 --authenticationDatabase admin -d mytest3 -c student --gzip --drop /opt/collection_2/test2/student.bson.gz
mongorestore -h 127.0.0.1:27017 --authenticationDatabase admin -d mytest4 --gzip --drop /opt/mongodb_2/test2/student.bson.gz
文章链接
发表评论