mongodb 2.4升级至3.2

本文涉及的产品
云数据库 MongoDB,通用型 2核4GB
简介: --mongorestore升级过程中如果遇到如下错误,需要删除admin数据库 [root@beta tmp]# mongorestore -h 127.0.0.1 --oplogReplay --port 27015 --dir /tmp/dump20160411/ 2016-04-


-- 单库迁移升级(场景:原来的数据库在mongo 2.4版本中(端口27016),现在现把它迁移到3.2版本中(端口27017),在迁移升级过程中也有数据的变更)

--假设要迁移的数据库为test
--先初始化一部分数据
use test
for(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版本为止



--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 dir
2016-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.


--查看索引中字段最长的 _id
var 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 文件,把与那个索引有关的语句删除掉








相关实践学习
MongoDB数据库入门
MongoDB数据库入门实验。
快速掌握 MongoDB 数据库
本课程主要讲解MongoDB数据库的基本知识,包括MongoDB数据库的安装、配置、服务的启动、数据的CRUD操作函数使用、MongoDB索引的使用(唯一索引、地理索引、过期索引、全文索引等)、MapReduce操作实现、用户管理、Java对MongoDB的操作支持(基于2.x驱动与3.x驱动的完全讲解)。 通过学习此课程,读者将具备MongoDB数据库的开发能力,并且能够使用MongoDB进行项目开发。 &nbsp; 相关的阿里云产品:云数据库 MongoDB版 云数据库MongoDB版支持ReplicaSet和Sharding两种部署架构,具备安全审计,时间点备份等多项企业能力。在互联网、物联网、游戏、金融等领域被广泛采用。 云数据库MongoDB版(ApsaraDB for MongoDB)完全兼容MongoDB协议,基于飞天分布式系统和高可靠存储引擎,提供多节点高可用架构、弹性扩容、容灾、备份回滚、性能优化等解决方案。 产品详情: https://www.aliyun.com/product/mongodb
目录
相关文章
|
存储 JSON NoSQL
暂缓MongoDB 4.4.2 、4.4.3、 4.4.4版本升级: 存在严重Bug
暂缓MongoDB 4.4.2 、4.4.3、 4.4.4版本升级: 存在严重Bug
227 0
暂缓MongoDB 4.4.2 、4.4.3、 4.4.4版本升级: 存在严重Bug