您好,登錄后才能下訂單哦!
這篇文章主要介紹“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大小,需要在每個節點上執行維護模式。(官方推薦)
關閉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 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
修改配置文件,修改端口,注釋掉replSet和認證相關的設置
port=27018 fork=true journal = true maxConns=500 logappend=true directoryperdb=true dbpath=/mongodb/data logpath=/mongodb/logs/mongodb.log
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)
保存oplog最新時間點
> 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) } )
> db.oplog.rs.save( db.temp.findOne() ) > db.oplog.rs.find()
> 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
最后恢復mongodb.conf到初始狀態,啟動
如果你的MongoDB版本為4.0以后的版本,可以直接使用replSetResizeOplog修改。
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)" }
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) }
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)" }
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) }
到此,關于“MongoDB修改oplog大小的方法有哪些”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。