MongoDB 数据导入与导出工具的使用

打印 上一主题 下一主题

主题 912|帖子 912|积分 2736

  1. 作者介绍:老苏,10余年DBA工作运维经验,擅长Oracle、MySQL、PG数据库运维(如安装迁移,性能优化、故障应急处理等)
  2. 公众号:老苏畅谈运维
  3. 欢迎关注本人公众号,更多精彩与您分享。
复制代码
MongoDB 官方提供的以下两组导入导出工具:


  • mongodump 与 mongorestore
  • mongoexport 与 mongoimport
接下来我说一说 MongoDB 官方导入/导出工具的常用使用命令。
一、mongodump 与 mongorestore

mongodump 与 mongorestore 是对整个数据举行导出或导入,操纵数据格式是 BSON 格式,对大量数据举行多 dump 和 restore 操纵时具备更高效率。
mongodump 导出命令用法:
mongodump --host <mongos_ip>:27017 -u -p --authenticationDatabase=admin --db= -o /data/dump_test
mongorestore 导入命令用法:
mongorestore --host <mongos_ip>:27017 -u -p --authenticationDatabase=admin --dir=/data/dump_test
mongodump常用参数:
$ mongodump --help
参数分析:
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-o:指明到要导出的文件名
-q:指明导出数据的过滤条件
-j, --numParallelCollections= number of collections to dump in parallel (4 by default) //指定并行
–oplog 备份的同时备份oplog
mongodump/mongorestore一些常用的使用方式:
1.全库备份
mongodump -uroot -p123456 --port 27017 --authenticationDatabase admin -j 2 -o /media/backup/full
2.备份dbtest库
mongodump -uroot -p123456 --port 27017 --authenticationDatabase admin -j 2 -d dbtest -o /media/backup/
3.备份单表
mongodump -uroot -p123456 --port 27017 --authenticationDatabase admin -j 2 -d dbtest -c students -o /media/backup/
4.压缩备份
mongodump -uroot -p123456 --port 27017 --authenticationDatabase admin -j 2 -d dbtest -o /media/backup/full --gzip
5.规复dbtest库到dbtest1
mongorestore -uroot -p123456 --port 27017 --authenticationDatabase admin -d dbtest1 /media/backup/full/dbtest
6.规复dbtest库下的students聚集(从指定压缩的文件规复)
mongorestore -uroot -p123456 --port 27017 --authenticationDatabase admin -d dbtest -c students --gzip /media/backup/dbtest/students.bson.gz
二、mongoexport 与 mongoimport

mongoexport 与 mongoimport 是对单个聚集举行导出导入,通常使用 ,操纵数据格式是 JSON 格式,数据导出后具备较高的可读性。
mongoexport 导出命令:
mongoexport --host <mongos_ip>:27017 -u -p --authenticationDatabase=admin --db= --collection=test -o /data/export_test.json
mongoimport 导入命令:
mongoimport --host <mongos_ip>:27017 -u -p --authenticationDatabase=admin --db= --collection=test --file=/data/export_test.json
$ mongoexport --help
参数分析:
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-f:指明要导出那些列
-o:指明到要导出的文件名
-q:指明导出数据的过滤条件
–authenticationDatabase admin
$ mongoimport --help
参数分析:
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-f:指明要导入那些列
-j, --numInsertionWorkers= number of insert operations to run concurrently (defaults to 1)//指定并行
若单实例节点存在增量数据,需要通过单实例的 oplog 来规复数据到本地。
$ ./mongorestore -h localhost:27017 --dir /media/backup/full
规复指定时间的数据到本地。
$ ./mongorestore -h localhost:27017 --oplogReplay --oplogLimit “1634895600” /media/backup/full
mongoexport/mongoimport一些常用的使用方式:
1.单表备份至json格式
mongoexport -uadmin -p123456 --port 27017 --authenticationDatabase admin -d admin -c b -o /media/backup/b.json
2.导出指定列生存csv格式
mongoexport -uadmin -p123456 --port 27017 --authenticationDatabase admin -d admin -c b --type=csv -f id -o /media/backup/b.csv
3.规复json格式表数据到 b1
mongoimport -uadmin -p123456 --port 27017 --authenticationDatabase admin -d admin -c b1 /media/backup/b.json
–加并行
mongoimport -uadmin -p123456 --port 27017 -j 4 --authenticationDatabase admin -d admin -c b1 /media/backup/b.json
4.规复csv格式的文件到 test库下表test2
要导入CSV格式文件中的内容,则需要通过–type参数指定导入格式:
注意:–headerline:指明第一行是列名,不需要导入。
(1)csv格式的文件头行,有列名字,要加–headerline
mongoimport -uadmin -p123456 --port 27017 --authenticationDatabase admin -d test -c test2 --type=csv --headerline --file /media/backup/b.csv
(2)csv格式的文件头行,没有列名字,不需要加–headerline
mongoimport -uadmin -p123456 --port 27017 --authenticationDatabase admin -d test -c test3 --type=csv -f id --file /media/backup/b.csv
–查看规复后的数据:
rs1 [direct: primary] admin> use test
switched to db test
rs1 [direct: primary] test> show tables;
test2
test3


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

尚未崩坏

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表