- 首先数据库备份: 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
--25T15::40.481+ building a list of collections to restore from /home/timeless/桌面/mongodump/blog dir
--25T15::40.491+ reading metadata for blog.post from /home/timeless/桌面/mongodump/blog/post.metadata.json
--25T15::40.499+ reading metadata for blog.user from /home/timeless/桌面/mongodump/blog/user.metadata.json
--25T15::40.510+ reading metadata for blog.meta from /home/timeless/桌面/mongodump/blog/meta.metadata.json
--25T15::40.555+ restoring blog.meta from /home/timeless/桌面/mongodump/blog/meta.bson
--25T15::40.568+ restoring indexes for collection blog.meta from metadata
--25T15::40.602+ restoring blog.post from /home/timeless/桌面/mongodump/blog/post.bson
--25T15::40.635+ restoring blog.user from /home/timeless/桌面/mongodump/blog/user.bson
--25T15::40.637+ finished restoring blog.meta ( documents)
--25T15::40.637+ restoring indexes for collection blog.post from metadata
--25T15::40.645+ reading metadata for blog.page from /home/timeless/桌面/mongodump/blog/page.metadata.json
--25T15::40.646+ reading metadata for blog.content from /home/timeless/桌面/mongodump/blog/content.metadata.json
--25T15::40.646+ finished restoring blog.post ( document)
--25T15::40.646+ reading metadata for blog.option from /home/timeless/桌面/mongodump/blog/option.metadata.json
--25T15::40.646+ restoring indexes for collection blog.user from metadata
--25T15::40.679+ finished restoring blog.user ( documents)
--25T15::40.680+ reading metadata for blog.counters from /home/timeless/桌面/mongodump/blog/counters.metadata.json
--25T15::40.680+ restoring blog.page from /home/timeless/桌面/mongodump/blog/page.bson
--25T15::40.707+ restoring blog.option from /home/timeless/桌面/mongodump/blog/option.bson
--25T15::40.741+ restoring blog.content from /home/timeless/桌面/mongodump/blog/content.bson
--25T15::40.785+ restoring blog.counters from /home/timeless/桌面/mongodump/blog/counters.bson
--25T15::40.836+ restoring indexes for collection blog.option from metadata
--25T15::40.837+ restoring indexes for collection blog.page from metadata
--25T15::40.838+ restoring indexes for collection blog.content from metadata
--25T15::40.847+ finished restoring blog.page ( document)
--25T15::40.847+ reading metadata for blog.system.users from /home/timeless/桌面/mongodump/blog/system.users.metadata.json
--25T15::40.847+ finished restoring blog.content ( document)
--25T15::40.848+ reading metadata for blog.webinfo from /home/timeless/桌面/mongodump/blog/webinfo.metadata.json
--25T15::40.848+ restoring indexes for collection blog.counters from metadata
--25T15::40.848+ finished restoring blog.option ( document)
--25T15::40.884+ restoring blog.webinfo from /home/timeless/桌面/mongodump/blog/webinfo.bson
--25T15::40.884+ restoring indexes for collection blog.webinfo from metadata
--25T15::40.916+ finished restoring blog.counters ( documents)
--25T15::40.916+ restoring blog.system.users from /home/timeless/桌面/mongodump/blog/system.users.bson
--25T15::40.916+ finished restoring blog.webinfo ( documents)
--25T15::40.917+ restoring indexes for collection blog.system.users from metadata
--25T15::40.928+ finished restoring blog.system.users ( documents)
--25T15::40.928+ done
该也可以指定要恢复的集合 -collection
注意的地方
- mongoimport 执行导入前,新数据库中没有 XX 表一样可以导入成功;
- mongoexport 和 mongoimport 所需的各个参数输入的先后顺序不严格;
- 对于副本集,mongoexport 的数据导出可以是副本集中的 primary 节点,也可以是 secondary 节点;
- 对于副本集,mongoimport 的数据导入必须是 primary 节点;
- 对于大数据量的导出,用 mongoexport 操作完全不会有生产问题;
- 对于大数据量的导入(超过 500 MB),用 mongoimport 操作的话会很容易将 primary 节点拖垮,因为这会导致全量同步导致主节点崩溃,所以要避免生产环境中大数据量的导入。正确的做法是先建立单点,导入,之后再建立副本集。