The most complete mongodb builds a replication set in a docker container


Build a mongodb replication set in docker to provide data redundancy and high availability, provide a certain degree of fault tolerance, and prevent the loss of a single database.

Create the master node

Create the mount directory and enter
mkdir -p /home/docker/test/node-p
cd /home/docker/test/node-p
Create data mount directory
mkdir db
Generate mongodb-keyfile file
openssl rand -base64 741 > mongodb-keyfile
Set file permissions
sudo chmod 600 mongodb-keyfile
sudo chown 999 mongodb-keyfile
Start the master node
docker run -d --name node-p -p 27027:27017 \
  -v /home/docker/test/node-p/db:/data/db \
  -v /home/docker/test/node-p/mongodb-keyfile:/opt/mongodb-keyfile \
  -v /etc/localtime:/etc/localtimmke:ro \
  --privileged=true \
  --restart=always \
  -e MONGO_INITDB_ROOT_USERNAME=root \
  -e MONGO_INITDB_ROOT_PASSWORD=mfweah0rGxyOH49A \
  mongo:4.2.6 --auth --keyFile /opt/mongodb-keyfile --oplogSize 102400 --wiredTigerCacheSizeGB 16 --replSet "cmdhelp" 
Enter the main node container
docker exec -it node-p /bin/bash
Verify permissions
mongo
use admin
db.auth('root','mfweah0rGxyOH49A')
Initialize the current node, configure the weight to 5
rs.initiate({ _id:"cmdhelp",members:[{_id:0,host:"192.168.2.234:27027", priority: 5}]})   

Create arbitration node

Create the mount directory and enter
mkdir -p /home/docker/test/node-m
cd /home/docker/test/node-m
Create data mount directory
mkdir db
Copy the mongodb-keyfile file of the master node (copy according to your actual path)
scp /home/docker/test/node-p/mongodb-keyfile .
Set file permissions
sudo chmod 600 mongodb-keyfile
sudo chown 999 mongodb-keyfile
Start the arbitration node
docker run -d --name node-m -p 27028:27017 \
  -v /home/docker/test/node-m/db:/data/db \
  -v /home/docker/test/node-m/mongodb-keyfile:/opt/mongodb-keyfile \
  -v /etc/localtime:/etc/localtimmke:ro \
  --privileged=true \
  --restart=always \
  -e MONGO_INITDB_ROOT_USERNAME=root \
  -e MONGO_INITDB_ROOT_PASSWORD=mfweah0rGxyOH49A \
  mongo:4.2.6 --auth --keyFile /opt/mongodb-keyfile --oplogSize 102400 --wiredTigerCacheSizeGB 1 --replSet "cmdhelp" 

Create slave node

Create the mount directory and enter
mkdir -p /home/docker/test/node-s
cd /home/docker/test/node-s
Create data mount directory
mkdir db
Copy the mongodb-keyfile file of the master node (copy according to your actual path)
scp /home/docker/test/node-p/mongodb-keyfile .
Set file permissions
sudo chmod 600 mongodb-keyfile
sudo chown 999 mongodb-keyfile
Start slave node
docker run -d --name node-s -p 27029:27017 \
  -v /home/docker/test/node-s/db:/data/db \
  -v /home/docker/test/node-s/mongodb-keyfile:/opt/mongodb-keyfile \
  -v /etc/localtime:/etc/localtimmke:ro \
  --privileged=true \
  --restart=always \
  -e MONGO_INITDB_ROOT_USERNAME=root \
  -e MONGO_INITDB_ROOT_PASSWORD=mfweah0rGxyOH49A \
  mongo:4.2.6 --auth --keyFile /opt/mongodb-keyfile --oplogSize 102400 --wiredTigerCacheSizeGB 16 --replSet "cmdhelp" 

Configure arbitration node and slave node

Enter the main node container
docker exec -it node-p /bin/bash
Management authority verification
mongo
use admin
db.auth('root','mfweah0rGxyOH49A')
Configure arbitration node
rs.addArb("192.168.2.234:27028")
Configure slave node
rs.add("192.168.2.234:27029")
View node status
rs.status()
ps:
  •  mongodb-keyfile needs to be maintained and permissions need to be configured
  •  This test is on a single machine, and multiple machines can replace the corresponding ip
Was this helpful?

I hope to help every developer quickly find the commands they need. Of course, you can also add your own commonly used commands for easy searching.