MongoDB备份与还原

MongoDB备份与还原

今天迁移MongoDB数据库出现了很奇怪的问题,使用的方法以下:html

备份方法:
mongodump -h dbhost -d dbname -o dbdirectory 
还原方法:
mongorestore -h <hostname><:port> -d dbname <path> 

备份正常,但还原出现报错:mongodb

Failed: blog.posts: error restoring from w/posts.bson: reading bson input: invalid BSONSize: 1802661751 bytesshell

网上查找资料说加上参数--batchSize=10,但仍是不行。数据库

最后在MongoDB官方网站找到了解决方法:备份时使用导出归档文件形式,还原也使用归档文件形式。post

备份
mongodump --archive=test.20150715.archive --db test 
还原
mongorestore --archive=test.20150715.archive --db test 

注意以上方法适合MongoDB 3.2版本以上。网站