部署MongoDB复制集群(最全)

2022-11-22 16:42:05 浏览数 (1)

集群简介

在生产环境中,MongoDB 经常会部署成一个三节点的复制集,或者一个分片集群。我们先来看左边。当 MongoDB 部署为一个复制集时,应用程序通过驱动,直接请求复制集中的主节点,完成读写操作。另外两个从节点,会自动和主节点同步,保持数据的更新。在集群运行的过程中,万一主节点遇到故障,两个从节点会在几秒的时间内选举出新的主节点,继续支持应用的读写操作。

我们再来看右边,当 MongoDB 被部署为一个分片集群时,应用程序通过驱动,访问路由节点,也就是 Mongos 节点 Mongos 节点会根据读写操作中的片键值,把读写操作分发的特定的分片执行,然后把分片的执行结果合并,返回给应用程序。那集群中的数据是如何分布的呢?这些元数据记录在 Config Server 中,这也是一个高可用的复制集。每个分片管理集群中整体数据的一部分,也是一个高可用复制集。此外,路由节点,也就是 Mongos 节点在生产环境通常部署多个。这样,整个分片集群没有任何单点故障。

在这里插入图片描述在这里插入图片描述

1.下载

https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-5.0.14.tgz

2.解压

代码语言:txt复制
mongodb-linux-x86_64-rhel70-5.0.14.tgz

3.创建复制集群

代码语言:txt复制
[root@10-0-42-211 mongodb]# mkdir rs1 rs2 rs3
[root@10-0-42-211 mongodb]# /home/mongodb/mongodb/bin/mongod --replSet rs --dbpath /data/mongodb/rs1 --port 27017 --fork --logpath ./rs1/mongod.log --bind_ip_all
about to fork child process, waiting until server is ready for connections.
forked process: 843
child process started successfully, parent exiting
[root@10-0-42-211 mongodb]# /home/mongodb/mongodb/bin/mongod --replSet rs --dbpath /data/mongodb/rs2 --port 27018 --fork --logpath ./rs2/mongod.log --bind_ip_all
about to fork child process, waiting until server is ready for connections.
forked process: 906
child process started successfully, parent exiting
[root@10-0-42-211 mongodb]# /home/mongodb/mongodb/bin/mongod --replSet rs --dbpath /data/mongodb/rs3 --port 27019 --fork --logpath ./rs3/mongod.log --bind_ip_all
about to fork child process, waiting until server is ready for connections.
forked process: 967
child process started successfully, parent exiting

4.查看端口情况

代码语言:txt复制
[root@10-0-42-211 mongodb]# netstat -anop | grep 2701
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      843/mongod           off (0.00/0/0)
tcp        0      0 0.0.0.0:27018           0.0.0.0:*               LISTEN      906/mongod           off (0.00/0/0)
tcp        0      0 0.0.0.0:27019           0.0.0.0:*               LISTEN      967/mongod           off (0.00/0/0)
tcp        0      0 10.0.42.211:27019       10.6.1.26:59287         ESTABLISHED 967/mongod           keepalive (54.41/0/0)
tcp        0      0 10.0.42.211:27018       10.6.1.26:59285         ESTABLISHED 906/mongod           keepalive (42.12/0/0)
tcp        0      0 10.0.42.211:27017       10.6.1.26:59281         ESTABLISHED 843/mongod           keepalive (30.09/0/0)
unix  2      [ ACC ]     STREAM     LISTENING     116503   967/mongod           /tmp/mongodb-27019.sock
unix  2      [ ACC ]     STREAM     LISTENING     116478   843/mongod           /tmp/mongodb-27017.sock
unix  2      [ ACC ]     STREAM     LISTENING     116492   906/mongod           /tmp/mongodb-27018.sock

5.指定复制集配置

/home/mongodb/mongodb/bin/mongo

进入进行设置

