docker-mongo复制集群
docker-compose.yml
配置:
version: '3'
services:
mongo1:
container_name: mongo1
image: mongo:latest
restart: always
ports:
- '37017:27017'
environment:
- TZ=Asia/Shanghai
volumes:
- $PWD/mongo1/data:/data/db
command: mongod --replSet rs
mongo2:
container_name: mongo2
image: mongo:latest
restart: always
ports:
- '37018:27017'
environment:
- TZ=Asia/Shanghai
volumes:
- $PWD/mongo2/data:/data/db
command: mongod --replSet rs
mongo3:
container_name: mongo3
image: mongo:latest
restart: always
ports:
- '37019:27017'
environment:
- TZ=Asia/Shanghai
volumes:
- $PWD/mongo3/data:/data/db
command: mongod --replSet rs
创建集群
rs.initiate({_id:'rs',members:[{_id:0,host:'ip1:37017'},{_id:1,host:'ip2:37018'},{_id:2,host:'ip3:37019'}]})
创建用户
db.createUser({user:"admin", pwd:"pwd", roles:[{role: "userAdminAnyDatabase", db:"admin" }]})
允许复本查询
默认情况下复本是不允许查询的
rs.slaveOk()
查看集群状态
rs.status()