preface
This article mainly introduces the deployment and configuration of MongoDB replica set, based on the previous article MongoDB CentOS 7 installation and deployment
Differences between master-slave replica sets and replica sets
The biggest difference between master-slave cluster and replica set is that replica set has no fixed master node; The whole cluster will select a master node. After it is hung up, select other nodes from the remaining slave nodes as the master node. The replica set always has a master node and one or more backup nodes
Replica set configuration
framework
One master, one copy and one arbitration
role | ip | port |
---|---|---|
Primary | 192.168.1.142 | 27017 |
Secondary | 192.168.1.143 | 27017 |
Arbiter | 192.168.1.144 | 27017 |
system configuration
- Set host name
hostnamectl set-hostname node1
- Configure the mapping relationship between host name and ip in / etc/hosts (host name query static table)
vi /etc/hosts
192.168.1.142 node1 192.168.1.143 node2 192.168.1.144 node3
matters needing attention
- The collection name replSetName of the replica set must be the same, which is unified as rs here
- The firewall of the machine is either closed or the corresponding port is opened
Create node
Add and modify the configuration file mongodb.conf
systemLog: #The destination of all log output sent by MongoDB is specified as a file destination: file #The path of the log file to which mongod or mongos should send all diagnostic logging information path: "/usr/local/mongodb-5.0.2/log/mongodb.log" #When the mongos or mongod instance restarts, mongos or mongod appends a new entry to the end of the existing log file. logAppend: true storage: #The directory where the mongod instance stores its data. The storage.dbPath setting applies only to mongod. dbPath: "/usr/local/mongodb-5.0.2/data" journal: #Enable or disable persistent logging to ensure that data files remain valid and recoverable. enabled: true processManagement: #Enable daemon mode for running mongos or mongod processes in the background. fork: true #Specifies the file location where the process ID of the mongos or mongod process is saved, where mongos or mongod will write its PID pidFilePath: "/usr/local/mongodb-5.0.2/run/mongod.pid" net: #The service instance binds all IPS, which has side effects. When the replica set is initialized, the node name will be automatically set to the local domain name instead of IP #bindIpAll: true #IP address bound by the service instance bindIp: 0.0.0.0 #Bound port port: 27017 replication: #The name of the replica set replSetName: rs
The service of the three sets is the same
Start node service
/usr/local/mongodb-5.0.2/bin/mongod -f /usr/local/mongodb-5.0.2/conf/mongodb.conf
[root@localhost /]# /usr/local/mongodb-5.0.2/bin/mongod -f /usr/local/mongodb-5.0.2/conf/mongodb.conf about to fork child process, waiting until server is ready for connections. forked process: 38140 child process started successfully, parent exiting
Three nodes can be started in the same way
Initialize configuration replica set and master node
Connect to the master node using client commands
/usr/local/mongodb-5.0.2/bin/mongo --port 27017
Ready to initialize a new replica set
grammar
rs.initiate(configuration)
Parameter | Type | Description |
---|---|---|
configuration | document | Optional. A document that specifies configuration for the new replica set. If a configuration is not specified, MongoDB uses a default replica set configuration. |
Initializes the replica set with the default configuration
rs.initiate()
> rs.initiate() { "info2" : "no configuration specified. Using a default configuration for the set", "me" : "node1:27017", "ok" : 1 } rs:SECONDARY> rs:PRIMARY>
explain:
1) The value of "ok" is 1, indicating that the creation is successful.
2) The value of "me" is the current hostname + port
3) The command line prompt changes to a slave node role, which cannot be read or written by default. Wait a moment, enter and become the master node
View the configuration content of the replica set
Returns the document containing the current replica set configuration
rs.conf(configuration)
rs.config() is an alias for this method.
Configuration: optional. If there is no configuration, the default master node configuration will be used.
rs:PRIMARY> rs.conf() { "_id" : "rs", "version" : 1, "term" : 1, "members" : [ { "_id" : 0, "host" : "node1:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "secondaryDelaySecs" : NumberLong(0), "votes" : 1 } ], "protocolVersion" : NumberLong(1), "writeConcernMajorityJournalDefault" : true, "settings" : { "chainingAllowed" : true, "heartbeatIntervalMillis" : 2000, "heartbeatTimeoutSecs" : 10, "electionTimeoutMillis" : 10000, "catchUpTimeoutMillis" : -1, "catchUpTakeoverDelayMillis" : 30000, "getLastErrorModes" : { }, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 }, "replicaSetId" : ObjectId("614585a268fddfacd19367d0") } }
explain:
1) "_id": "rs": the primary key value of the replica set's configuration data store. By default, it is the replica set's name
2) "members": replica set member array. At this time, there is only one: "host": "node1:27017". The member is not an arbitration node: "arbiterOnly": false, priority (weight value): "priority": 1,
3) "settings": parameter configuration of replica set.
[prompt]
The view command of replica set configuration essentially queries the data in the table of system.replset
master:PRIMARY> use local switched to db local master:PRIMARY> show collections oplog.rs replset.election replset.initialSyncId replset.minvalid replset.oplogTruncateAfterPoint startup_log system.replset system.rollback.id system.tenantMigration.oplogView system.views master:PRIMARY> db.system.replset.find() { "_id" : "master", "version" : 1, "term" : 1, "members" : [ { "_id" : 0, "host" : "localhost.localdomain:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "secondaryDelaySecs" : NumberLong(0), "votes" : 1 } ], "protocolVersion" : NumberLong(1), "writeConcernMajorityJournalDefault" : true, "settings" : { "chainingAllowed" : true, "heartbeatIntervalMillis" : 2000, "heartbeatTimeoutSecs" : 10, "electionTimeoutMillis" : 10000, "catchUpTimeoutMillis" : -1, "catchUpTakeoverDelayMillis" : 30000, "getLastErrorModes" : { }, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 }, "replicaSetId" : ObjectId("614486497c0afbdf3172ebfe") } }
View replica set status
Check replica set status
explain:
Returns the document containing status information. This output uses data obtained from heartbeat packets sent by other members of the replica set to reflect the current state of the replica set
Front status.
Syntax:
rs.status()
rs:PRIMARY> rs.status() { "set" : "rs", "date" : ISODate("2021-09-18T06:27:46.387Z"), "myState" : 1, "term" : NumberLong(1), "syncSourceHost" : "", "syncSourceId" : -1, "heartbeatIntervalMillis" : NumberLong(2000), "majorityVoteCount" : 1, "writeMajorityCount" : 1, "votingMembersCount" : 1, "writableVotingMembersCount" : 1, "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1631946456, 1), "t" : NumberLong(1) }, "lastCommittedWallTime" : ISODate("2021-09-18T06:27:36.551Z"), "readConcernMajorityOpTime" : { "ts" : Timestamp(1631946456, 1), "t" : NumberLong(1) }, "appliedOpTime" : { "ts" : Timestamp(1631946456, 1), "t" : NumberLong(1) }, "durableOpTime" : { "ts" : Timestamp(1631946456, 1), "t" : NumberLong(1) }, "lastAppliedWallTime" : ISODate("2021-09-18T06:27:36.551Z"), "lastDurableWallTime" : ISODate("2021-09-18T06:27:36.551Z") }, "lastStableRecoveryTimestamp" : Timestamp(1631946436, 1), "electionCandidateMetrics" : { "lastElectionReason" : "electionTimeout", "lastElectionDate" : ISODate("2021-09-18T06:22:26.463Z"), "electionTerm" : NumberLong(1), "lastCommittedOpTimeAtElection" : { "ts" : Timestamp(0, 0), "t" : NumberLong(-1) }, "lastSeenOpTimeAtElection" : { "ts" : Timestamp(1631946146, 1), "t" : NumberLong(-1) }, "numVotesNeeded" : 1, "priorityAtElection" : 1, "electionTimeoutMillis" : NumberLong(10000), "newTermStartDate" : ISODate("2021-09-18T06:22:26.470Z"), "wMajorityWriteAvailabilityDate" : ISODate("2021-09-18T06:22:26.476Z") }, "members" : [ { "_id" : 0, "name" : "node1:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 388, "optime" : { "ts" : Timestamp(1631946456, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2021-09-18T06:27:36Z"), "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "electionTime" : Timestamp(1631946146, 2), "electionDate" : ISODate("2021-09-18T06:22:26Z"), "configVersion" : 1, "configTerm" : 1, "self" : true, "lastHeartbeatMessage" : "" } ], "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1631946456, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }, "operationTime" : Timestamp(1631946456, 1) }
explain:
1) "Set": "rs": the name of the replica set
2) "myState": 1: indicates that the state is normal
3) "members": replica set member array. At this time, there is only one: "name": "node1:27017". The member's role is "stateStr": "PRIMARY", and the node is healthy: "health": 1
Add replica slave node
Add a slave node to the master node and add other members to the replica set
Syntax:
rs.add(host, arbiterOnly)
Parameter | Type | Description |
---|---|---|
host | string or document | A new member to add to the replica set. Specify as string or configuration document: 1) if it is a string, specify the host name and optional port number of the new member; 2) If it is a document, specify the replica set member configuration document found in the members array. You must specify the host field in the member configuration document. For the description of the document configuration field, see the following document: "configuration document of host members" |
arbiterOnly | boolean | Optional. Applicable only if the value is a string. If true, the added host is the arbiter. |
[example]
Add the replica node of 192.168.1.143 to the replica set
rs:PRIMARY> rs.add("node2:27017") { "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1631948744, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }, "operationTime" : Timestamp(1631948744, 1) }
explain:
OK: 1 indicates that the addition is successful, but it does not mean that the specified replica node has been added to the replica set normally
View replica set status
rs:PRIMARY> rs.status() { "set" : "rs", "date" : ISODate("2021-09-18T08:03:52.132Z"), "myState" : 1, "term" : NumberLong(1), "syncSourceHost" : "", "syncSourceId" : -1, "heartbeatIntervalMillis" : NumberLong(2000), "majorityVoteCount" : 2, "writeMajorityCount" : 2, "votingMembersCount" : 2, "writableVotingMembersCount" : 2, "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1631952227, 1), "t" : NumberLong(1) }, "lastCommittedWallTime" : ISODate("2021-09-18T08:03:47.058Z"), "readConcernMajorityOpTime" : { "ts" : Timestamp(1631952227, 1), "t" : NumberLong(1) }, "appliedOpTime" : { "ts" : Timestamp(1631952227, 1), "t" : NumberLong(1) }, "durableOpTime" : { "ts" : Timestamp(1631952227, 1), "t" : NumberLong(1) }, "lastAppliedWallTime" : ISODate("2021-09-18T08:03:47.058Z"), "lastDurableWallTime" : ISODate("2021-09-18T08:03:47.058Z") }, "lastStableRecoveryTimestamp" : Timestamp(1631952207, 1), "electionCandidateMetrics" : { "lastElectionReason" : "electionTimeout", "lastElectionDate" : ISODate("2021-09-18T06:22:26.463Z"), "electionTerm" : NumberLong(1), "lastCommittedOpTimeAtElection" : { "ts" : Timestamp(0, 0), "t" : NumberLong(-1) }, "lastSeenOpTimeAtElection" : { "ts" : Timestamp(1631946146, 1), "t" : NumberLong(-1) }, "numVotesNeeded" : 1, "priorityAtElection" : 1, "electionTimeoutMillis" : NumberLong(10000), "newTermStartDate" : ISODate("2021-09-18T06:22:26.470Z"), "wMajorityWriteAvailabilityDate" : ISODate("2021-09-18T06:22:26.476Z") }, "members" : [ { "_id" : 0, "name" : "node1:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 6154, "optime" : { "ts" : Timestamp(1631952227, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2021-09-18T08:03:47Z"), "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "electionTime" : Timestamp(1631946146, 2), "electionDate" : ISODate("2021-09-18T06:22:26Z"), "configVersion" : 15, "configTerm" : 1, "self" : true, "lastHeartbeatMessage" : "" }, { "_id" : 1, "name" : "node2:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 667, "optime" : { "ts" : Timestamp(1631952227, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1631952227, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2021-09-18T08:03:47Z"), "optimeDurableDate" : ISODate("2021-09-18T08:03:47Z"), "lastHeartbeat" : ISODate("2021-09-18T08:03:50.682Z"), "lastHeartbeatRecv" : ISODate("2021-09-18T08:03:51.135Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncSourceHost" : "node1:27017", "syncSourceId" : 0, "infoMessage" : "", "configVersion" : 15, "configTerm" : 1 } ], "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1631952227, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }, "operationTime" : Timestamp(1631952227, 1) }
You can see that the members array becomes two bits
[note]
If optimeDate optimeDurableDate lastHeartbeatRecv is 1970, it is because the slave server cannot connect to the master server
My reason is that vi /etc/hosts only configures the host name and ip of the local machine for the first time. You can add all three nodes
If the value of stateStr does not change to SECONDARY, it is also a problem in the network environment between nodes
More replica set commands
rs:PRIMARY> rs.help() rs.status() { replSetGetStatus : 1 } checks repl set status rs.initiate() { replSetInitiate : null } initiates set with default settings rs.initiate(cfg) { replSetInitiate : cfg } initiates set with configuration cfg rs.conf() get the current configuration object from local.system.replset rs.reconfig(cfg, opts) updates the configuration of a running replica set with cfg, using the given opts (disconnects) rs.reconfigForPSASet(memberIndex, cfg, opts) updates the configuration of a Primary-Secondary-Arbiter (PSA) replica set while preserving majority writes memberIndex: index of the node being updated; cfg: the desired new config; opts: options passed in with the reconfig Not to be used with every configuration rs.add(hostportstr) add a new member to the set with default attributes (disconnects) rs.add(membercfgobj) add a new member to the set with extra attributes (disconnects) rs.addArb(hostportstr) add a new member which is arbiterOnly:true (disconnects) rs.stepDown([stepdownSecs, catchUpSecs]) step down as primary (disconnects) rs.syncFrom(hostportstr) make a secondary sync from the given member rs.freeze(secs) make a node ineligible to become primary for the time specified rs.remove(hostportstr) remove a host from the replica set (disconnects) rs.secondaryOk() allow queries on secondary nodes rs.printReplicationInfo() check oplog size and time range rs.printSecondaryReplicationInfo() check replica set members and replication lag db.isMaster() check who is primary db.hello() check who is primary reconfiguration helpers disconnect from the database so the shell will display an error, even if the command succeeds.
Add quorum node
rs.addArb("node3:27017")