mongodb 2.4升级至3.2

本文档详细介绍了如何将MongoDB从2.4版本升级到3.2版本,包括在升级过程中遇到的问题及解决方案,如用户角色不兼容、索引大小限制和oplog的应用。通过删除admin数据库、手动处理索引和oplog重放,成功完成了数据库的迁移和升级。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


--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 文件,把与那个索引有关的语句删除掉



-- 单库迁移升级(场景:原来的数据库在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版本为止






引用\[1\]:tar xf mongodb-linux-x86_64-4.0.28.tgz -C /usr/local/tools 重命名文件 mv mongodb-linux-x86_64-4.0.28 mongodb 停止服务 。 引用\[2\]:1,mongodb2.4版本的下载 git clone https://github.com/mongodb/mongo.git 安装相关依赖: sudo yum install -y mongodb-org-2.6.7 mongodb-org-server-2.6.7 mongodb-org-shell-2.6.7 mongodb-org-mongos-2.6.7 mongodb-org-tools-2.6.7 。 引用\[3\]:3.1 升级过程 升级步骤 1,停掉原来的mongo; 2,将Mongo2.6版本数据全备份; 3,将3.2版本bin替换原来的bin文件来启动; 4,# /usr/local/src/mongo3.2/bin/mongod --dbpath /usr/local/src/db/mongo --logpath /usr/local/src/log/mongo.log --logappend --fork 5,进入mongo,查看数据库 show dbs; 查看数据集合:show collections 查看集合数据:db.testData.find() 。 问题: mongodb全部版本的下载 回答: 您可以通过以下方式下载不同版本的mongodb: - 对于4.0.28版本,您可以使用以下命令进行下载和安装: tar xf mongodb-linux-x86_64-4.0.28.tgz -C /usr/local/tools mv mongodb-linux-x86_64-4.0.28 mongodb - 对于2.4版本,您可以使用以下命令进行下载和安装: git clone https://github.com/mongodb/mongo.git sudo yum install -y mongodb-org-2.6.7 mongodb-org-server-2.6.7 mongodb-org-shell-2.6.7 mongodb-org-mongos-2.6.7 mongodb-org-tools-2.6.7 - 对于3.2版本的升级,您可以按照以下步骤进行操作: 1. 停止原来的Mongo服务。 2. 备份Mongo2.6版本的数据。 3.3.2版本的bin文件替换原来的bin文件来启动Mongo。 4. 使用以下命令启动Mongo: /usr/local/src/mongo3.2/bin/mongod --dbpath /usr/local/src/db/mongo --logpath /usr/local/src/log/mongo.log --logappend --fork 5. 进入Mongo,使用命令show dbs查看数据库,使用命令show collections查看数据集合,使用命令db.testData.find()查看集合数据。 #### 引用[.reference_title] - *1* [MongoDB 小版本升级步骤](https://blog.csdn.net/lp351539365/article/details/128264323)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [mongodb2.4版本跨版本升级](https://blog.csdn.net/weixin_44800915/article/details/106564328)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值