Docker 部署方式
-
拉取镜像
docker pull mongo:3.2
-
配置mongo 启动参数
在宿主机上创建 /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
将这几个配置文件同步到几台节点上
-
以非验证方式启动(为了可以创建用户),将上面的 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 是指定相关的配置文件
在几台服务器都启动对应的Mongo站点
通过
docker logs mongo
查看日志输出到此Mongo已经启动了
但并不是以副本集方式进行通信的
-
初始化Mongo副本集
登录以上任意一台副本集,执行初始化
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()
,可以看到 已经有两台服务器加入其中了{ "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,因为少一台仲裁的节点
-
在增加仲裁节点前,先创建一个root用户,便于后面开启mongo的鉴权(连接副本集)
db.createUser({ user: 'root', pwd: '123456', roles: [ { role: "root", db: "admin" } ] });
创建好用户后,可以将节点配置文件中的 authkey 注释去掉
启动mongo
-
同样执行步骤4,创建一个mongo节点,执行以下方法增加 仲裁节点
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", # 仲裁节点 "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 节点掉线了,仲裁节点会进行选举
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