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.
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.
Principle explanation
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_mongo
the 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