Objective
Personal understanding, a primary function of a replica set is when the master library fails, and one of the salve from the library is elected to become the new master. The frame diagram is as follows:
Among them, the electors do not participate in the data storage, its role is only to elect a new master. When the original master is restored, he will only be added to the replica set as a salve from the library.
Of course, we can also manually configure the priority parameter to make it master when the restore starts.
Demo
This is illustrated by specific examples.
First set up 3 in the MONGO directory (one as master, one as a slave, one as an elector, in the actual project, these three services are finally placed on different servers) configuration file, the file content is as follows:
Master
#配置mongodb数据的存储路径
Dbpath=d:\mongodb\data\rs1
#配置mongodb日志文件
Logpath=d:\mongodb\log\rs1.log
#配置端口, if not configured, the default 27017 port
port=40001
#副本集名字, consistency is required in each configuration file
Replset=rs_test
Salve
#配置mongodb数据的存储路径dbpath =d:\mongodb\data\rs2# Configure the MongoDB log file logpath=d:\mongodb\log\rs2.log# configuration port, if not configured, The default 27017 port port=40002# replica set name, which needs to be consistent in each configuration file Replset=rs_test
Voter profile
#配置mongodb数据的存储路径
Dbpath=d:\mongodb\data\rs3
#配置mongodb日志文件
Logpath=d:\mongodb\log\rs3.log
#配置端口, if not configured, the default 27017 port
port=40003
#副本集名字, consistency is required in each configuration file
Replset=rs_test
Launch three MongoDB instances
First, in the bin directory open three shell windows, respectively, the above configuration of the three MongoDB, such as the following command:
Mongod-f d:\mongodb\rs1.conf
If all starts successfully, we will see the main information in the log file:
Waiting for connections on port 40003
Second, the connection is configured on a 40001-port MongoDB instance:
Mongo-port 40001
Initialize 40001 to master:
Rs.initiate ()
Initialize successfully, using command Rs.status () to view the status of the replica set, you will see such as:
You can see that 40001 has started as primary.
Again, execute command rs.add ("127.0.0.1:40002") added from the library.
Added electors: Rs.addarb ("127.0.0.1:40003")
Execute Rs.status () again to see the status of the replica set, you will see such as:
Verification of elections
Close the 40001 server window (downtime), perform rs.status () in the 40002 window, and you will see such as:
Finally, enable the 40001 service side again and execute Mongod-f d:\mongodb\rs1.conf under the bin
Then review the status Rs.status () again, and you can see that 40001 has been re-added to the replica set and is joined as a library, such as:
About the MongoDB copy set is introduced here, I hope to be helpful to everyone. If there is something wrong in the text, please point out.
Thank you.
mongodb3.6 replica Set (iii) first day of vacation-second article mongodb How to Do data disaster preparedness