簡單模式
version: '2'
services:
mongo-container:
image: mongo:3.4
container_name: mongodb
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=123456
ports:
- "27017:27017"
volumes:
- "./data/mongo:/data/db"
command: mongod
單片 一主一從一仲裁
配置文件mongod.conf
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /data/db
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
#security:
# keyFile: "/etc/key.file"
#operationProfiling:
replication:
replSetName: rs0
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
version: '3.7'
services:
shard11:
hostname: shard11
container_name: shard11
image: mongo:4.0.3
restart: always
networks:
- mongo_test
command: mongod -f /data/conf/mongod.conf
privileged: true
ports:
- 19001:27017
volumes:
- /etc/localtime:/etc/localtime
- /home/data/db/testm/shard11:/data/db
- /home/data/db/testm/mongos/mongod.conf:/data/conf/mongod.conf
deploy:
placement:
constraints:
- node.hostname == manager
shard12:
hostname: shard12
container_name: shard12
image: mongo:4.0.3
restart: always
command: mongod -f /data/conf/mongod.conf
privileged: true
networks:
- mongo_test
ports:
- 19002:27017
volumes:
- /etc/localtime:/etc/localtime
- /home/data/db/testm/shard12:/data/db
- /home/data/db/testm/mongos/mongod.conf:/data/conf/mongod.conf
deploy:
placement:
constraints:
- node.hostname == manager
shard13:
hostname: shard13
container_name: shard13
image: mongo:4.0.3
restart: always
command: mongod -f /data/conf/mongod.conf
privileged: true
networks:
- mongo_test
ports:
- 19003:27017
volumes:
- /etc/localtime:/etc/localtime
- /home/data/db/testm/shard13:/data/db
- /home/data/db/testm/mongos/mongod.conf:/data/conf/mongod.conf
deploy:
placement:
constraints:
- node.hostname == manager
networks:
mongo_test:
external: true
啟動 容器后進入一個容器內
mongo
rs.initiate( {_id : "rs0",members: [{ _id: 0, host: "shard11:27017",priority:2 },{ _id: 1, host: "shard12:27017",priority:1 },{ _id: 2, host: "shard13:27017", arbiterOnly:true }]})
rs.status()
可以看到stateStr 的值都不一樣
image.png
主從模式下只有主 可以寫操作
仲裁節點甚至都不會保存數據
點波關注 系統搭建(docker)