MongoDB备份、还原、导出、导入、克隆
2016-06-03 15:39
447 查看
数据库备份 – mongodump
备份本地所有MongoDB数据库:
mongodump -h 127.0.0.1 –port 27017 -o /root/db/alldb
备份远程指定数据库:
mongodump -h 192.168.1.233 –port 27018 -d yourdb -o /root/db/yourdb
数据库还原-mongorestore
恢复所有数据库到MongoDB中:
mongorestore -h 127.0.0.1 –port 27018 /root/db/alldb
还原指定数据库:
mongorestore –port 27017 -d yourdb /root/db/yourdb
导出集合数据-mongoexport
导出数据库中指定集合的数据:
mongoexport -h 192.168.1.233 –port 27018 -d yourdb -c yourcoll -o /root/yourcoll.json
导出集合中指定字段的数据,导出的文件格式为csv:
mongoexport -d yourdb -c test -f “id,name,score” –csv -o /root/test.csv
根据条件导出数据:
mongoexport -d yourdb -c yourcoll -q ‘{score:{$gt:80}}’ -o /root/yourcoll-bk.json
集合数据导入-mongoimport
还原导出的集合数据:
mongoimport -d yourdb -c yourcoll –file /root/yourcoll.json
导入集合数据,插入或更新现有的数据:
mongoimport -d test -c yourcoll –file /root/yourcoll.json –upsert
MongoDB数据库克隆
命令格式:
db.copyDatabase(fromdb, todb, fromhost, username, password)
从远程MongoDB中复制指定数据库到本地:
db.copyDatabase(“yii2”, “lyii2”, “192.168.0.69”)
集合的克隆
命令格式:
db.runCommand({ cloneCollection: “”, from: “”, query: { } });
从远程MongoDB中克隆指定的集合到本地数据库中:
db.runCommand({ cloneCollection: “test.user”, from: “192.168.0.69”, query:{} })
备份本地所有MongoDB数据库:
mongodump -h 127.0.0.1 –port 27017 -o /root/db/alldb
备份远程指定数据库:
mongodump -h 192.168.1.233 –port 27018 -d yourdb -o /root/db/yourdb
数据库还原-mongorestore
恢复所有数据库到MongoDB中:
mongorestore -h 127.0.0.1 –port 27018 /root/db/alldb
还原指定数据库:
mongorestore –port 27017 -d yourdb /root/db/yourdb
导出集合数据-mongoexport
导出数据库中指定集合的数据:
mongoexport -h 192.168.1.233 –port 27018 -d yourdb -c yourcoll -o /root/yourcoll.json
导出集合中指定字段的数据,导出的文件格式为csv:
mongoexport -d yourdb -c test -f “id,name,score” –csv -o /root/test.csv
根据条件导出数据:
mongoexport -d yourdb -c yourcoll -q ‘{score:{$gt:80}}’ -o /root/yourcoll-bk.json
集合数据导入-mongoimport
还原导出的集合数据:
mongoimport -d yourdb -c yourcoll –file /root/yourcoll.json
导入集合数据,插入或更新现有的数据:
mongoimport -d test -c yourcoll –file /root/yourcoll.json –upsert
MongoDB数据库克隆
命令格式:
db.copyDatabase(fromdb, todb, fromhost, username, password)
从远程MongoDB中复制指定数据库到本地:
db.copyDatabase(“yii2”, “lyii2”, “192.168.0.69”)
集合的克隆
命令格式:
db.runCommand({ cloneCollection: “”, from: “”, query: { } });
从远程MongoDB中克隆指定的集合到本地数据库中:
db.runCommand({ cloneCollection: “test.user”, from: “192.168.0.69”, query:{} })
相关文章推荐
- (转)MongoDB分片实战 集群搭建
- 8天学通MongoDB——第八天 驱动实践
- 8天学通MongoDB——第七天 运维技术
- 8天学通MongoDB——第六天 分片技术
- 8天学通MongoDB——第五天 主从复制
- 8天学通MongoDB——第四天 索引操作
- 8天学通MongoDB——第三天 细说高级操作
- 8天学通MongoDB——第二天 细说增删查改
- 8天学通MongoDB——第一天 基础入门
- MongoDB -- 复制集搭建
- mongoDB--GridFS简介
- mongoDB——java操作mongoDB实现文件上传下载
- MongoDB 系统介绍
- java 中 mongodb的各种操作 模糊查询 精确查询 等等
- MongoDB Java使用指南
- MongoDB 的 GridFS 详细分析
- Mongodb 安装过程与服务无法启动100、48问题
- Linux下安装配置MongoDB 3.0.x 版本数据库
- MongoDB学习笔记
- MongoDB快速入门笔记(八)之MongoDB的java驱动操作代码讲解