一、基本概念
1、副本集:一个副本集就是一组MongoDB实例组成的集群,由一个主(Primary)服务器和多个备份(Secondary)服务器构成
2、主节点(master):主节点接收所有写入操作。主节点将对其数据集所做的所有更改记录到其 oplog。
3、副节点(secondary):复制主节点的 oplog 并将操作应用到其数据集,如果主节点不可用,一个合格的副节点将被选为新的主节点。
4、仲裁节点(arbiter):负载选举,当主节点不可用,它将从副节点中选一个作为主节点。
二、部署副本集
1、下载monodb镜像,我这里版本为:3.2.11
2、创建三个mongodb的数据目录:
mkdir -p /home/mongodb-cluster/data/master mkdir -p /home/mongodb-cluster/data/secondary mkdir -p /home/mongodb-cluster/data/arbiter
3、编写docker-compose.yml
version: '2.1' services: master: image: mongodb:3.2.11 container_name: master restart: always ports: - 27017:27017 volumes: - ./data/master:/data/db command: mongod --dbpath /data/db --replSet testSet --oplogSize 128 environment: MONGO_INITDB_ROOT_USERNAME: root MONGO_INITDB_ROOT_PASSWORD: 12345678 secondary: image: mongodb:3.2.11 container_name: secondary restart: always ports: - 27018:27017 volumes: - ./data/secondary:/data/db command: mongod --dbpath /data/db --replSet testSet --oplogSize 128 environment: MONGO_INITDB_ROOT_USERNAME: root MONGO_INITDB_ROOT_PASSWORD: 12345678 arbiter: image: mongodb:3.2.11 container_name: arbiter restart: always volumes: - ./data/arbiter:/data/db command: mongod --replSet testSet --smallfiles --oplogSize 128
4、启动
docker-compse up -d
5、配置副本集
1)进入容器内部:
docker exec -it master mongo
2)在mongo shell里面执行:rs.initiate()
3)继续执行:rs.add('secondary:27017') 表示将secondary添加进副本集
4)继续执行:rs.add('arbiter:27017',true) 其中true表示这个节点是仲裁节点
5)查看配置
testSet:PRIMARY> rs.conf() { "_id" : "testSet", "version" : 3, "protocolVersion" : NumberLong(1), "members" : [ { "_id" : 0, "host" : "9c3bf874c72c:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 }, { "_id" : 1, "host" : "secondary:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 }, { "_id" : 2, "host" : "arbiter:27017", "arbiterOnly" : true, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 } ], "settings" : { "chainingAllowed" : true, "heartbeatIntervalMillis" : 2000, "heartbeatTimeoutSecs" : 10, "electionTimeoutMillis" : 10000, "getLastErrorModes" : { }, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 }, "replicaSetId" : ObjectId("5c6fb85fb21aeac107319862") } }
6)查看状态
testSet:PRIMARY> rs.status() { "set" : "testSet", "date" : ISODate("2019-02-22T08:58:02.662Z"), "myState" : 1, "term" : NumberLong(1), "heartbeatIntervalMillis" : NumberLong(2000), "members" : [ { "_id" : 0, "name" : "9c3bf874c72c:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 511, "optime" : { "ts" : Timestamp(1550825820, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2019-02-22T08:57:00Z"), "electionTime" : Timestamp(1550825567, 2), "electionDate" : ISODate("2019-02-22T08:52:47Z"), "configVersion" : 3, "self" : true }, { "_id" : 1, "name" : "secondary:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 117, "optime" : { "ts" : Timestamp(1550825820, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2019-02-22T08:57:00Z"), "lastHeartbeat" : ISODate("2019-02-22T08:58:02.542Z"), "lastHeartbeatRecv" : ISODate("2019-02-22T08:58:01.545Z"), "pingMs" : NumberLong(0), "syncingTo" : "9c3bf874c72c:27017", "configVersion" : 3 }, { "_id" : 2, "name" : "arbiter:27017", "health" : 1, "state" : 7, "stateStr" : "ARBITER", "uptime" : 62, "lastHeartbeat" : ISODate("2019-02-22T08:58:02.542Z"), "lastHeartbeatRecv" : ISODate("2019-02-22T08:58:00.599Z"), "pingMs" : NumberLong(0), "configVersion" : 3 } ], "ok" : 1 }
三、验证mongdb可用性:
由上测试可知:副节点只能读,不能写