Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the ways for MongoDB to modify the size of oplog

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "what are the methods for MongoDB to modify the size of oplog". In daily operation, I believe that many people have doubts about the method of modifying oplog size by MongoDB. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "what are the methods for MongoDB to modify the size of oplog?" Next, please follow the editor to study!

There are four ways to modify oplog:

Method one

The steps are as follows:

Stop all secondary nodes

The primary node deletes the files in the local directory, and the replica node deletes all files in the data directory.

Modify the configuration files of all nodes, such as oplogSize=1000

Restart all nodes, including primary and replica nodes

Reconfigure replca set, and the replica node resynchronizes the data (initial sync)

Advantages: easy to operate.

Disadvantages: need to stop service, if the amount of data is large, the cost of data synchronization is high.

Method two

The steps are as follows:

Remove one of the secondary nodes and close the mongod service to delete all files in the data directory

Modify the parameter file for this node, such as oplogSize=1000, and start the mongod service

Execute rs.add () on the primary node to add this node to the replica set

Cycle 1-3 steps to change all replica nodes

The primary node executes rs.stepDown () to demote the primary node to the replica node

Remove this node from the new primary node and proceed to steps 1-3

In this way, each node is modified one by one to complete the oplog modification.

Advantages: the problem of downtime in method 1 is solved.

Disadvantages: each node has to be resynchronized one by one, and the time cost is higher.

Method three

The oplog exists internally as a capped collection, so you cannot modify its size in the course of normal operations. Also: to resize the oplog, you need to perform maintenance mode on each node. (official recommendation)

1. Close mongod

Close the mongod instance. If it is a primary node, perform rs.stepDown () degradation.

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 > 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 DBClientConnection failed to receive message from 27017-HostUnreachable: Connection closed by peerserver should be down...2021-04-29T18:44:33.950+0800 I NETWORK [js] trying reconnect to 127.0.1 trying reconnect to 27017 failed2021-04-29T18:44:33.967+0800 I NETWORK [js] reconnect 127.0.0.1 Suzhou 27017 failed failed2. Modify the configuration file

Modify configuration file, modify port, comment out replSet and authentication related settings

Port=27018fork=truejournal = truemaxConns=500logappend=truedirectoryperdb=truedbpath=/mongodb/datalogpath=/mongodb/logs/mongodb.log3. Start the mongod instance and back up oplogmongod-f/mongodb / conf/mongodb.conf about to fork child process, waiting until server is ready for connections.forked process: 31553child process started successfully, parent exitingmongodump-d local-c oplog.rs-- port 27018-h 172.16.254.134-o / mongodb/backup2021-04-29T18:55:18.167+0800 writing local.oplog.rs to / mongodb/backup/local/oplog.rs.bson2021-04-29T18:55:18.170+0800 done dumping local.oplog.rs (798 documents) 4. Rebuild oplog

Save the latest point in time of 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)}

Delete the old oplog

Db.oplog.rs.drop ()

Rebuild the new oplog with the size of 2GB

> db.runCommand ({create: "oplog.rs", capped: true, size: (2 * 1024 * 1024 * 1024)) 5. Insert previously saved oplog time point record > db.oplog.rs.save (db.temp.findOne ()) > db.oplog.rs.find () 6. Close mongod instance > 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 js 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 failed2021-04-29T19:06:53.749+0800 I NETWORK [js] reconnect 127.0.0.1 29T19:06:53.749+0800 27018 failed failed

Finally restore mongodb.conf to its initial state and start

Method 4

If your MongoDB version is later than 4.0, you can modify it directly using replSetResizeOplog.

1. Check oplog size handong1:SECONDARY > db.getReplicationInfo () {"logSizeMB": 1000, "usedMB": 1000, "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. Modify oplog size 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. Verify oplog size 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 Defragment Recycled space (optional) 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)} so far The study on "what are the ways for MongoDB to modify the size of oplog" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report