赞
踩
本关任务:按照编程要求备份数据库。
为了完成本关任务,你需要掌握: 1.掌握 mongodump 备份工具的参数含义; 2.如何使用 mongodump 备份数据。
mongodump 的参数与 mongoexport(数据导出)的参数基本一致:
参数 | 参数说明 |
---|---|
-h | 指明数据库宿主机的IP |
-u | 指明数据库的用户名 |
-p | 指明数据库的密码 |
-d | 指明数据库的名字 |
-c | 指明collection的名字 |
-o | 指明到要导出的文件名 |
-q | 指明导出数据的过滤条件 |
–authenticationDatabase | 验证数据的名称 |
–gzip | 备份时压缩 |
–oplog | use oplog for taking a point-in-time snapshot |
备份工具同导入导出工具类似,都是在命令行进行操作,无需进入客户端。
全库备份(如果数据库未设置用户和密码,可以省略 -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下的路径均 不存在,需要自己先行创建):
平台会对你编写的代码进行测试:
如果操作无误,会显示如 测试集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
本关任务:按照编程要求恢复数据。
为了完成本关任务,你需要掌握: 1.掌握 mongorestore 恢复工具的参数含义; 2.如何使用 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 -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的客户端进行,无用户和密码):
平台会对你编写的代码进行测试:
如果操作无误,会显示如 测试集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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。