Docker 部署方式
-
拉取鏡像
docker pull mongo:3.2
-
配置mongo 啟動參數(shù)
在宿主機(jī)上創(chuàng)建 /opt/mongo/data 和 /opt/mongo/conf 目錄
-
在/opt/mongo/conf 目錄下新建 mongo.yaml 配置文件
storage: dbPath: /data/db engine: wiredTiger wiredTiger: engineConfig: cacheSizeGB: 1 replication: replSetName: rs oplogSizeMB: 100 security: keyFile: /data/configdb/authkey authorization: disabled clusterAuthMode: keyFile
-
在/opt/mongo/conf 目錄下新建 authKey 文件(后面/opt/mongo/conf會掛載到容器的 /data/configdb)
openssl rand -base64 666 > /opt/mongo/conf/authkey chmod 600 /opt/mongo/conf/authkey
將這幾個配置文件同步到幾臺節(jié)點(diǎn)上
-
以非驗(yàn)證方式啟動(為了可以創(chuàng)建用戶),將上面的 security 注釋掉
docker run --name mongo -p 27017:27017 -v /opt/mongo/conf:/data/configdb -v /opt/mongo/data:/data/db -d mongo:3.2 -f /data/configdb/mongo.yaml
-f 是指定相關(guān)的配置文件
在幾臺服務(wù)器都啟動對應(yīng)的Mongo站點(diǎn)
通過
docker logs mongo
查看日志輸出到此Mongo已經(jīng)啟動了
但并不是以副本集方式進(jìn)行通信的
-
初始化Mongo副本集
登錄以上任意一臺副本集,執(zhí)行初始化
var config={ _id:"rs", members:[ {_id:0,host:"192.168.136.129:27017"}, {_id:1,host:"192.168.136.129:27018"} ]}; rs.initiate(config);
同時查看
rs.status()
,可以看到 已經(jīng)有兩臺服務(wù)器加入其中了{ "set" : "rs", "date" : ISODate("2021-08-19T04:26:34.875+0000"), "myState" : NumberInt(1), "term" : NumberLong(1), "heartbeatIntervalMillis" : NumberLong(2000), "members" : [ { "_id" : NumberInt(0), "name" : "192.168.136.129:27017", "health" : NumberInt(1), "state" : NumberInt(1), "stateStr" : "PRIMARY", "uptime" : NumberInt(78), "optime" : { "ts" : Timestamp(1629347194, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2021-08-19T04:26:34.000+0000"), "infoMessage" : "could not find member to sync from", "electionTime" : Timestamp(1629347193, 1), "electionDate" : ISODate("2021-08-19T04:26:33.000+0000"), "configVersion" : NumberInt(1), "self" : true }, { "_id" : NumberInt(1), "name" : "192.168.136.129:27018", "health" : NumberInt(1), "state" : NumberInt(2), "stateStr" : "SECONDARY", "uptime" : NumberInt(11), "optime" : { "ts" : Timestamp(1629347183, 1), "t" : NumberLong(-1) }, "optimeDate" : ISODate("2021-08-19T04:26:23.000+0000"), "lastHeartbeat" : ISODate("2021-08-19T04:26:33.731+0000"), "lastHeartbeatRecv" : ISODate("2021-08-19T04:26:31.490+0000"), "pingMs" : NumberLong(0), "configVersion" : NumberInt(1) } ], "ok" : NumberInt(1) }
但此時熄求,如果Primary 掛掉了翘瓮,另一臺并不會升級成 Primary,因?yàn)樯僖慌_仲裁的節(jié)點(diǎn)
-
在增加仲裁節(jié)點(diǎn)前,先創(chuàng)建一個root用戶锹锰,便于后面開啟mongo的鑒權(quán)(連接副本集)
db.createUser({ user: 'root', pwd: '123456', roles: [ { role: "root", db: "admin" } ] });
創(chuàng)建好用戶后,可以將節(jié)點(diǎn)配置文件中的 authkey 注釋去掉
啟動mongo
-
同樣執(zhí)行步驟4漓库,創(chuàng)建一個mongo節(jié)點(diǎn)恃慧,執(zhí)行以下方法增加 仲裁節(jié)點(diǎn)
rs.addArb("192.168.136.129:27019")
{ "set" : "rs", "date" : ISODate("2021-08-19T05:01:36.773+0000"), "myState" : NumberInt(1), "term" : NumberLong(4), "heartbeatIntervalMillis" : NumberLong(2000), "members" : [ { "_id" : NumberInt(0), "name" : "192.168.136.129:27017", "health" : NumberInt(1), "state" : NumberInt(1), "stateStr" : "PRIMARY", # 讀寫 "uptime" : NumberInt(1142), "optime" : { "ts" : Timestamp(1629348176, 1), "t" : NumberLong(4) }, "optimeDate" : ISODate("2021-08-19T04:42:56.000+0000"), "electionTime" : Timestamp(1629348175, 1), "electionDate" : ISODate("2021-08-19T04:42:55.000+0000"), "configVersion" : NumberInt(2), "self" : true }, { "_id" : NumberInt(1), "name" : "192.168.136.129:27018", "health" : NumberInt(1), "state" : NumberInt(2), "stateStr" : "SECONDARY", # 只讀 "uptime" : NumberInt(1103), "optime" : { "ts" : Timestamp(1629348176, 1), "t" : NumberLong(4) }, "optimeDate" : ISODate("2021-08-19T04:42:56.000+0000"), "lastHeartbeat" : ISODate("2021-08-19T05:01:35.180+0000"), "lastHeartbeatRecv" : ISODate("2021-08-19T05:01:35.281+0000"), "pingMs" : NumberLong(0), "syncingTo" : "192.168.136.129:27017", "configVersion" : NumberInt(2) }, { "_id" : NumberInt(2), "name" : "192.168.136.129:27019", "health" : NumberInt(1), "state" : NumberInt(7), "stateStr" : "ARBITER", # 仲裁節(jié)點(diǎn) "uptime" : NumberInt(1141), "lastHeartbeat" : ISODate("2021-08-19T05:01:35.180+0000"), "lastHeartbeatRecv" : ISODate("2021-08-19T05:01:35.838+0000"), "pingMs" : NumberLong(0), "configVersion" : NumberInt(2) } ], "ok" : NumberInt(1) }
這時,如果master 節(jié)點(diǎn)掉線了渺蒿,仲裁節(jié)點(diǎn)會進(jìn)行選舉
Docker-compose 方式
version: "3"
services:
mongo_master_service:
image: mongo:3.2
networks:
mongo_service_net:
ports:
- "27017:27017"
volumes:
- /opt/mongo/conf:/data/configdb
- /opt/mongo/data:/data/db
command: mongod -f /data/configdb/mongo.yaml
mongo_secondary_service:
image: mongo:3.2
networks:
mongo_service_net:
ports:
- "27018:27017"
volumes:
- /opt/mongo1/conf:/data/configdb
- /opt/mongo1/data:/data/db
command: mongod -f /data/configdb/mongo.yaml
mongo_arbt_service:
image: mongo:3.2
networks:
mongo_service_net:
ports:
- "27019:27017"
volumes:
- /opt/mongo2/conf:/data/configdb
- /opt/mongo2/data:/data/db
command: mongod -f /data/configdb/mongo.yaml
networks:
mongo_service_net:
ipam:
driver: default
docker-compose up -d