分享MongoDB修改oplog大小的4種方法
修改oplog有四種方法:
方法一
步驟如下:
- 停掉所有secondary節(jié)點(diǎn)
- 主節(jié)點(diǎn)刪除local目錄下文件,副本節(jié)點(diǎn)刪除數(shù)據(jù)目錄下所有文件
- 修改所有節(jié)點(diǎn)的配置文件,如:oplogSize=1000
- 重啟所有節(jié)點(diǎn),包括主節(jié)點(diǎn)和副本節(jié)點(diǎn)
- 重新配置replca set,副本節(jié)點(diǎn)會(huì)重新同步數(shù)據(jù)(initial sync)
優(yōu)點(diǎn):操作簡(jiǎn)單。
缺點(diǎn):需要停服務(wù),若數(shù)據(jù)量大,數(shù)據(jù)同步代價(jià)高。
方法二
步驟如下:
- remove其中一個(gè)secondary節(jié)點(diǎn),并關(guān)閉mongod服務(wù),刪除數(shù)據(jù)目錄下所有文件
- 修改此節(jié)點(diǎn)的參數(shù)文件,如:oplogSize=1000,并啟動(dòng)mongod服務(wù)
- 在primary節(jié)點(diǎn)執(zhí)行rs.add()將此節(jié)點(diǎn)加入到replica set
- 循環(huán)1-3步驟,將所有副本節(jié)點(diǎn)全部改完
- primary節(jié)點(diǎn)執(zhí)行rs.stepDown()將主節(jié)點(diǎn)降級(jí)為副本節(jié)點(diǎn)
- 從新的主節(jié)點(diǎn)remove掉此節(jié)點(diǎn),并進(jìn)行步驟1-3
這樣逐個(gè)修改每個(gè)節(jié)點(diǎn),完成oplog修改。
優(yōu)點(diǎn):解決了方法一中的停機(jī)問題。
缺點(diǎn):每個(gè)節(jié)點(diǎn)都要逐個(gè)重新同步,時(shí)間代價(jià)更高。
方法三
The oplog exists internally as a capped collection, so you cannot modify its size in the course of normal operations.另:改變oplog大小,需要在每個(gè)節(jié)點(diǎn)上執(zhí)行維護(hù)模式。(官方推薦)
1.關(guān)閉mongod
關(guān)閉mongod實(shí)例,如果是primary節(jié)點(diǎn),執(zhí)行rs.stepDown() 降級(jí)
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 admin switched to db admin handong1: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 peer server should be down... 2021-04-29T18:44:33.950+0800 I NETWORK [js] trying reconnect to 127.0.0.1:27017 failed 2021-04-29T18:44:33.967+0800 I NETWORK [js] reconnect 127.0.0.1:27017 failed failed
2.修改配置文件
修改配置文件,修改端口,注釋掉replSet和認(rèn)證相關(guān)的設(shè)置
port=27018 fork=true journal = true maxConns=500 logappend=true directoryperdb=true dbpath=/mongodb/data logpath=/mongodb/logs/mongodb.log
3.啟動(dòng)mongod實(shí)例,并備份oplog
mongod -f /mongodb/conf/mongodb.conf about to fork child process, waiting until server is ready for connections. forked process: 31553 child process started successfully, parent exiting
mongodump -d local -c oplog.rs --port 27018 -h 172.16.254.134 -o /mongodb/backup 2021-04-29T18:55:18.167+0800 writing local.oplog.rs to /mongodb/backup/local/oplog.rs.bson 2021-04-29T18:55:18.170+0800 done dumping local.oplog.rs (798 documents)
4.重建oplog
保存oplog最新時(shí)間點(diǎn)
> use local switched 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時(shí)間點(diǎn)記錄
> db.oplog.rs.save( db.temp.findOne() ) > db.oplog.rs.find()
6.關(guān)閉mongod實(shí)例
> use admin switched 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 peer server should be down... 2021-04-29T19:06:53.749+0800 I NETWORK [js] trying reconnect to 127.0.0.1:27018 failed 2021-04-29T19:06:53.749+0800 I NETWORK [js] reconnect 127.0.0.1:27018 failed failed
最后恢復(fù)mongodb.conf到初始狀態(tài),啟動(dòng)
方法四
如果你的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.驗(yàn)證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 local switched to db local handong1: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) }
到此這篇關(guān)于分享MongoDB修改oplog大小的4種方法的文章就介紹到這了,更多相關(guān)MongoDB修改oplog大小內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MongoDB 導(dǎo)出導(dǎo)入備份恢復(fù)數(shù)據(jù)詳解及實(shí)例
這篇文章主要介紹了MongoDB 導(dǎo)出導(dǎo)入備份恢復(fù)數(shù)據(jù)詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-10-10MongoDB下根據(jù)數(shù)組大小進(jìn)行查詢的方法
這篇文章主要介紹了MongoDB下根據(jù)數(shù)組大小進(jìn)行查詢的方法,分別實(shí)現(xiàn)了指定大小的數(shù)組和某個(gè)范圍的數(shù)組,需要的朋友可以參考下2014-04-04詳解MongoDB中用sharding將副本集分配至服務(wù)器集群的方法
副本集是MongoDB的主從復(fù)制中的重要功能,經(jīng)常被用來(lái)作額外的備份,這里我們就來(lái)詳解MongoDB中用sharding將副本集分配至服務(wù)器集群的方法,首先還是來(lái)回顧一下MongoDB中副本集的基本知識(shí):2016-07-07SpringBoot系列之MongoDB?Aggregations用法詳解
MongoDB?中使用聚合(Aggregations)來(lái)分析數(shù)據(jù)并從中獲取有意義的信息,本文重點(diǎn)給大家介紹SpringBoot系列之MongoDB?Aggregations用法,感興趣的朋友跟隨小編一起看看吧2022-02-02MongoDB查詢性能優(yōu)化驗(yàn)證及驗(yàn)證
這篇文章主要介紹了MongoDB查詢性能驗(yàn)證及優(yōu)化的相關(guān)知識(shí),涉及到MongoDB 查詢優(yōu)化原則知識(shí)點(diǎn),本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧2016-02-02