Detailed explanation of MongoDB replica set

Replica set role

  1. The data is synchronized. If a replica hangs up, the rest of the replica sets can keep up and continue to be used.
  2. Databases that can be backed up

Enable multiple copies

All operations this time are carried out on windows, and linux is the same principle.
First write three mongod cfg
Suppose mongo is installed in D:\Program Files\mongodb\server.2 directory
mongod.cfg:

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: D:\Program Files\mongodb\server\4.2\data
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path:  D:\Program Files\mongodb\server\4.2\log\mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0

#processManagement:

#security:
  #authorization: enabled
  #keyFile:	D:\Program Files\mongodb\server\4.2\mongo_key
  
#operationProfiling:

# The name of the replica set to which replSetName belongs. All hosts in the replica set must use the same set name
replication:
  replSetName: rs0

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

The other two mongod Only a few places are modified in CFG:

dbPath: D:\Program Files\mongodb\server\4.2\data
path:  D:\Program Files\mongodb\server\4.2\log\mongod.log
port: 27017

Remember: replSetName must be set to the same. If it is not set, it will not be started as a replica set

# The name of the replica set to which replSetName belongs. All hosts in the replica set must use the same set name
replication:
  replSetName: rs0

Start:

mongod.exe --config mongod.cfg
mongod.exe --config mongod2.cfg
mongod.exe --config mongod3.cfg

If my ip is 192.168.1.888
The three mongo s are:

192.168.1.888:27017
192.168.1.888:27016
192.168.1.888:27015

When you enter mongo, you'd better take the host with you

mongo.exe  --host 192.168.1.888 --port 27017
config = {_id:"liangqicong",members:[{ _id:0,host:"192.168.1.188:27017" },{ _id:1,host:"192.168.1.188:27016" },{ _id:2,host:"192.168.1.188:27015" }]}
rs.initiate(config);
rs.status() #View mongo replica set status

Explain: the meaning of each item in rs.conf()

{
        "_id" : "rs0",#MongoDB replica set name
        "version" : 135972,
        "protocolVersion" : NumberLong(1),
        "writeConcernMajorityJournalDefault" : true,
        "members" : [#Replica set members
                {
                        "_id" : 0,
                        "host" : "192.168.1.183:27017",//host, the ip of mongo instance, can be the domain name
                        "arbiterOnly" : false,#Whether he is an arbiter, does not save data, and only participates in the election
                        "buildIndexes" : true,#Index mongo
                        "hidden" : false,#Whether to hide the instance. If it is set to true, the database cannot be read, and the member has the right to vote
                        "priority" : 10,#Priority, priority and votes are explained in detail below, focusing on this
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),#How long is the synchronization delayed
                        "votes" : 1#number of votes
                },
                {
                        "_id" : 1,
                        "host" : "192.168.1.183:27015",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 0,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 0
                },
                {
                        "_id" : 2,
                        "host" : "ngrok.viatank.com:5555",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 0,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 0
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "catchUpTimeoutMillis" : -1,
                "catchUpTakeoverDelayMillis" : 30000,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("5f35ed66c17c49c0519d52c3")
        }
}

priority and votes

  1. Priority is the sign of becoming a PRIMARY member. The higher the number, the higher the priority, and the greater the possibility of being selected as PRIMARY
  2. If priority is 0, there will be no voting rights and there is no possibility of becoming PRIMARY
  3. A votes value of 0 or 1 indicates the number of votes owned
  4. If there are three mongo s, it's better to set priority to different values. Otherwise, if one of them dies when the number of votes is 1, the other two will not be able to elect PRIMARY, because the number of votes they have is the same and the priority is the same
  5. If you only want to do backup now, two mongos, mongo1 for PRIMARY, and mongo2 for backup. Then the priority of mongo2 should be set to 0, and the votes should be set to 0. This can ensure that if mongo1 hangs up, mongo2 will not take over. If mongo1 starts again, it will also be elected as PRIMARY

Modify the information in rs.conf()

cfg = rs.conf()
cfg.members[0].priority = 5
cfg.members[1].votes = 0
rs.reconfig(cfg)

Remember, do not modify replset directly in the database. There will be problems in synchronization

rs.slaveOk()
For the current session, this command allows reading operations from non primary instances (i.e. slave or secondary instances)

Aside: many clients need to reconnect before they can see the data, because each connection is a session. If you do not reconnect, you cannot have a read operation

Posted by turbocueca on Mon, 23 May 2022 20:43:28 +0300