代码语言:txt复制
rs.initiate()
rs.add ('10.0.42.211:27018')
rs.add ('10.0.42.211:27019')
rs.status()
代码语言:txt复制
rs:PRIMARY> rs.status()
{
        "set" : "rs",
        "date" : ISODate("2022-11-22T07:52:54.485Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "votingMembersCount" : 3,
        "writableVotingMembersCount" : 3,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1669103566, 1),
                        "t" : NumberLong(1)
                },
                "lastCommittedWallTime" : ISODate("2022-11-22T07:52:46.945Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1669103566, 1),
                        "t" : NumberLong(1)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1669103566, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1669103566, 1),
                        "t" : NumberLong(1)
                },
                "lastAppliedWallTime" : ISODate("2022-11-22T07:52:46.945Z"),
                "lastDurableWallTime" : ISODate("2022-11-22T07:52:46.945Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1669103536, 1),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "electionTimeout",
                "lastElectionDate" : ISODate("2022-11-22T07:24:15.765Z"),
                "electionTerm" : NumberLong(1),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(1669101855, 1),
                        "t" : NumberLong(-1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1669101855, 1),
                        "t" : NumberLong(-1)
                },
                "numVotesNeeded" : 1,
                "priorityAtElection" : 1,
                "electionTimeoutMillis" : NumberLong(10000),
                "newTermStartDate" : ISODate("2022-11-22T07:24:16.518Z"),
                "wMajorityWriteAvailabilityDate" : ISODate("2022-11-22T07:24:16.905Z")
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "10-0-42-211:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 1950,
                        "optime" : {
                                "ts" : Timestamp(1669103566, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2022-11-22T07:52:46Z"),
                        "lastAppliedWallTime" : ISODate("2022-11-22T07:52:46.945Z"),
                        "lastDurableWallTime" : ISODate("2022-11-22T07:52:46.945Z"),
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1669101855, 2),
                        "electionDate" : ISODate("2022-11-22T07:24:15Z"),
                        "configVersion" : 5,
                        "configTerm" : 1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 1,
                        "name" : "10.0.42.211:27018",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 1688,
                        "optime" : {
                                "ts" : Timestamp(1669103566, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1669103566, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2022-11-22T07:52:46Z"),
                        "optimeDurableDate" : ISODate("2022-11-22T07:52:46Z"),
                        "lastAppliedWallTime" : ISODate("2022-11-22T07:52:46.945Z"),
                        "lastDurableWallTime" : ISODate("2022-11-22T07:52:46.945Z"),
                        "lastHeartbeat" : ISODate("2022-11-22T07:52:52.764Z"),
                        "lastHeartbeatRecv" : ISODate("2022-11-22T07:52:52.764Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "10-0-42-211:27017",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 5,
                        "configTerm" : 1
                },
                {
                        "_id" : 2,
                        "name" : "10.0.42.211:27019",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 1670,
                        "optime" : {
                                "ts" : Timestamp(1669103566, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1669103566, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2022-11-22T07:52:46Z"),
                        "optimeDurableDate" : ISODate("2022-11-22T07:52:46Z"),
                        "lastAppliedWallTime" : ISODate("2022-11-22T07:52:46.945Z"),
                        "lastDurableWallTime" : ISODate("2022-11-22T07:52:46.945Z"),
                        "lastHeartbeat" : ISODate("2022-11-22T07:52:52.764Z"),
                        "lastHeartbeatRecv" : ISODate("2022-11-22T07:52:52.763Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "10.0.42.211:27018",
                        "syncSourceId" : 1,
                        "infoMessage" : "",
                        "configVersion" : 5,
                        "configTerm" : 1
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1669103566, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1669103566, 1)
}

6.创建用户

代码语言:txt复制
use admin

db.createUser({ user:'admin',pwd:'123456',roles:[ { role:'userAdminAnyDatabase', db: 'admin'},"readWriteAnyDatabase"]});

db.auth('admin', '123456')

7.navcat 登录

只登录主节点可以进行读写,从节点只能读不可写

0 人点赞