In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Editor to share with you the example analysis of the MongoDB copy set, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!
The version of Mongodb used in the experimental environment is mongodb-linux-x86_64-2.6.0.
Built by three virtual machines, configured as a single core, 1G memory.
The experimental environment is as follows:
The replica set of MongoDB is different from the previous master-slave mode.
In the event of a cluster Master failure, the replica set can automatically vote, elect a new Master, and guide the rest of the Slave servers to connect to the new Master
This process is transparent to the application. It can be said that the replica set of MongoDB is a master-slave replication with its own failover function.
1 advantages over the traditional master-slave model
In the traditional master-slave mode, you need to specify the Master in the cluster manually.
If the Master fails, it is usually manual intervention to specify a new Master.
This process is generally not transparent to the application, which is often accompanied by the application to modify the configuration file, restart the application server and so on.
In the MongoDB replica set, any node in the cluster can become a Master node.
Once the Master node fails, a new Master node is elected among the remaining nodes.
And guide the remaining nodes to connect to the new Master node. This process is transparent to the application.
2 Bully election algorithm
Bully algorithm is a coordinator (master node) election algorithm, the main idea is that each member of the cluster can declare that it is the master node and notify other nodes.
Other nodes can choose to accept the claim or reject it and enter the master node to compete. Only the node that is accepted by all other nodes can become the primary node.
The node determines who should win according to some attributes. This property can be a static ID or an updated metric like the most recent transaction ID (the latest node will win)
His election process is as follows:
? Get the last operation timestamp of each server node. Every mongodb has an oplog mechanism that records local operations, which makes it easy to compare data synchronization with the main server and can also be used for error recovery.
? If most of the servers in the cluster are down, the nodes that remain alive are in the secondary state and stop, and there is no election.
? If the last synchronization time of the elected master node or all slave nodes in the cluster looks old, stop the election and wait for someone to operate.
? If there is no problem above, select the server node with the latest timestamp of the last operation (to ensure that the data is up-to-date) as the primary node.
The trigger conditions of the election
When initializing a replica set.
The replica set is disconnected from the primary node, which may be a network problem.
The primary node is dead.
Human intervention, such as changing node priority, etc.
There is also a prerequisite for the election, the number of nodes participating in the election must be more than half of the total number of nodes in the replica set, and if it is already less than half, all nodes remain read-only.
3 build replica set cluster
Each virtual machine starts the instance with the following configuration file:
Dbpath = / home/lihuilin/mongodata
Smallfiles = true
ReplSet = mvbox
Then log in to mongo from any virtual machine and enter the following settings
Config = {_ id: "mvbox", members: [
{_ id:0,host: "192.168.1.1 purl 27017"}
{_ id:1,host: "192.168.1.2 purl 27017"}
{_ id:2,host: "192.168.1.3 virtual 27017"}]
}
Rs.initiate (config)
You can see that the copy set is already in effect.
You can use rs.status () to view the cluster status, or rs.isMaster ()
4 change node priority
Changing the priority of a node triggers a re-election so that the master node can be specified manually.
Use the following command to log in at the primary node and promote 192.168.1.3 to Master.
Rs.conf ()
Cfg=rs.conf ()
Cfg.members [0] .priority = 1
Cfg.members [1] .priority = 1
Cfg.members [2] .priority = 10
Rs.reconfig (cfg)
It is important to note that changing the node priority requires logging in to the Master node to run. Or report a mistake.
Looking at the cluster status again, you can see that 192.168.1.3 is already running as Master
5 Node Typ
The node types of MongoDB are primary node (Master), replica node (Slave or Secondary), arbitration node, Secondary-Only node, Hidden node, Delayed node and Non-Voting node.
The quorum node does not store data, but only votes in the group responsible for failover, thus reducing the pressure on data replication.
Secondary-Only: cannot be a primary node, but can only be a secondary replica node to prevent some low-performance nodes from becoming master nodes.
Hidden: this type of node cannot be referenced by the client IP, nor can it be set as the master node, but it can vote and is generally used to back up data.
Delayed: you can specify a time delay to synchronize data from the primary node. Mainly used for backup data, if real-time synchronization, mistakenly deleted data immediately synchronized to the slave node. Therefore, delayed replication is mainly used to avoid user errors.
Non-Voting: a secondary node that does not have the right to vote, a pure backup data node.
6 set hidden node (Hidden)
A hidden node can vote in an election, but it cannot be referenced by the client or become a master node. That is, this node cannot be used in read-write separation scenarios.
Set 192.168.1.3 as the hidden node.
Note that only members with a priority of 0 can be set as hidden nodes.
If the node whose priority is not set to 0 is hidden, the error is as follows
Use the following command to set the hidden node
Cfg=rs.conf ()
Cfg.members [0] .priority = 10
Cfg.members [1] .priority = 1
Cfg.members [2] .priority = 0
Cfg.members [2] .hidden = 1
Rs.reconfig (cfg)
After the setup is complete, use rs.status () to see whether the node is still SECONDARY.
But you can see the change in this node through rs.isMaster () and rs.conf ().
The 192.168.1.3 node in the hosts of rs.isMaster () is no longer visible
And rs.conf () shows that the node status is hidden
7 set up the arbitration node
The quorum node does not store data, but is only used for voting. So the quorum node has a low load on the server.
Once a node joins the cluster as an arbitrator, it can only be an arbitrator and cannot be configured as a non-arbitrator, and vice versa.
Another cluster can only use at most one arbitrator, and the additional arbitrator slows down the speed of electing new Master nodes and does not provide better data security.
When initializing the cluster, the configuration of the setting arbitrator is as follows
Config = {_ id: "mvbox", members: [
{_ id:0,host: "192.168.1.1 purl 27017"}
{_ id:1,host: "192.168.1.2 27017", arbiterOnly:true}
{_ id:2,host: "192.168.1.3 virtual 27017"}]
}
The arbitrator is used mainly because the MongoDB replica set requires odd members and there are not enough servers. The arbitrator node should not be used when there are sufficient servers.
8 set delay replication node
MongoDB officially does not have an incremental backup scheme, only an exported tool, mongodump.
Like a database, he can't push the data to the moment before the accident through binlog or archived logs.
Suppose you use mongodump backup at 2 a.m. every day, and if an accident occurs at 5 p.m., and the database is corrupted, all data will be lost from 2 a.m. to 5 p.m.
Although replica sets can avoid this problem to some extent, human error cannot be avoided by default.
For example, no filter criteria is specified and all data is deleted. The replica node applies this command to delete the data of all replica nodes.
In this scenario, a delay node can be used, which delays the application of replication.
If there is a human error in the primary node, and this operation has not been applied to the delayed node because of the delay.
At this point, modify the priority of the delay node to the highest level to make it the new Master server.
The priority of the delay node must be 0. This is the same as the hidden node.
Set 192.168.1.2 as the delay node
Cfg=rs.conf ()
Cfg.members [1] .priority = 0
Cfg.members [1] .slaveDelay = 3600
Rs.reconfig (cfg)
The unit of slaveDelay is seconds
In 192.168.1.1, the master node deletes a collection of all data to simulate human error.
Looking at 192.168.1.3, it was found that all the data had been lost.
On the 192.168.1.2 delay node, you can see that the data is still there because of the delayed replication.
Do not raise the priority of the delay node at this time. Because in this way, he will immediately apply all the operations of the original master node and become the new master node. In this way, the misoperation is synchronized to the delay node.
First, close the other members of the replica set, except for the delay node.
Delete all data in the other member data directory. Make sure that every other member's data directory is empty (except for delay nodes)
Restart other members and they will automatically recover data from the delayed node.
9 set the Secondary-Only node
A node with a Priority of 0 can never be a master, so setting a Secondary-only node only requires setting its priority to 0. 0.
10 set the Non-Voting node
Assuming that the 192.168.1.1 setting does not allow voting, use the following command
Cfg=rs.conf ()
Cfg.members [0] .votes = 0
Rs.reconfig (cfg)
11 member status of replica set
Replica set membership status refers to the stateStr field of rs.status ()
STARTUP: just joined the replication set, and the configuration has not been loaded yet
STARTUP2: configuration has been loaded, initialization status
RECOVERING: recovering, not suitable for reading
ARBITER: arbitrator
DOWN: node unreachable
UNKNOWN: do not get the state of other nodes but do not know what state it is. It usually occurs in a structure with only two members, brain fissure.
REMOVED: removing replication set
ROLLBACK: data rollback, moving to RECOVERING or SECONDARY state at the end of the rollback
FATAL: error. Check the log grep "replSet FATAL" to find the cause of the error and re-synchronize
PRIMARY: primary node
SECONDARY: backup nod
12 read-write separation
If the read-write pressure of the Master node is too high, the read-write separation scheme can be considered.
However, you need to consider a scenario in which the write pressure on the primary server is very high, so the write pressure on the replica node is also high.
At this time, if the reading pressure of the replica node is also high, according to the read-write lock mechanism at the MongoDB library level
It is likely that the replication writer will not be able to get the write lock, resulting in a large delay between the replica node and the primary node.
If you do read-write separation, you first need to declare it as slave on the replica node.
Then set it in the JAVA program
There are several settings for ReadRreference
Primary: default parameter, read only from the primary node
PrimaryPreferred: most of the data is read from the primary node, and only from the secondary node if the primary node is not available.
Secondary: read only from the secondary node. The problem is that the secondary node's data is "older" than the primary node's data.
SecondaryPreferred: priority is given to reading from the secondary node, and reading data from the master node when the secondary node is not available
Nearest: read data from the node with the lowest latency on the network, whether it is the master node or the secondary node.
The above is all the content of this article "sample Analysis of MongoDB replica set". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.