南宁建设网站,网站做第三方支付,移动应用开发公司网站模板,企业网站建设用标语在实际的应用场景中#xff0c;经常需要对业务数据进行备份以做容灾准备#xff0c; MongoDB 提供了备份和恢复的功能#xff0c;分别是 MongoDB 下的
一、数据备份 任务描述 本关任务#xff1a;按照编程要求备份数据库。 相关知识 为了完成本关任务#xff0c;你需要掌…在实际的应用场景中经常需要对业务数据进行备份以做容灾准备 MongoDB 提供了备份和恢复的功能分别是 MongoDB 下的
一、数据备份 任务描述 本关任务按照编程要求备份数据库。 相关知识 为了完成本关任务你需要掌握 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.创建以上目录 rootevassh-13661150:~# mkdir /opt/mongodb
rootevassh-13661150:~# mkdir /opt/mongodb_1
rootevassh-13661150:~# mkdir /opt/mongodb_2
rootevassh-13661150:~# mkdir /opt/collection_1
rootevassh-13661150:~# mkdir /opt/collection_2 2. 分别将 /home/example 目录下的 person.json 和 student.csv 导入到 MongoDB 的 test1 数据库的 person 集合、test2 数据库的 student 集合 rootevassh-13661150:~# mongoimport -d test1 -c person --type json --file /home/example/person.json
2022-12-18T15:32:43.0370000 connected to: localhost
2022-12-18T15:32:43.0520000 imported 8 documents
rootevassh-13661150:~# mongoimport -d test2 -c student --type csv --headerline --ignoreBlanks --file /home/example/student.csv
2022-12-18T15:33:36.4030000 connected to: localhost
2022-12-18T15:33:36.4160000 imported 8 documents mongoimport -d test1 -c person --type json --file /home/example/person.json mongoimport -d test2 -c student --type csv --headerline --ignoreBlanks --file /home/example/student.csv 3.其他五步按顺序 rootevassh-13661150:~# mongodump -h 127.0.0.1:27017 --authenticationDatabase admin -o /opt/mongodb
2022-12-18T15:35:24.5210000 writing admin.system.version to
2022-12-18T15:35:24.5220000 done dumping admin.system.version (1 document)
2022-12-18T15:35:24.5220000 writing test1.person to
2022-12-18T15:35:24.5220000 writing test2.student to
2022-12-18T15:35:24.5230000 done dumping test1.person (8 documents)
2022-12-18T15:35:24.5230000 done dumping test2.student (8 documents)rootevassh-13661150:~# mongodump -h 127.0.0.1:27017 --authenticationDatabase admin -d test1 -o /opt/mongodb_1
2022-12-18T15:36:20.9890000 writing test1.person to
2022-12-18T15:36:20.9890000 done dumping test1.person (8 documents)rootevassh-13661150:~# mongodump -h 127.0.0.1:27017 --authenticationDatabase admin -d test1 -c person -o /opt/collection_1
2022-12-18T15:37:27.5520000 writing test1.person to
2022-12-18T15:37:27.5530000 done dumping test1.person (8 documents)rootevassh-13661150:~# mongodump -h 127.0.0.1:27017 --authenticationDatabase admin -d test2 -c student -o /opt/collection_2 --gzip
2022-12-18T15:38:16.9750000 writing test2.student to
2022-12-18T15:38:16.9770000 done dumping test2.student (8 documents)rootevassh-13661150:~# mongodump -h 127.0.0.1:27017 --authenticationDatabase admin -d test2 -o /opt/mongodb_2 --gzip
2022-12-18T15:38:52.3900000 writing test2.student to
2022-12-18T15:38:52.3910000 done dumping test2.student (8 documents) 二、数据恢复 任务描述 本关任务按照编程要求恢复数据。 相关知识 为了完成本关任务你需要掌握 1.掌握 mongorestore 恢复工具的参数含义 2.如何使用 mongorestore 恢复数据。 mongorestore 恢复工具
参数 参数说明
-h 指明数据库宿主机的IP
-u 指明数据库的用户名
-p 指明数据库的密码
-d 指明数据库的名字
-c 指明collection的名字
-o 指明到要导出的文件名
-q 指明导出数据的过滤条件
--authenticationDatabase 验证数据的名称
--gzip 备份时压缩
--oplog use 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 的数据库中并删除之前的备份的数据库。 rootevassh-13661150:~# mongorestore -h 127.0.0.1:27017 --authenticationDatabase admin --drop /opt/mongodb
2022-12-18T15:48:05.7200000 preparing collections to restore from
2022-12-18T15:48:05.7880000 reading metadata for test1.person from /opt/mongodb/test1/person.metadata.json
2022-12-18T15:48:05.7930000 reading metadata for test2.student from /opt/mongodb/test2/student.metadata.json
2022-12-18T15:48:05.8150000 restoring test1.person from /opt/mongodb/test1/person.bson
2022-12-18T15:48:05.8150000 restoring test2.student from /opt/mongodb/test2/student.bson
2022-12-18T15:48:05.8180000 no indexes to restore
2022-12-18T15:48:05.8180000 finished restoring test1.person (8 documents)
2022-12-18T15:48:05.8180000 no indexes to restore
2022-12-18T15:48:05.8180000 finished restoring test2.student (8 documents)
2022-12-18T15:48:05.8180000 donerootevassh-13661150:~# mongorestore -h 127.0.0.1:27017 --authenticationDatabase admin -d mytest1 /opt/mongodb_1/test1
2022-12-18T15:48:36.6930000 the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
2022-12-18T15:48:36.6930000 building a list of collections to restore from /opt/mongodb_1/test1 dir
2022-12-18T15:48:36.6940000 reading metadata for mytest1.person from /opt/mongodb_1/test1/person.metadata.json
2022-12-18T15:48:36.7060000 restoring mytest1.person from /opt/mongodb_1/test1/person.bson
2022-12-18T15:48:36.7080000 no indexes to restore
2022-12-18T15:48:36.7080000 finished restoring mytest1.person (8 documents)
2022-12-18T15:48:36.7080000 donerootevassh-13661150:~# mongorestore -h 127.0.0.1:27017 --authenticationDatabase admin -d mytest2 -c person /opt/collection_1/test1/person.bson
2022-12-18T15:48:45.0650000 checking for collection data in /opt/collection_1/test1/person.bson
2022-12-18T15:48:45.0650000 reading metadata for mytest2.person from /opt/collection_1/test1/person.metadata.json
2022-12-18T15:48:45.0770000 restoring mytest2.person from /opt/collection_1/test1/person.bson
2022-12-18T15:48:45.1400000 no indexes to restore
2022-12-18T15:48:45.1400000 finished restoring mytest2.person (8 documents)
2022-12-18T15:48:45.1400000 donerootevassh-13661150:~# mongorestore -h 127.0.0.1:27017 --authenticationDatabase admin -d mytest3 -c student --gzip --drop /opt/collection_2/test2/student.bson.gz
2022-12-18T15:48:52.8300000 checking for collection data in /opt/collection_2/test2/student.bson.gz
2022-12-18T15:48:52.8300000 reading metadata for mytest3.student from /opt/collection_2/test2/student.metadata.json.gz
2022-12-18T15:48:52.8430000 restoring mytest3.student from /opt/collection_2/test2/student.bson.gz
2022-12-18T15:48:52.9060000 no indexes to restore
2022-12-18T15:48:52.9070000 finished restoring mytest3.student (8 documents)
2022-12-18T15:48:52.9070000 donerootevassh-13661150:~# mongorestore -h 127.0.0.1:27017 --authenticationDatabase admin -d mytest4 --gzip --drop /opt/mongodb_2/test2/student.bson.gz
2022-12-18T15:49:00.8100000 checking for collection data in /opt/mongodb_2/test2/student.bson.gz
2022-12-18T15:49:00.8100000 reading metadata for mytest4.student from /opt/mongodb_2/test2/student.metadata.json.gz
2022-12-18T15:49:00.8220000 restoring mytest4.student from /opt/mongodb_2/test2/student.bson.gz
2022-12-18T15:49:00.8850000 no indexes to restore
2022-12-18T15:49:00.8850000 finished restoring mytest4.student (8 documents)
2022-12-18T15:49:00.8850000 done