mongodb数据库操作--备份 还原 导出 导入

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mongodb数据库操作--备份 还原 导出 导入相关的知识,希望对你有一定的参考价值。

  • 首先数据库备份: mongodump -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -o 文件存在路径 
mongodump -h 127.0.0.1 -u admin -p xxx  -d blog -o /home/timeless/桌面/mongodump --authenticationDatabase admin

注意  --authenticationDatabase  参数制定认证数据库   否则会提示错误:

Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed.   //提示认证失败

也可以指定导出的集合  -collection 

  • 还原数据库实例
$ mongorestore -h XXX -u admin -p XXX -d blog  /home/timeless/桌面/mongodump/blog --authenticationDatabase admin   
2016-09-25T15:09:40.481+0800    building a list of collections to restore from /home/timeless/桌面/mongodump/blog dir
2016-09-25T15:09:40.491+0800    reading metadata for blog.post from /home/timeless/桌面/mongodump/blog/post.metadata.json
2016-09-25T15:09:40.499+0800    reading metadata for blog.user from /home/timeless/桌面/mongodump/blog/user.metadata.json
2016-09-25T15:09:40.510+0800    reading metadata for blog.meta from /home/timeless/桌面/mongodump/blog/meta.metadata.json
2016-09-25T15:09:40.555+0800    restoring blog.meta from /home/timeless/桌面/mongodump/blog/meta.bson
2016-09-25T15:09:40.568+0800    restoring indexes for collection blog.meta from metadata
2016-09-25T15:09:40.602+0800    restoring blog.post from /home/timeless/桌面/mongodump/blog/post.bson
2016-09-25T15:09:40.635+0800    restoring blog.user from /home/timeless/桌面/mongodump/blog/user.bson
2016-09-25T15:09:40.637+0800    finished restoring blog.meta (3 documents)
2016-09-25T15:09:40.637+0800    restoring indexes for collection blog.post from metadata
2016-09-25T15:09:40.645+0800    reading metadata for blog.page from /home/timeless/桌面/mongodump/blog/page.metadata.json
2016-09-25T15:09:40.646+0800    reading metadata for blog.content from /home/timeless/桌面/mongodump/blog/content.metadata.json
2016-09-25T15:09:40.646+0800    finished restoring blog.post (1 document)
2016-09-25T15:09:40.646+0800    reading metadata for blog.option from /home/timeless/桌面/mongodump/blog/option.metadata.json
2016-09-25T15:09:40.646+0800    restoring indexes for collection blog.user from metadata
2016-09-25T15:09:40.679+0800    finished restoring blog.user (2 documents)
2016-09-25T15:09:40.680+0800    reading metadata for blog.counters from /home/timeless/桌面/mongodump/blog/counters.metadata.json
2016-09-25T15:09:40.680+0800    restoring blog.page from /home/timeless/桌面/mongodump/blog/page.bson
2016-09-25T15:09:40.707+0800    restoring blog.option from /home/timeless/桌面/mongodump/blog/option.bson
2016-09-25T15:09:40.741+0800    restoring blog.content from /home/timeless/桌面/mongodump/blog/content.bson
2016-09-25T15:09:40.785+0800    restoring blog.counters from /home/timeless/桌面/mongodump/blog/counters.bson
2016-09-25T15:09:40.836+0800    restoring indexes for collection blog.option from metadata
2016-09-25T15:09:40.837+0800    restoring indexes for collection blog.page from metadata
2016-09-25T15:09:40.838+0800    restoring indexes for collection blog.content from metadata
2016-09-25T15:09:40.847+0800    finished restoring blog.page (1 document)
2016-09-25T15:09:40.847+0800    reading metadata for blog.system.users from /home/timeless/桌面/mongodump/blog/system.users.metadata.json
2016-09-25T15:09:40.847+0800    finished restoring blog.content (1 document)
2016-09-25T15:09:40.848+0800    reading metadata for blog.webinfo from /home/timeless/桌面/mongodump/blog/webinfo.metadata.json
2016-09-25T15:09:40.848+0800    restoring indexes for collection blog.counters from metadata
2016-09-25T15:09:40.848+0800    finished restoring blog.option (1 document)
2016-09-25T15:09:40.884+0800    restoring blog.webinfo from /home/timeless/桌面/mongodump/blog/webinfo.bson
2016-09-25T15:09:40.884+0800    restoring indexes for collection blog.webinfo from metadata
2016-09-25T15:09:40.916+0800    finished restoring blog.counters (4 documents)
2016-09-25T15:09:40.916+0800    restoring blog.system.users from /home/timeless/桌面/mongodump/blog/system.users.bson
2016-09-25T15:09:40.916+0800    finished restoring blog.webinfo (0 documents)
2016-09-25T15:09:40.917+0800    restoring indexes for collection blog.system.users from metadata
2016-09-25T15:09:40.928+0800    finished restoring blog.system.users (0 documents)
2016-09-25T15:09:40.928+0800    done

该也可以指定要恢复的集合  -collection

 

注意的地方 

  • mongoimport 执行导入前,新数据库中没有 XX 表一样可以导入成功;
  • mongoexport 和 mongoimport 所需的各个参数输入的先后顺序不严格;
  • 对于副本集,mongoexport 的数据导出可以是副本集中的 primary 节点,也可以是 secondary 节点;
  • 对于副本集,mongoimport 的数据导入必须是 primary 节点;
  • 对于大数据量的导出,用 mongoexport 操作完全不会有生产问题;
  • 对于大数据量的导入(超过 500 MB),用 mongoimport 操作的话会很容易将 primary 节点拖垮,因为这会导致全量同步导致主节点崩溃,所以要避免生产环境中大数据量的导入。正确的做法是先建立单点,导入,之后再建立副本集。

 

以上是关于mongodb数据库操作--备份 还原 导出 导入的主要内容,如果未能解决你的问题,请参考以下文章

mongodb 备份还原导入导出

mongodb 备份 还原 导出 导入

Mongodb备份还原常用命令

Mongodb备份还原常用命令

mongodb 数据备份,还原笔记

入门系列之在Ubuntu 14.04上备份,还原和迁移MongoDB数据库