version: '3'
services:
mongodb1:
image: mongo:4.4.6
container_name: mongodb1
networks:
yewu-network:
ipv4_address: 192.168.177.61
restart: always
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=FjcpIvWHVSfrZlry
ports:
- 27017:27017
volumes:
- ./mongodb1-data:/data/db
entrypoint:
- sh
- -euc
- |
cat <<'EOF'> /data/replica.key
koZpwlmElilPVpIm+hFeuMuz0yhz7H8DJ28HSDiZQb76EE77MbY/V47Jf2MQKTU9
AJW8L9dCcwr+iJ/nszZ4cj/xOikAITJQP7hQb+Aa+uR+iqZ0nFBMdMWozFKUxBhe
uapG/A==
EOF
chown mongodb:mongodb /data/replica.key
chmod 600 /data/replica.key
docker-entrypoint.sh mongod --replSet "rs0" --bind_ip_all --wiredTigerCacheSizeGB 2 --auth --keyFile /data/replica.key
mongodb2:
image: mongo:4.4.6
container_name: mongodb2
networks:
yewu-network:
ipv4_address: 192.168.177.62
restart: always
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=FjcpIvWHVSfrZlry
volumes:
- ./mongodb2-data:/data/db
entrypoint:
- sh
- -euc
- |
cat <<'EOF'> /data/replica.key
koZpwlmElilPVpIm+hFeuMuz0yhz7H8DJ28HSDiZQb76EE77MbY/V47Jf2MQKTU9
AJW8L9dCcwr+iJ/nszZ4cj/xOikAITJQP7hQb+Aa+uR+iqZ0nFBMdMWozFKUxBhe
uapG/A==
EOF
chown mongodb:mongodb /data/replica.key
chmod 600 /data/replica.key
docker-entrypoint.sh mongod --replSet "rs0" --bind_ip_all --wiredTigerCacheSizeGB 2 --auth --keyFile /data/replica.key
mongodb3:
image: mongo:4.4.6
container_name: mongodb3
networks:
yewu-network:
ipv4_address: 192.168.177.63
restart: always
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=FjcpIvWHVSfrZlry
volumes:
- ./mongodb3-data:/data/db
entrypoint:
- sh
- -euc
- |
cat <<'EOF'> /data/replica.key
koZpwlmElilPVpIm+hFeuMuz0yhz7H8DJ28HSDiZQb76EE77MbY/V47Jf2MQKTU9
AJW8L9dCcwr+iJ/nszZ4cj/xOikAITJQP7hQb+Aa+uR+iqZ0nFBMdMWozFKUxBhe
uapG/A==
EOF
chown mongodb:mongodb /data/replica.key
chmod 600 /data/replica.key
docker-entrypoint.sh mongod --replSet "rs0" --bind_ip_all --wiredTigerCacheSizeGB 2 --auth --keyFile /data/replica.key
networks:
yewu-network:
driver: bridge
ipam:
config:
- subnet: 192.168.177.0/24
name: yewu-network
(1)mongodb集群信息
mongo --host rs0/192.168.177.61:27017,192.168.177.62:27017,192.168.177.63:27017 -u admin -p FjcpIvWHVSfrZlry --authenticationDatabase admin
(2)部署时,首先需要生成key文件
openssl rand -base64 1024 > mongodb.key
(3)然后使用admin账号登录mongo,并生成集群信息。
mongo -u admin -p FjcpIvWHVSfrZlry
rs.initiate({
_id: "rs0",
members: [
{
_id: 0,
host: "192.168.177.61:27017"
},
{
_id: 1,
host: "192.168.177.62:27017"
},
{
_id: 2,
host: "192.168.177.63:27017"
}
]
});
(4)测试集群是否正常