MongoDB修改oplog大小的方法有哪些
这篇文章主要介绍“MongoDB修改oplog大小的方法有哪些”,在日常操作中,相信很多人在MongoDB修改oplog大小的方法有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”MongoDB修改oplog大小的方法有哪些”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
修改oplog有四种方法:
方法一
步骤如下:
停掉所有secondary节点
主节点删除local目录下文件,副本节点删除数据目录下所有文件
修改所有节点的配置文件,如:oplogSize=1000
重启所有节点,包括主节点和副本节点
重新配置replca set,副本节点会重新同步数据(initial sync)
优点:操作简单。
缺点:需要停服务,若数据量大,数据同步代价高。
方法二
步骤如下:
remove其中一个secondary节点,并关闭mongod服务,删除数据目录下所有文件
修改此节点的参数文件,如:oplogSize=1000,并启动mongod服务
在primary节点执行rs.add()将此节点加入到replica set
循环1-3步骤,将所有副本节点全部改完
primary节点执行rs.stepDown()将主节点降级为副本节点
从新的主节点remove掉此节点,并进行步骤1-3
这样逐个修改每个节点,完成oplog修改。
优点:解决了方法一中的停机问题。
缺点:每个节点都要逐个重新同步,时间代价更高。
方法三
The oplog exists internally as a capped collection, so you cannot modify its size in the course of normal operations.另:改变oplog大小,需要在每个节点上执行维护模式。(官方推荐)
1.关闭mongod
关闭mongod实例,如果是primary节点,执行rs.stepDown() 降级
handong1:PRIMARY> rs.stepDown(){"ok" : 1,"$clusterTime" : {"clusterTime" : Timestamp(1619693040, 1),"signature" : {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)}},"operationTime" : Timestamp(1619693040, 1)}handong1:SECONDARY> handong1:SECONDARY> handong1:SECONDARY> handong1:SECONDARY> use adminswitched to db adminhandong1:SECONDARY> db.shutdownServer()2021-04-29T18:44:33.947+0800 I NETWORK [js] DBClientConnection failed to receive message from 127.0.0.1:27017 - HostUnreachable: Connection closed by peerserver should be down...2021-04-29T18:44:33.950+0800 I NETWORK [js] trying reconnect to 127.0.0.1:27017 failed2021-04-29T18:44:33.967+0800 I NETWORK [js] reconnect 127.0.0.1:27017 failed failed
2.修改配置文件
修改配置文件,修改端口,注释掉replSet和认证相关的设置
port=27018fork=truejournal = truemaxConns=500logappend=truedirectoryperdb=truedbpath=/mongodb/datalogpath=/mongodb/logs/mongodb.log
3.启动mongod实例,并备份oplog
mongod -f /mongodb/conf/mongodb.conf about to fork child process, waiting until server is ready for connections.forked process: 31553child process started successfully, parent exiting
mongodump -d local -c oplog.rs --port 27018 -h 172.16.254.134 -o /mongodb/backup2021-04-29T18:55:18.167+0800writing local.oplog.rs to /mongodb/backup/local/oplog.rs.bson2021-04-29T18:55:18.170+0800done dumping local.oplog.rs (798 documents)
4.重建oplog
保存oplog最新时间点
> use localswitched to db local> db.temp.save( db.oplog.rs.find( { }, { ts: 1, h: 1 } ).sort( {$natural : -1} ).limit(1).next() )WriteResult({ "nInserted" : 1 })
> db.temp.find(){ "_id" : ObjectId("608a914089abaa981f14e888"), "ts" : Timestamp(1619693066, 1), "h" : NumberLong(0) }
删除旧的oplog
db.oplog.rs.drop()
重建新的oplog,大小为2GB
> db.runCommand( { create: "oplog.rs", capped: true, size: (2 * 1024 * 1024 * 1024) } )
5.插入前面保存的oplog时间点记录
> db.oplog.rs.save( db.temp.findOne() )> db.oplog.rs.find()
6.关闭mongod实例
> use adminswitched to db admin> db.shutdownServer()2021-04-29T19:06:53.745+0800 I NETWORK [js] DBClientConnection failed to receive message from 127.0.0.1:27018 - HostUnreachable: Connection closed by peerserver should be down...2021-04-29T19:06:53.749+0800 I NETWORK [js] trying reconnect to 127.0.0.1:27018 failed2021-04-29T19:06:53.749+0800 I NETWORK [js] reconnect 127.0.0.1:27018 failed failed
最后恢复mongodb.conf到初始状态,启动
方法四
如果你的MongoDB版本为4.0以后的版本,可以直接使用replSetResizeOplog修改。
1.查看oplog大小
handong1:SECONDARY> db.getReplicationInfo(){"logSizeMB" : 1000,"usedMB" : 0.17,"timeDiff" : 6736,"timeDiffHours" : 1.87,"tFirst" : "Thu Apr 29 2021 17:19:14 GMT+0800 (CST)","tLast" : "Thu Apr 29 2021 19:11:30 GMT+0800 (CST)","now" : "Thu Apr 29 2021 19:11:42 GMT+0800 (CST)"}
2.修改oplog大小
handong1:SECONDARY> db.adminCommand({replSetResizeOplog:1,size:2000}){"ok" : 1,"$clusterTime" : {"clusterTime" : Timestamp(1619694744, 14),"signature" : {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)}},"operationTime" : Timestamp(1619694744, 14)}
3.验证oplog大小
handong1:SECONDARY> db.getReplicationInfo(){"logSizeMB" : 2000,"usedMB" : 0.18,"timeDiff" : 6852,"timeDiffHours" : 1.9,"tFirst" : "Thu Apr 29 2021 17:19:14 GMT+0800 (CST)","tLast" : "Thu Apr 29 2021 19:13:26 GMT+0800 (CST)","now" : "Thu Apr 29 2021 19:13:28 GMT+0800 (CST)"}
4.整理碎片,回收空间(可选)
handong1:SECONDARY> use localswitched to db localhandong1:SECONDARY> db.runCommand({"compact" : "oplog.rs"}){"ok" : 1,"$clusterTime" : {"clusterTime" : Timestamp(1619694840, 1),"signature" : {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)}},"operationTime" : Timestamp(1619694840, 1)}
到此,关于“MongoDB修改oplog大小的方法有哪些”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341