docker-compose搭建mongoDB副本集(1主+1副+1仲裁)

Posted 波神

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker-compose搭建mongoDB副本集(1主+1副+1仲裁)相关的知识,希望对你有一定的参考价值。

一、基本概念

 1、副本集:一个副本集就是一组MongoDB实例组成的集群,由一个主(Primary)服务器和多个备份(Secondary)服务器构成

 2、主节点(master):主节点接收所有写入操作。主节点将对其数据集所做的所有更改记录到其 oplog。

 3、副节点(secondary):复制主节点的 oplog 并将操作应用到其数据集,如果主节点不可用,一个合格的副节点将被选为新的主节点。

 4、仲裁节点(arbiter):负载选举,当主节点不可用,它将从副节点中选一个作为主节点。

 

二、部署副本集

1、下载monodb镜像,我这里版本为:3.2.11

docker pull registry.cn-hangzhou.aliyuncs.com/boshen-ns/mongodb:3.2.11
docker tag docker pull registry.cn-hangzhou.aliyuncs.com/boshen-ns/mongodb:3.2.11 mongodb: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
  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
  arbiter:
    image: mongodb:3.2.11
    container_name: arbiter
    restart: always
    ports:
      - 27019:27017
    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(\'172.16.1.11:27018\')  表示将secondary添加进副本集

4)继续执行:rs.add(\'172.16.1.11:27019\',true)   其中true表示这个节点是仲裁节点

5)查看配置 

testSet:PRIMARY> rs.conf()
{
    "_id" : "testSet",
    "version" : 77284,
    "protocolVersion" : NumberLong(1),
    "members" : [
        {
            "_id" : 0,
            "host" : "172.16.1.11:27017",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 1,
            "tags" : {
                
            },
            "slaveDelay" : NumberLong(0),
            "votes" : 1
        },
        {
            "_id" : 1,
            "host" : "172.16.1.11:27018",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 1,
            "tags" : {
                
            },
            "slaveDelay" : NumberLong(0),
            "votes" : 1
        },
        {
            "_id" : 2,
            "host" : "172.16.1.11:27019",
            "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-26T02:12:48.124Z"),
    "myState" : 1,
    "term" : NumberLong(2),
    "heartbeatIntervalMillis" : NumberLong(2000),
    "members" : [
        {
            "_id" : 0,
            "name" : "172.16.1.11:27017",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 268,
            "optime" : {
                "ts" : Timestamp(1551147003, 4),
                "t" : NumberLong(2)
            },
            "optimeDate" : ISODate("2019-02-26T02:10:03Z"),
            "lastHeartbeat" : ISODate("2019-02-26T02:12:46.189Z"),
            "lastHeartbeatRecv" : ISODate("2019-02-26T02:12:46.188Z"),
            "pingMs" : NumberLong(0),
            "syncingTo" : "172.16.1.11:27018",
            "configVersion" : 77284
        },
        {
            "_id" : 1,
            "name" : "172.16.1.11:27018",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 938,
            "optime" : {
                "ts" : Timestamp(1551147003, 4),
                "t" : NumberLong(2)
            },
            "optimeDate" : ISODate("2019-02-26T02:10:03Z"),
            "electionTime" : Timestamp(1551146242, 1),
            "electionDate" : ISODate("2019-02-26T01:57:22Z"),
            "configVersion" : 77284,
            "self" : true
        },
        {
            "_id" : 2,
            "name" : "172.16.1.11:27019",
            "health" : 1,
            "state" : 7,
            "stateStr" : "ARBITER",
            "uptime" : 270,
            "lastHeartbeat" : ISODate("2019-02-26T02:12:46.189Z"),
            "lastHeartbeatRecv" : ISODate("2019-02-26T02:12:48.118Z"),
            "pingMs" : NumberLong(0),
            "configVersion" : 77284
        }
    ],
    "ok" : 1
}

如果想要修改副本集的配置,可以采用以下方法:

conf=rs.conf()
conf.members[0].host="172.16.1.11:27017"
rs.reconfig(conf,{"force":true})

 

三、验证mongdb可用性:

 由上测试可知:副节点只能读,不能写

 

二、windows客户端连接mongodb

1、登录mongo shell,创建一个admin用户

testSet:PRIMARY> db.createUser({user:"admin",pwd:"admin",roles:[{"role":"userAdminAnyDatabase","db":"admin"},{"role":"readWrite","db":"testdb"}]})
Successfully added user: {
    "user" : "admin",
    "roles" : [
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        },
        {
            "role" : "readWrite",
            "db" : "testdb"
        }
    ]
}

2、安装 robomongo-1.0.0-windows-x86_64-89f24ea.exe

3、填写连接信息

 

以上是关于docker-compose搭建mongoDB副本集(1主+1副+1仲裁)的主要内容,如果未能解决你的问题,请参考以下文章

mongodb副本集搭建

mongodb副本集介绍mongodb副本集搭建mongodb副本集测试

window搭建mongodb副本集

mongodb----副本集搭建及故障自动切换

搭建MongoDB的副本集

MongoDB 副本集搭建