Hello, if you find this article helpful, you can express your gratitude through saweria .



The client has 3 MongoDB instances with a replica set, and they want to move to another server because they need more storage to store data. What is the best way to do that with minimal downtime?

  1. Using the backup & restore method does not work because the data from the production replica set will always change. You need to stop the replica set to make the data from the old server and replica consistent.

  2. Using the sync method (using cp or rsync) is the same as the backup & restore method; it does not work because the data from the production replica set will always change. You need to stop the replica set to make the data from the old server and replica consistent.

  3. The best way to migrate a MongoDB replica set is to add a new server as a hidden member and then promote it as primary & secondary.

    ## Add a new server as a hidden member
    rs.add({_id:6, host: "192.168.1.6:27017", priority: 0, hidden: true})
    
    # To promote it as a secondary replica
    cfg = rs.conf()
    cfg.members[6].priority = 1
    cfg.members[6].hidden = false
    rs.reconfig(cfg)
    
    
    #if above command doesnt work
    var config = rs.conf();
    
    config.members.forEach(function(member) {
        if (member._id === 6) {
            member.hidden = false; 
            member.priority = 1; 
        }
    });