In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
A MongoDB replica set (Replica Set) is a cluster of MongoDB instances, consisting of a primary node (primary) and multiple standby nodes (Secondary). Through Repication, data updates are pushed from primary to other slave
On the node. Each MongoDB instance maintains the same copy of the dataset. By maintaining redundant copies of the dataset, remote data backup, read-write separation and automatic failover can be realized.
Topology:
Create a Replica set with three members, one primary and two secondary members, primary handles client requests, and secondary is used to hold copies of primary data. The client reads and writes on the primary, through the
Replication's asynchronous synchronization mechanism synchronizes data operations with sencondary members to ensure that three members have the same dataset.
If the primary connection is interrupted for more than 10 seconds, other nodes will automatically elect a primary node, which is responsible for responding to the client's request and realizing automatic data failover.
Technical principle description:
There are two different ways to start mongodb instance, one is single instance startup (standalone), the other is replica set (replica set) mode. If you set the replSet parameter, the mongodb instance will start in replica set mode.
To elect primary members:
There are two types of members in Replica Set: Primary members and Secondary members. A Replica Set can have only one Primary member, but can have multiple Secondary members. Primary is used to process client requests, and Secondary is used to hold a copy of the data of Primary. If the Primary crashes and Replica Set detects that the Primary is inaccessible, it initiates an automatic failover process and votes to elect one of the remaining Secondary members as the Primary to receive and process client requests.
When electing Primary members, the principles of "majority" and "one vote veto" are used. In Replica Set, each member can only require himself to be elected as a Primary node. When a Secondary member is unable to communicate with a Primary member, the member initiates an election, requesting other members to elect himself as a Primary member. Only with the support of the "majority" member can the member be elected as a Primary member; as long as one member vetoes, the election will be cancelled.
Not every member has the right to vote. In a Replica Set, at most seven members have the right to vote. Primary members are elected by these seven members. For a member who has the right to vote, its attribute: "votes" is 1; if 0, it means that the member does not have the right to vote.
Operation log:
Mongodb uses the operation log (oplog) to implement the replication function. Oplog contains every update operation of the primary node, which is passed to other secondary nodes through oplog, and the submitted operation is redone (redo) in other nodes to achieve asynchronous synchronization of data. Each member maintains its own oplog, recording every data copied from the primary node.
The replication process is to copy the data first, and then write it in oplog. If the operation is repeated as a result of a restart, performing the same operation in the oplog multiple times is the same as performing it once.
Oplog saves the log of update operations for each doc. If a command updates only one doc, then the Replication process inserts a log into the oplog; if a command updates multiple doc, then the Replication process inserts multiple logs into the oplog, each log updating only one doc.
Profile parameter definition:
ReplSet: sets the name of Replica Set, which must be the same in each configuration file
The directory used by dbpath:MongoDB to store data
Logpath: log data used to record mongod
Port: specifies the port on which MongoDB listens. The default value is 27017.
Create the configuration document, call the rs.initiate () function, and initialize the Replica Set according to the configuration document:
Conf=
{
"_ id": "replname"
"members": [
{"_ id": 0, "host": "127.0.0.1 purl 2777"}
{"_ id": 1, "host": "127.0.0.1 3777"}
{"_ id": 2, "host": "127.0.0.1 purl 4777"}
]
}
Rs.initiate (conf)
Use "_ id": "replname" to specify the name of the Replica Set
The members array specifies the ID and host ("host:port") of the members of Replica Set
1. Modify Replica Set
If Replica Set is initialized with rs.initiate (), then MongoDB initializes Replica Set with the default configuration document, and you can add members through the add () function
2. Add a member to Replica Set
Rs.add ("host:port")
3. Delete a member from Replica Set
Rs.remove ("host")
4. View the configuration of Replica Set
Rs.conf ()
5. Check the status of Replica Set
Rs.status ()
{
"_ id": "replname"
"version": 7
"protocolVersion": NumberLong (1)
"writeConcernMajorityJournalDefault": true
"members": [
{
"_ id": 0
"host": "127.0.0.1 purl 2777"
"arbiterOnly": false
"buildIndexes": true
"hidden": false
"priority": 1
"tags": {
}
"slaveDelay": NumberLong (0)
"votes": 1
}
The ID field of Replica Set uniquely identifies a Replica Set, and each Replica Set has a self-increasing version number, which is identified by the Version field to identify a different version of Replica Set. The initial value of the version field is 1, and the version field increments each time you modify the configuration of Replica Set.
ArbiterOnly:0 or 1, marks an arbitration (arbiter), the only function of Arbiter is to participate in the election of Primary, Arbiter does not save data, will not provide services for client, it exists for the purpose of electing Primary.
Hidden:0 or 1, indicating whether the member is a hidden member, the main role of a Hidden member is to back up data, and you can use a poor-performing server as a Hidden member. Hidden members will not receive requests from Client, nor will they become Primary. When you set a Hidden member, you must set the members [n]. Priority property to 0. 0.
Priority: a numeric type that sets the priority for a member to become a Primary. The higher the priority, the more likely it is to become a Primary. If priority=0, then the member will never become Primary.
Votes:1, or 0, indicates the number of votes for that member. In each Replica Set, there are up to 7 members with a votes property value of 1. A member whose votes attribute is 1 (voting members) has the right to elect a Primary. If a member wants to become a Primary, he must have the support of most members of voting members.
In Replica Set, if the number of voting members is 5, then a member becomes a Primary only if it is supported by more than 2 voting members and there is no voting members objection. As long as any voting member objects to the member becoming a Primary, the member cannot become a Primary.
View process information on the client:
Shard1:PRIMARY > db.serverCmdLineOpts ()
Meaning of the command:
The rs.: copy command, which is replSet, is an acronym for replica set.
Db.: database commands, such as db.printReplicationInfo (), db.printSlaveReplicationInfo ()
Rs.status: check the replication status of members, which can be executed on any node
Rs.config (): you can get the current copy of the configuration, modify the configuration file, and then pass the modified configuration file to the reconfig, initiate commands
Rs.reconfig () (replSetReconfig): modifies the configuration of the replica set. Rs.reconfig (conf, {"force": true})
Rs.initiate (): initializes the configuration, only needs to call rs.initiate on one member in the replica set (general master node), and the member that receives the initiate command will automatically pass the configuration file to other members in the replica set
Restrictions when modifying the configuration of replica set members:
1. Cannot modify _ id
2. The priority of the member currently executing the rs.reconfig command cannot be set to 0.
3. An arbitrator member cannot be turned into a non-arbitrator member, anyway.
4. You cannot change buildIndexes from false to true.
Force reconfiguration:
Call rs.reconfig (conf, {"force": ture}) on the backup node to force the replica set to be reconfigured. Note that conf must be a correct and valid configuration. And force reconfiguration is only allowed on the backup node.
View replication sourc
Query the node from which the node is copied. Run on the backup node, or you can run rs.status () to view the syncingTo field information.
Db.adminCommand ({"replSetGetStatus": 1}) ['syncingTo']
Copy chain:
MongoDB selects the synchronization source based on ping time, and one member sends a heartbeat request to another member to know the time taken by the heartbeat request ("pingMs" in rs.status () records the average time it takes a member to reach the relevant member). MongosDB maintains the average time spent on requests between different members. When selecting a synchronization source, you will choose a member who is closer to you and whose data is newer than you. However, members of the same data center may replicate from other members of the same data center, rather than from a master node located in another data center (which reduces network traffic), so there will be a replication chain. the longer the replication chain, the longer it takes for the primary node's operations to replicate to all servers. This is undesirable for the need to read data from a copy.
Modify replication sourc
Db.adminCommand ({"replSetSyncFrom": "192.168.137.10 purl 27011"})
Disable replication chain (executed in the master copy)
Conf = rs.conf ()
Conf.settings.chainingAllowed = false
Rs.reconfig (conf)
Calculation delay:
View the current replica set oplog status
Rs.printReplicationInfo ()
Size of configured oplog size:oplog configuration
Log length start to end: the duration of the operation included in the oplog.
Oplog first event time: the time of the first operation of oplog.
Oplog last event time: the time of the last operation of oplog.
Now: current time.
Note: the time difference between the first operation and the last operation in oplog is the length of the operation log.
Viewing the replication delay displays the current synchronization time of all backup nodes and the length of time behind the primary node:
Rs.printSlaveReplicationInfo ()
Resize the oplog:
1) if it is the primary node, turn the primary node into a backup node.
2) Save the last insert operation in oplog to another collection.
Use local
Db.tempLastOp20180330.save (db.oplog.rs.find ({}, {ts: 1, h: 1}) .sort ({$natural:-1}) .limit (1) .next ()
3) shut down the current service
4) Boot in stand-alone mode. You can specify a new port or comment out the replSet to start with the configuration file.
Mongod-- port 6777-- bind_ip 192.168.10.10127.0.0.1-- dbpath / u02/mongo6777/data/
5) Delete the current oplog
Db.oplog.rs.drop ()
6) create a new oplog
Db.createCollection ("oplog.rs", {"capped": true, "size": 1048576})
Db.createCollection ("oplog.rs", {"capped": true, "size": (2) 1024: 1024)})
7) write the last insert record back to oplog
Var tempLastOp=db.tempLastOp20180330.find () .next ()
Db.oplog.rs.insert (tempLastOp)
Db.oplog.rs.find ()
8) start the current service as a replica member
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.