MongoDB数据库迁移

一、迁移整个库

1.mongodump(导出)

命令格式:mongodump -h host:port -d dbname -o D:\data\dump

 

2.mongorestore(导入)

命令格式:mongorestore -h host:port -d dbnameNew --dir D:\data\dump\dbname

 

注:如果是有权限认证,需要用户名密码的时候,需要加 --authenticationDatabase admin

命令格式:mongorestore -h host:port -d dbnameNew -u username -p pwd --authenticationDatabase admin --dir D:\data\dump\dbname

 

 

 

mongorestore出现的问题

   L2Calc.calcgsdataflash  createIndex error:  Index with name: _id_ already exists with different options

原因:执行mongorestore的时候,库中的collection生成时会默认加一个_id_的索引,且不是unique属性的,

   而你之前用mongodump导出时,calcgsdataflash这个collection的索引 _id_ 是unique属性的,就发生冲突了。

解决方法:加 --noIndexRestore 导入数据,索引等数据导完了再加。

  (mongorestore命令的详细参数,参见官方文档:https://docs.mongodb.com/manual/reference/program/mongorestore/)

   命令格式:mongorestore -h host:port -d dbnameNew -u username -p pwd --authenticationDatabase admin --noIndexRestore --dir D:\data\dump\dbname

 

二、迁移单个collection

命令格式:mongodump -h host:port -d dbname -c collectionname -o D:\data\dump

 

命令格式:mongorestore -h host:port -d dbnameNew -c collectionname --dir D:\data\dump\dbname\collectionname.bson

 

posted @ 2017-04-05 19:18  那一剑的風情  阅读(1840)  评论(0编辑  收藏  举报