mongodb 2.4升级至3.2

来源:互联网 发布:美女写真软件 编辑:程序博客网 时间:2024/05/22 00:18
--mongorestore升级过程中如果遇到如下错误,需要删除admin数据库[root@beta tmp]# mongorestore -h 127.0.0.1 --oplogReplay --port 27015 --dir /tmp/dump20160411/2016-04-11T16:36:14.026+0800    building a list of dbs and collections to restore from /tmp/dump20160411 dir2016-04-11T16:36:14.039+0800    assuming users in the dump directory are from <= 2.4 (auth version 1)2016-04-11T16:36:14.040+0800    Failed: the users and roles collections in the dump have an incompatible auth version with target server: cannot restore users of auth version 1 to a server of auth version 5--删除admin数据库,可以正确还原数据库从2.4至3.2[root@beta dump20160411]# rm -rf admin/[root@beta dump20160411]# mongorestore -h 127.0.0.1 --oplogReplay --port 27015 --dir /tmp/dump20160411/--同样的数据在3.2版本中压缩比比在2.4中多10倍以上[root@beta mongodb]# du -sh *35G     27014 (2.4版本)3.1G    27015 (3.2版本) --从mongo2.6开始,mongo限制索引的大小不能超过1MB(之前的版本是索引无效),否则其会报错 Failed: messaging.messageHistory: error creating indexes for messaging.messageHistory: createIndex error: WiredTigerIndex::insert: key too large to index, failing  2001 { : "环境后台出现错误: org.springframework.validation.BeanPropertyBindingResult: 4 errors Field error in object 'flightWhiteSearchRequirment' on fi..." }The total size of an index entry, which can include structural overhead depending on the BSON type, must be less than 1024 bytes.MongoDB will not create an index on a collection if the index entry for an existing document exceeds the index key limit. Previous versions of MongoDB would create the index but not index such documents.--查看索引中字段最长的 _idvar max_id=null;var max_title_length=1;db.messageHistory.find().forEach(function(doc){if(doc.title.length>max_title_length) {max_title_length=doc.title.length; max_id=doc._id;} });--此时有两种解决方法,一是在 mongorestore 中加上 noIndexRestore 不恢复索引,注意此时意味着恢复完成后要手动对每一个数据库的每一个文档重建索引一是在 编辑 collection.metadata.json 文件,把与那个索引有关的语句删除掉-- 单库迁移升级(场景:原来的数据库在mongo 2.4版本中(端口27016),现在现把它迁移到3.2版本中(端口27017),在迁移升级过程中也有数据的变更)--假设要迁移的数据库为test--先初始化一部分数据use testfor(var i=1;i<10000;i++) {db.user.insert({"name":"rudy"+Math.round(Math.random()*1000),"password":"123"+Math.round(Math.random()*100)}) }--备份之前,查看此时的时间戳Timestamp(Date.parse(new Date())/1000,1);Timestamp(1463128620, 1)--备份出数据mongodump -o /tmp/backup/dump`date +%Y%m%d` -d test --port 27016--接着做数据的变更(注意增删改等操作也可能在其它数据中进行)for(var i=1;i<10;i++) {var rand=Math.round(Math.random()*1000);if(i%2==0)  {db.user.remove({"name":"rudy"+rand})}}--将备份出的数据导入到3.2版本中mongorestore -h 127.0.0.1  --port 27017  --dir /tmp/backup/dump20160516/ --依据导出前的时间戳,导出与数据库test有关的oplog(2.4版本),注意导出时的查询条件,以及对"."的双转义mongodump -h 127.0.0.1 --port 27016 -d local -c "oplog.rs" -q '{"ns":/^test\\./,ts:{$gte:Timestamp(1463128620, 1)}}' -o /tmp/backup/oplog01/--新起一个实例把oplog导出到新实例中(如果需要循环操作,从第二次起,导入前把oplog.rs清空)mongorestore -h 127.0.0.1 --port 27019 -d local -c "oplog.rs" --dir /tmp/backup/oplog01/local/oplog.rs.bson--把oplog导入到3.2版本的数据库中,以实现滚动的数据应用mongooplog --host=127.0.0.1 --port 27017 --from 127.0.0.1:27019 --循环从2.4版本导出oplog,在3.2版本中应用oplog的过程,直到应用的连接连接到3.2版本为止(另注:由于从3.2版本开始mongooplog已经被废弃,要使用mongorestore进行oplog操作)--由于mongorestore需要的是oplog.bson,故需要对备份出来的oplog重命名cp /tmp/backup/oplog01/local/oplog.rs.bson /tmp/backup/oplog01/local/oplog.bson--指定oplogReplay参数对oplog进行重放(注意此时不需要 -d local -c "oplog.rs" 参数,因为其只会把oplog导入到local数据库中,但不对oplog进行重放)mongorestore -h 127.0.0.1 --port 27017 --oplogReplay --dir /tmp/backup/oplog01/local/--循环从2.4版本导出oplog,在3.2版本中应用oplog的过程,直到应用的连接连接到3.2版本为止

0 0