基于副本集模式的docker搭建mongodb集群

1.拉取镜像

docker pull mongo

2.创建容器,设置副本集名称: mongo_clus(必须)

docker run -di --name=master_mongo -p 27018:27017 mongo:latest --replSet mongo_clus

docker run -di --name=backup_mongo -p 27019:27017 mongo:latest --replSet mongo_clus

docker run -di --name=arbi_mongo -p 27020:27017 mongo:latest  --replSet mongo_clus

3.登录master_mongo,创建集群,查看集群状态。

docker exec  -it master_mongo /bin/bash

mongo --host 192.168.222.128 --port 27018

cfg={ "_id":"mongo_clus", members:[ { _id:0, host:"192.168.222.128:27018", priority:2 }, { _id:1, host:"192.168.222.128:27019", priority:1 }, { _id:2, host:"192.168.222.128:27020", arbiterOnly:true } ] }

rs.initiate(cfg)

rs.status()

4.使用nosqlbooster连接测试,插入测试数据

use studies
db.createCollection("person", { capped : true, size : 50 * 1024 * 1024, max : 100 * 1000 } )
db.person.insert({name:"杨春雷",age:30})

(2)单独连接备份库是否有数据

5.关闭开启master_mongo查看主备切换是否正常

docker stop master_mongo
docker start master_mongo