前言

去年记录过以普通方式搭建副本集,今年以Docker方式搭建,简化搭建过程。此文仅为记录。

过程

Dokcer安装

MongoDB版本为4.0.13Docker安装过程略。

启动容器

docker run --name mongo0 -p 37017:27017 -d mongo:4.0.13 --replSet "gps"

docker run --name mongo1 -p 47017:27017 -d mongo:4.0.13 --replSet "gps"

docker run --name mongo2 -p 57017:27017 -d mongo:4.0.13 --replSet "gps"

进入其中一个容器

docker exec -it mongo0 /bin/bash

连接mongo

mongo --host 192.168.1.158 --port 37017

进行副本集配置

其中一个为仲裁节点

var config = { _id: "gps", members:[ {_id:0, host:"192.168.1.158:37017"}, {_id:1, host:"192.168.1.158:47017"}, {_id:2, host:"192.168.1.158:57017", arbiterOnly:true}]};
rs.initiate(config)

成功之后会出现类似的结果:

{
	"ok" : 1,
	"operationTime" : Timestamp(1579059713, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1579059713, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}

同时命令行左边会由>变为:

gps:SECONDARY>

查看副本集状态

注意副本集使用的是status, 容易和db.stats()混淆打错。配置成功的话members属性中应该是有三个节点:主、从、仲裁节点。

rs.status()
{
    "set" : "gps",
    "date" : ISODate("2020-01-15T07:17:02.299Z"),
    "myState" : 1,
    "term" : NumberLong(1),
    "syncingTo" : "",
    "syncSourceHost" : "",
    "syncSourceId" : -1,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1579072617, 1),
            "t" : NumberLong(1)
        },
        "readConcernMajorityOpTime" : {
            "ts" : Timestamp(1579072617, 1),
            "t" : NumberLong(1)
        },
        "appliedOpTime" : {
            "ts" : Timestamp(1579072617, 1),
            "t" : NumberLong(1)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1579072617, 1),
            "t" : NumberLong(1)
        }
    },
    "lastStableCheckpointTimestamp" : Timestamp(1579072597, 2),
    "members" : [ 
        {
            "_id" : 0,
            "name" : "192.168.1.158:37017",
            "health" : 1.0,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 4840,
            "optime" : {
                "ts" : Timestamp(1579072617, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2020-01-15T07:16:57.000Z"),
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "electionTime" : Timestamp(1579068396, 1),
            "electionDate" : ISODate("2020-01-15T06:06:36.000Z"),
            "configVersion" : 1,
            "self" : true,
            "lastHeartbeatMessage" : ""
        }, 
        {
            "_id" : 1,
            "name" : "192.168.1.158:47017",
            "health" : 1.0,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 4236,
            "optime" : {
                "ts" : Timestamp(1579072617, 1),
                "t" : NumberLong(1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1579072617, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2020-01-15T07:16:57.000Z"),
            "optimeDurableDate" : ISODate("2020-01-15T07:16:57.000Z"),
            "lastHeartbeat" : ISODate("2020-01-15T07:17:01.413Z"),
            "lastHeartbeatRecv" : ISODate("2020-01-15T07:17:01.399Z"),
            "pingMs" : NumberLong(3),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "192.168.1.158:37017",
            "syncSourceHost" : "192.168.1.158:37017",
            "syncSourceId" : 0,
            "infoMessage" : "",
            "configVersion" : 1
        }, 
        {
            "_id" : 2,
            "name" : "192.168.1.158:57017",
            "health" : 1.0,
            "state" : 7,
            "stateStr" : "ARBITER",
            "uptime" : 4236,
            "lastHeartbeat" : ISODate("2020-01-15T07:17:01.434Z"),
            "lastHeartbeatRecv" : ISODate("2020-01-15T07:17:00.430Z"),
            "pingMs" : NumberLong(3),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "configVersion" : 1
        }
    ],
    "ok" : 1.0,
    "operationTime" : Timestamp(1579072617, 1),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1579072617, 1),
        "signature" : {
            "hash" : { "$binary" : "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "$type" : "00" },
            "keyId" : NumberLong(0)
        }
    }
}

MongoTemplate连接副本集

格式:

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

例子:

mongodb://username:password@192.168.1.158:37017,192.168.1.158:47017/test-gps?replicaSet=gps&authSource=test-gps&readPreference=secondaryPreferred&safe=true&authMechanism=SCRAM-SHA-1&maxPoolSize=500&minPoolSize=10

replicaSet:副本集名
authSource:使用哪个数据进行认证
safe=true:在执行更新操作之后,驱动都会发送getLastError命令来确保更新成功
readPreference:

  • primary
    主节点,默认模式,读操作只在主节点,如果主节点不可用,报错或者抛出异常。
  • primaryPreferred
    首选主节点,大多情况下读操作在主节点,如果主节点不可用,如故障转移,读操作在从节点。
  • secondary
    从节点,读操作只在从节点, 如果从节点不可用,报错或者抛出异常。
  • secondaryPreferred
    首选从节点,大多情况下读操作在从节点,特殊情况(如单主节点架构)读操作在主节点。
  • nearest
    最邻近节点,读操作在最邻近的成员,可能是主节点或者从节点。

记得设置好用户权限!