MongoDB复制集:概念和实现

2023-04-15 00:00:00 mongodb 复制 概念

MongoDB复制集是一个由多个MongoDB实例组成的集群,其中一个节点被指定为主节点(primary),其他节点都是从节点(secondary)。主节点负责接收所有的写请求,并将它们同步到从节点,而从节点则只能读取数据,不能进行写入操作。复制集的目的是提高可用性和可扩展性。

实现一个MongoDB复制集需要以下步骤:

  1. 配置MongoDB实例的replication选项,启用复制功能。在配置文件mongod.conf中添加以下行:
replication:
    replSetName: <your_replset_name>
  1. 启动MongoDB实例并进入Mongo shell,使用rs.initiate()初始化复制集。例如,以“pidancode.com”作为副本集名称:
rs.initiate({
    _id: "pidancode",
    members: [
        {_id: 0, host: "<primary_host>:<primary_port>"},
        {_id: 1, host: "<secondary_host>:<secondary_port>"},
        {_id: 2, host: "<secondary_host>:<secondary_port>"}
    ]
})

在这个例子中,我们指定了三个节点,其中第一个是主节点(primary),后面两个是从节点(secondary)。

  1. 检查复制集状态,确保它已经成功启动。使用rs.status()命令可以查看当前的节点状态和复制集信息。
rs.status()

{
    "set" : "pidancode",
    "date" : ISODate("2021-08-31T03:49:05.782Z"),
    "myState" : 1,
    "term" : NumberLong(1),
    "syncingTo" : "",
    "syncSourceHost" : "",
    "syncSourceId" : -1,
    "heartbeatIntervalMillis" : 2000,
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1630378136, 1),
            "t" : NumberLong(1)
        },
        "readConcernMajorityOpTime" : {
            "ts" : Timestamp(1630378136, 1),
            "t" : NumberLong(1)
        },
        "appliedOpTime" : {
            "ts" : Timestamp(1630378136, 1),
            "t" : NumberLong(1)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1630378136, 1),
            "t" : NumberLong(1)
        }
    },
    "members" : [
        {
            "_id" : 0,
            "name" : "<primary_host>:<primary_port>",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 1217,
            "optime" : {
                "ts" : Timestamp(1630378136, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2021-08-31T03:42:16Z"),
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "electionTime" : Timestamp(1630376955, 1),
            "electionDate" : ISODate("2021-08-31T03:22:35Z"),
            "configVersion" : 1,
            "self" : true,
            "lastHeartbeatMessage" : ""
        },
        {
            "_id" : 1,
            "name" : "<secondary_host>:<secondary_port>",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 1133,
            "optime" : {
                "ts" : Timestamp(1630378136, 1),
                "t" : NumberLong(1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1630378136, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2021-08-31T03:42:16Z"),
            "optimeDurableDate" : ISODate("2021-08-31T03:42:16Z"),
            "lastHeartbeat" : ISODate("2021-08-31T03:49:04.923Z"),
            "lastHeartbeatRecv" : ISODate("2021-08-31T03:49:04.223Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "<primary_host>:<primary_port>",
            "syncSourceHost" : "<primary_host>:<primary_port>",
            "syncSourceId" : 0,
            "infoMessage" : "",
            "configVersion" : 1
        },
        {
            "_id" : 2,
            "name" : "<secondary_host>:<secondary_port>",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 1133,
            "optime" : {
                "ts" : Timestamp(1630378136, 1),
                "t" : NumberLong(1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1630378136, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2021-08-31T03:42:16Z"),
            "optimeDurableDate" : ISODate("2021-08-31T03:42:16Z"),
            "lastHeartbeat" : ISODate("2021-08-31T03:49:04.927Z"),
            "lastHeartbeatRecv" : ISODate("2021-08-31T03:49:04.228Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "<primary_host>:<primary_port>",
            "syncSourceHost" : "<primary_host>:<primary_port>",
            "syncSourceId" : 0,
            "infoMessage" : "",
            "configVersion" : 1
        }
    ],
    "ok" : 1,
    "$clusterTime" : {
        "clusterTime" : Timestamp(1630378136, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    },
    "operationTime" : Timestamp(1630378136, 1)
}

在这个例子中,我们可以看到当前节点的状态。主节点的state为1,从节点的state为2。

通过以上步骤,我们已经可以创建一个MongoDB复制集。接下来,在实际应用中,我们可以通过Java、Python或其他编程语言来实现MongoDB的读写操作,从而实现高可用性的分布式应用。

相关文章