MongoDB replica cluster-building

replica cluster

For small and medium-sized projects, using a replica cluster is sufficient.

It consists of one master and two slave databases. When the master database goes down, both slave databases can be selected as the master database.

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-iYoSBEQ8-1683450976699)(assets/1190037-20180106145148128-1854811460.png)]

When the main database goes down, both slave databases will run for election, and one of them will become the master database. When the original master database is restored, it can join the current replication cluster as a slave database.

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-TMFhiiZe-1683450976701)(assets/1190037-20180106145154284-397901575.png)]

Principle explanation

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-HVTvIEBT-1683450976701) (assets/image-20210109222831263.png)]

Master: represents the master node, which provides CRUD services for all data

Backup: represents the slave node, which does not provide any services

Arbitration: represents the arbitration node. The arbitration node does not store any data. Its main function is to promote the backup node to a master node after the master node hangs up.

Cluster construction

Create container

docker run -di --name=master_mongo  -p 27017:27017 mongo:4.0.3  --replSet mongo_clus 
docker run -di --name=backup_mongo1 -p 27018:27017 mongo:4.0.3  --replSet mongo_clus 
docker run -di --name=backup_mongo2 -p 27019:27017 mongo:4.0.3  --replSet mongo_clus

Set the replica set name, that is, set the cluster name. It must be set, otherwise the cluster cannot be built.

Configuration

into master_mongothe container

docker exec  -it master_mongo /bin/bash

Log inMongo

mongo -port 27017

Create a cluster

cfg={
    
    
  "_id":"mongo_clus",
  members:[{
    
    
      _id:0,
      host:"192.168.136.161:27017",
      priority:2
  },{
    
    
      _id:1,
      host:"192.168.136.161:27018",
      priority:1
  },{
    
    
      _id:2,
      host:"192.168.136.161:27019",
      priority:2
  }]
}
rs.initiate(cfg)

Pay attention to modifying the ip address

Guess you like

Origin blog.csdn.net/weixin_45417754/article/details/130545217