Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of role types of MongoDB replication set

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

MongoDB Replication Set Role Type

MongoDB Replication Set Role Type

1. Standard node: participate in primary election, and automatically give up primary when it is down or stops service.

2. Passive node: it can only become the second node and does not participate in the election. If it is set as a passive node, it cannot participate in the election.

3. Arbitration node: responsible for voting, not storing data, ensuring that the number of votes of standard nodes will not be the same.

Experiments are conducted to verify that standard nodes preempt each other, passive nodes do not preempt each other, and arbitration nodes do not store data.

Experiment with MongoDB multi-instance, yum install MongoDB and open multi-instance please refer to the following

My other blog: blog.51cto.com/13760226/2174032

experimental role

● Standard node 1: 192.168.60.135: 27017

● Standard node 2: 192.168.60.135: 27018

● Passive nodes: 192.168.60.135: 27019

● Arbitration node: 192.168.60.135: 27020

I. Modify configuration files

vim /etc/mongod.conf

systemLog: #Modify log file path under log module

destination: file

logAppend: true

path: /usr/local/mongodb/mongod1.log

(The remaining instances also need to be modified, for example: mongod2.log, mongod3.log, mongod4.log)

storage: #Modify data configuration file under storage module

dbPath: /usr/local/mongo1

(Same as above, for example: mongo2, mongo3, mongo4)

net:

port: 27017 The port number of each instance cannot be the same (example: 27018, 27019, 27020)

bindIp: 0.0.0.0 Listen to arbitrary addresses

Directly add the following two lines to open the replication set

replication:

replSetName: repl

II. Copy configuration files and create their own data storage directories

cp /etc/mongod.conf /etc/mongod2.conf

cp /etc/mongod.conf /etc/mongod3.conf

cp /etc/mongod.conf /etc/mongod4.conf

mkdir /usr/local/mongo{1,2,3,4} #Create 4 data storage directories at once (corresponding to configuration files)

mkdir /usr/local/mongodb #Create log storage directory

touch mongod{1,2,3,4}.log #Create log file corresponding to instance

(Note: Don't forget to modify the copied configuration file, the port number cannot be the same)

3. Enable multiple instances, set standards, passive, arbitration nodes

[root@localhost ~]#mongod -f /etc/mongod.conf

[root@localhost ~]#mongod -f /etc/mongod2.conf

[root@localhost ~]#mongod -f /etc/mongod3.conf

[root@localhost ~]#mongod -f /etc/mongod4.conf

[root@localhost mongodb]# netstat -ntap | grep mongod

tcp 0 0 0.0.0.0:27019 0.0.0.0:* LISTEN 2280/mongod

tcp 0 0 0.0.0.0:27020 0.0.0.0:* LISTEN 2308/mongod

tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 2224/mongod

tcp 0 0 0.0.0.0:27018 0.0.0.0:* LISTEN 2252/mongod

1) Enter one of the Mongodb settings, this article takes 27017 as an example

mongo enters the database and is set as follows

>cfg={"_id":"repl","members":

[{"_id":0,"host":"192.168.60.135:27017","priority":100},

{"_id":1,"host":"192.168.60.135:27018","priority":100},

{"_id":2,"host":"192.168.60.135:27019","priority":0},

{"_id":3,"host":"192.168.60.135:27020","arbiterOnly":true}]}

cfg is just a name, similar to a variable. Set priorities 27017 and 27018 to 100,

27019 has a priority of 0 and is not eligible for election, and 27020 is set to arbite and is an arbitration copy set.

> rs. initialize (cfg) #Initialize

{ #prompt the following information

"ok" : 1,

"operationTime" : Timestamp(1536851525, 1),

"$clusterTime" : {

"clusterTime" : Timestamp(1536851525, 1),

"signature" : {

"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),

"keyId" : NumberLong(0)

}

}

}

2) View status

repl:PRIMARY> rs.status()

27017 is selected as the standard node, 27018 has voting rights but already has a standard node, so it becomes a passive node.

27019 is selected as the passive node and 27020 is set as the arbiter node.

IV. Verification

1)Verify that standard nodes preempt each other

repl:PRIMARY> rs.stepDown() #voluntarily relinquishes primary replication rights

2)Verify passive nodes do not preempt

[root@localhost ~]# mongod -f /etc/mongod.conf --shutdown

killing process with pid: 2224

[root@localhost ~]# mongod -f /etc/mongod2.conf --shutdown

killing process with pid: 2252

Closed MongoDB for 27017 and 27018

[root@localhost ~]# mongo -port 27019 #Enter mongodb port 27019

>rs.status()

Conclusion: When two standard nodes are down, passive nodes will not replace them as standard nodes because they have no voting rights.

3) Verify that the arbitration server does not store data

Open MongoDB port 27017

[root@localhost ~]# mongod -f /etc/mongod.conf

mongo #landing

use list; #Create a database

db.list.insert({"name":"zhangsan"}) Create a list collection and insert information

repl:PRIMARY> db.list.find(); query list

{ "_id" : ObjectId("5b9a8ae579c4aae2378a4fd6"), "name" : "zhangsan" }

Then we switch to 27020 arbitration server

[root@localhost ~]# mongo -port 27020

rs.slaveOk() #Output this command first, otherwise it cannot be viewed

repl:ARBITER> show dbs; #View database

local 0.000GB #Only one database can be seen local

Conclusion: The arbitration server is only responsible for voting and does not participate in data storage

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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report