1. 本示例基于Centos 7, 三台主机的ip分别为:
主机一:10.21.14.164
主机二:10.21.14.165
主机三:10.21.14.166
2. 主机上建立挂载目录和redis配置文件:
cd /app
mkdir -p mongo/data
3. 主机一运行MongoDB:
docker run --name mongo -v /app/mongo/data:/data/db -p 27017:27017 -d mongo --replSet "rs"
4. 主机二运行MongoDB:
docker run --name mongo -v /app/mongo/data:/data/db -p 27017:27017 -d mongo --replSet "rs"
5. 主机三运行MongoDB:
docker run --name mongo -v /app/mongo/data:/data/db -p 27017:27017 -d mongo --replSet "rs"
6. 进入主机一容器:
docker exec -it mongo /bin/bash
./mongo 10.21.14.164:27017
>use admin
>config = { "_id": "rs", "members": [{ "_id": 0, "host": "10.21.14.164:27017", "priority": 2 }, { "_id": 1, "host": "10.21.14.165:27017", "priority": 1 }, { "_id": 2, "host": "10.21.14.166:27017", "arbiterOnly":true }] }
>rs.initiate(config)
>rs.status()
6. 主机一运行Mongo-express容器:
docker run -d -it --rm --name mongo-express -p 18081:8081 -e ME_CONFIG_MONGODB_SERVER="10.21.14.164" -e ME_CONFIG_MONGODB_PORT="27017" mongo-express