mongodb跨集合查询_MongoDB分片集群-Sharded Cluster - 三度

分片概念

分片(sharding)是一种跨多台机器分布数据的方法, MongoDB使用分片来支持具有非常大的数据集和高吞吐量操作的部署。
换句话说:分片(sharding)是指将数据拆分,将其分散存在不同的机器上的过程。有时也用分区(partitioning)来表示这个概念。将数据分散到不同的机器上,不需要功能强大的大型计算机就可以储存更多的数据,处理更多的负载。
具有大型数据集或高吞吐量应用程序的数据库系统可以会挑战单个服务器的容量。例如,高查询率会耗尽服务器的CPU容量。工作集大小大于系统的RAM会强调磁盘驱动器的I / O容量。

有两种解决系统增长的方法:垂直扩展和水平扩展。

  • 垂直扩展意味着增加单个服务器的容量,例如使用更强大的CPU,添加更多RAM或增加存储空间量。可用技术的局限性可能会限制单个机器对于给定工作负载而言足够强大。此外,基于云的提供商基于可用的硬件配置具有硬性上限。结果,垂直缩放有实际的最大值。
  • 水平扩展意味着划分系统数据集并加载多个服务器,添加其他服务器以根据需要增加容量。虽然单个机器的总体速度或容量可能不高,但每台机器处理整个工作负载的子集,可能提供比单个高速大容量服务器更高的效率。扩展部署容量只需要根据需要添加额外的服务器,这可能比单个机器的高端硬件的总体成本更低。权衡是基础架构和部署维护的复杂性增加。

MongoDB支持通过分片进行水平扩展。

分片集群包含的组件

MongoDB分片群集包含以下组件:

  • 分片(存储):每个分片包含分片数据的子集。 每个分片都可以部署为副本集。
  • mongos (路由):mongos充当查询路由器,在客户端应用程序和分片集群之间提供接口。
  • config servers (“调度”的配置):配置服务器存储群集的元数据和配置设置。 从MongoDB 3.4开始,必须将配置服务器部署为副本集(CSRS)。

下图描述了分片集群中组件的交互:8c90b7376f9b3893dc151e2060155533.png

MongoDB在集合级别对数据进行分片,将集合数据分布在集群中的分片上。

分片集群架构目标

两个分片节点副本集(3+3)+一个配置节点副本集(3)+两个路由节点(2),共11个服务节点。c9737d841d41bd636cf862965656a45f.png

分片(存储)节点副本集的创建

所有的的配置文件都直接放到 sharded_cluster 的相应的子目录下面,默认配置文件名字:

第一套副本集

准备存放数据和日志的目录:

#-----------myshardrs01
mkdir -p /home/mongodb/sharded_cluster/myshardrs01_27018/log & mkdir -p /home/mongodb/sharded_cluster/myshardrs01_27018/data/db
mkdir -p /home/mongodb/sharded_cluster/myshardrs01_27118/log & mkdir -p /home/mongodb/sharded_cluster/myshardrs01_27118/data/db
mkdir -p /home/mongodb/sharded_cluster/myshardrs01_27218/log & mkdir -p /home/mongodb/sharded_cluster/myshardrs01_27218/data/db

新建或修改配置文件:

# vim /home/mongodb/sharded_cluster/myshardrs01_27018/
systemLog:
  destination: file
  path: /home/mongodb/sharded_cluster/myshardrs01_27018/log/mongod.log
  logAppend: true
storage:
  dbPath: /home/mongodb/sharded_cluster/myshardrs01_27018/data/db
  journal:
    enabled: true
processManagement:
  fork: true
  pidFilePath: /home/mongodb/sharded_cluster/myshardrs01_27018/log/mongod.pid
net:
  bindIp: 192.168.0.253
  port: 27018
replication:
  replSetName: myshardrs01
sharding:
  clusterRole: shardsvr

# vim /home/mongodb/sharded_cluster/myshardrs01_27118/
systemLog:
  destination: file
  path: /home/mongodb/sharded_cluster/myshardrs01_27118/log/mongod.log
  logAppend: true
storage:
  dbPath: /home/mongodb/sharded_cluster/myshardrs01_27118/data/db
  journal:
    enabled: true
processManagement:
  fork: true
  pidFilePath: /home/mongodb/sharded_cluster/myshardrs01_27118/log/mongod.pid
net:
  bindIp: 192.168.0.253
  port: 27118
replication:
  replSetName: myshardrs01
sharding:
  clusterRole: shardsvr
  
# vim /home/mongodb/sharded_cluster/myshardrs01_27218/
systemLog:
  destination: file
  path: /home/mongodb/sharded_cluster/myshardrs01_27218/log/mongod.log
  logAppend: true
storage:
  dbPath: /home/mongodb/sharded_cluster/myshardrs01_27218/data/db
  journal:
    enabled: true
processManagement:
  fork: true
  pidFilePath: /home/mongodb/sharded_cluster/myshardrs01_27218/log/mongod.pid
net:
  bindIp: 192.168.0.253
  port: 27218
replication:
  replSetName: myshardrs01
sharding:
  clusterRole: shardsvr

启动第一套副本集:一主一副本一仲裁
依次启动三个mongod服务:

/usr/bin/mongod -f /home/mongodb/sharded_cluster/myshardrs01_27018/
/usr/bin/mongod -f /home/mongodb/sharded_cluster/myshardrs01_27118/
/usr/bin/mongod -f /home/mongodb/sharded_cluster/myshardrs01_27218/

查看服务是否启动:

# ps -ef |grep mongod
root      4080     1  3 14:45 ?        00:00:01 /usr/bin/mongod -f /home/mongodb/sharded_cluster/myshardrs01_27018/
root      4133     1  4 14:45 ?        00:00:01 /usr/bin/mongod -f /home/mongodb/sharded_cluster/myshardrs01_27118/
root      4186     1  6 14:45 ?        00:00:01 /usr/bin/mongod -f /home/mongodb/sharded_cluster/myshardrs01_27218/

# ss -tulnp |grep 27*
tcp    LISTEN     0      128                     *:*                   users:(("mongod",pid=4080,fd=12))
tcp    LISTEN     0      128                     *:*                   users:(("mongod",pid=4133,fd=12))
tcp    LISTEN     0      128                     *:*                   users:(("mongod",pid=4186,fd=12))

(1)初始化副本集和创建主节点:
使用客户端命令连接任意一个节点,但这里尽量要连接主节点:

/usr/bin/mongo --host 192.168.0.253 --port 27018

执行初始化副本集命令:

> ()
{
        "info2" : "no configuration specified. Using a default configuration for the set",
        "me" : "",
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1605163725, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1605163725, 1)
}

查看副本集情况:

myshardrs01:PRIMARY> ()
{
        "set" : "myshardrs01",
        "date" : ISODate("2020-11-12T06:49:"),
        "myState" : 1,
        "term" : NumberLong(1),
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 1,
        "writeMajorityCount" : 1,
        "votingMembersCount" : 1,
        "writableVotingMembersCount" : 1,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1605163776, 1),
                        "t" : NumberLong(1)
                },
                "lastCommittedWallTime" : ISODate("2020-11-12T06:49:"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1605163776, 1),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityWallTime" : ISODate("2020-11-12T06:49:"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1605163776, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1605163776, 1),
                        "t" : NumberLong(1)
                },
                "lastAppliedWallTime" : ISODate("2020-11-12T06:49:"),
                "lastDurableWallTime" : ISODate("2020-11-12T06:49:")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1605163726, 4),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "electionTimeout",
                "lastElectionDate" : ISODate("2020-11-12T06:48:"),
                "electionTerm" : NumberLong(1),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1605163725, 1),
                        "t" : NumberLong(-1)
                },
                "numVotesNeeded" : 1,
                "priorityAtElection" : 1,
                "electionTimeoutMillis" : NumberLong(10000),
                "newTermStartDate" : ISODate("2020-11-12T06:48:"),
                "wMajorityWriteAvailabilityDate" : ISODate("2020-11-12T06:48:")
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 232,
                        "optime" : {
                                "ts" : Timestamp(1605163776, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-11-12T06:49:36Z"),
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "Could not find member to sync from",
                        "electionTime" : Timestamp(1605163726, 1),
                        "electionDate" : ISODate("2020-11-12T06:48:46Z"),
                        "configVersion" : 1,
                        "configTerm" : -1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1605163776, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1605163776, 1)
}

(2)主节点配置查看:

myshardrs01:PRIMARY> ()
{
        "_id" : "myshardrs01",
        "version" : 1,
        "protocolVersion" : NumberLong(1),
        "writeConcernMajorityJournalDefault" : true,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "catchUpTimeoutMillis" : -1,
                "catchUpTakeoverDelayMillis" : 30000,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("5facdacda5ab79463a9265e4")
        }
}

( 3)添加副本节点:

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

(4)添加仲裁节点:

myshardrs01:PRIMARY> Arb("")
{
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1605164036, 2),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1605164036, 2)
}

查看副本集的配置情况:

myshardrs01:PRIMARY> ()
{
        "_id" : "myshardrs01",
        "version" : 3,
        "protocolVersion" : NumberLong(1),
        "writeConcernMajorityJournalDefault" : true,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 1,
                        "host" : "",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 2,
                        "host" : "",
                        "arbiterOnly" : true,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 0,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "catchUpTimeoutMillis" : -1,
                "catchUpTakeoverDelayMillis" : 30000,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("5facdacda5ab79463a9265e4")
        }
}

第二套副本集

准备存放数据和日志的目录:

#-----------myshardrs02
mkdir -p /home/mongodb/sharded_cluster/myshardrs02_27318/log & mkdir -p /home/mongodb/sharded_cluster/myshardrs02_27318/data/db
mkdir -p /home/mongodb/sharded_cluster/myshardrs02_27418/log & mkdir -p /home/mongodb/sharded_cluster/myshardrs02_27418/data/db
mkdir -p /home/mongodb/sharded_cluster/myshardrs02_27518/log & mkdir -p /home/mongodb/sharded_cluster/myshardrs02_27518/data/db

新建或修改配置文件:

# vim /home/mongodb/sharded_cluster/myshardrs02_27318/
systemLog:
  destination: file
  path: /home/mongodb/sharded_cluster/myshardrs02_27318/log/mongod.log
  logAppend: true
storage:
  dbPath: /home/mongodb/sharded_cluster/myshardrs02_27318/data/db
  journal:
    enabled: true
processManagement:
  fork: true
  pidFilePath: /home/mongodb/sharded_cluster/myshardrs02_27318/log/mongod.pid
net:
  bindIp: 192.168.0.253
  port: 27318
replication:
  replSetName: myshardrs02
sharding:
  clusterRole: shardsvr

# vim /home/mongodb/sharded_cluster/myshardrs02_27418/
systemLog:
  destination: file
  path: /home/mongodb/sharded_cluster/myshardrs02_27418/log/mongod.log
  logAppend: true
storage:
  dbPath: /home/mongodb/sharded_cluster/myshardrs02_27418/data/db
  journal:
    enabled: true
processManagement:
  fork: true
  pidFilePath: /home/mongodb/sharded_cluster/myshardrs02_27418/log/mongod.pid
net:
  bindIp: 192.168.0.253
  port: 27418
replication:
  replSetName: myshardrs02
sharding:
  clusterRole: shardsvr
  
# vim /home/mongodb/sharded_cluster/myshardrs02_27518/
systemLog:
  destination: file
  path: /home/mongodb/sharded_cluster/myshardrs02_27518/log/mongod.log
  logAppend: true
storage:
  dbPath: /home/mongodb/sharded_cluster/myshardrs02_27518/data/db
  journal:
    enabled: true
processManagement:
  fork: true
  pidFilePath: /home/mongodb/sharded_cluster/myshardrs02_27518/log/mongod.pid
net:
  bindIp: 192.168.0.253
  port: 27518
replication:
  replSetName: myshardrs02
sharding:
  clusterRole: shardsvr

启动第一套副本集:一主一副本一仲裁

依次启动三个mongod服务:

/usr/bin/mongod -f /home/mongodb/sharded_cluster/myshardrs02_27318/
/usr/bin/mongod -f /home/mongodb/sharded_cluster/myshardrs02_27418/
/usr/bin/mongod -f /home/mongodb/sharded_cluster/myshardrs02_27518/

查看服务是否启动:

# ps -ef |grep mongod    
root      4080     1  0 14:45 ?        00:00:08 /usr/bin/mongod -f /home/mongodb/sharded_cluster/myshardrs01_27018/
root      4133     1  0 14:45 ?        00:00:07 /usr/bin/mongod -f /home/mongodb/sharded_cluster/myshardrs01_27118/
root      4186     1  0 14:45 ?        00:00:07 /usr/bin/mongod -f /home/mongodb/sharded_cluster/myshardrs01_27218/
root      4527     1  5 14:59 ?        00:00:01 /usr/bin/mongod -f /home/mongodb/sharded_cluster/myshardrs02_27318/
root      4580     1  7 14:59 ?        00:00:00 /usr/bin/mongod -f /home/mongodb/sharded_cluster/myshardrs02_27418/
root      4633     1 11 14:59 ?        00:00:00 /usr/bin/mongod -f /home/mongodb/sharded_cluster/myshardrs02_27518/

# ss -tulnp |grep 27  
tcp    LISTEN     0      128                     *:*                   users:(("mongod",pid=4080,fd=12))
tcp    LISTEN     0      128                     *:*                   users:(("mongod",pid=4133,fd=12))
tcp    LISTEN     0      128                     *:*                   users:(("mongod",pid=4186,fd=12))
tcp    LISTEN     0      128                     *:*                   users:(("mongod",pid=4527,fd=12))
tcp    LISTEN     0      128                     *:*                   users:(("mongod",pid=4580,fd=12))
tcp    LISTEN     0      128                     *:*                   users:(("mongod",pid=4633,fd=12))

(1)初始化副本集和创建主节点:
使用客户端命令连接任意一个节点,但这里尽量要连接主节点:

/usr/bin/mongo --host 192.168.0.253 --port 27318

执行初始化副本集命令:

> ()
{
        "info2" : "no configuration specified. Using a default configuration for the set",
        "me" : "",
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1605164586, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1605164586, 1)
}

查看副本集情况:

myshardrs02:PRIMARY> ()
{
        "set" : "myshardrs02",
        "date" : ISODate("2020-11-12T07:03:"),
        "myState" : 1,
        "term" : NumberLong(1),
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 1,
        "writeMajorityCount" : 1,
        "votingMembersCount" : 1,
        "writableVotingMembersCount" : 1,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1605164596, 1),
                        "t" : NumberLong(1)
                },
                "lastCommittedWallTime" : ISODate("2020-11-12T07:03:"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1605164596, 1),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityWallTime" : ISODate("2020-11-12T07:03:"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1605164596, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1605164596, 1),
                        "t" : NumberLong(1)
                },
                "lastAppliedWallTime" : ISODate("2020-11-12T07:03:"),
                "lastDurableWallTime" : ISODate("2020-11-12T07:03:")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1605164586, 5),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "electionTimeout",
                "lastElectionDate" : ISODate("2020-11-12T07:03:"),
                "electionTerm" : NumberLong(1),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1605164586, 1),
                        "t" : NumberLong(-1)
                },
                "numVotesNeeded" : 1,
                "priorityAtElection" : 1,
                "electionTimeoutMillis" : NumberLong(10000),
                "newTermStartDate" : ISODate("2020-11-12T07:03:"),
                "wMajorityWriteAvailabilityDate" : ISODate("2020-11-12T07:03:")
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 231,
                        "optime" : {
                                "ts" : Timestamp(1605164596, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-11-12T07:03:16Z"),
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "Could not find member to sync from",
                        "electionTime" : Timestamp(1605164586, 2),
                        "electionDate" : ISODate("2020-11-12T07:03:06Z"),
                        "configVersion" : 1,
                        "configTerm" : -1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1605164596, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1605164596, 1)
}

(2)主节点配置查看:

myshardrs02:PRIMARY> ()
{
        "_id" : "myshardrs02",
        "version" : 1,
        "protocolVersion" : NumberLong(1),
        "writeConcernMajorityJournalDefault" : true,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "catchUpTimeoutMillis" : -1,
                "catchUpTakeoverDelayMillis" : 30000,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("5facde29ec2cfe3457df95cd")
        }
}

( 3)添加副本节点:

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

(4)添加仲裁节点:

myshardrs02:PRIMARY> Arb("")
{
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1605164699, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1605164699, 1)
}

查看副本集的配置情况:

myshardrs02:PRIMARY> ()
{
        "_id" : "myshardrs02",
        "version" : 3,
        "protocolVersion" : NumberLong(1),
        "writeConcernMajorityJournalDefault" : true,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 1,
                        "host" : "",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 2,
                        "host" : "",
                        "arbiterOnly" : true,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 0,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "catchUpTimeoutMillis" : -1,
                "catchUpTakeoverDelayMillis" : 30000,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("5facde29ec2cfe3457df95cd")
        }
}

查看副本集情况:

myshardrs02:PRIMARY> ()
{
        "set" : "myshardrs02",
        "date" : ISODate("2020-11-12T07:07:"),
        "myState" : 1,
        "term" : NumberLong(1),
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "votingMembersCount" : 3,
        "writableVotingMembersCount" : 2,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1605164816, 1),
                        "t" : NumberLong(1)
                },
                "lastCommittedWallTime" : ISODate("2020-11-12T07:06:"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1605164816, 1),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityWallTime" : ISODate("2020-11-12T07:06:"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1605164816, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1605164816, 1),
                        "t" : NumberLong(1)
                },
                "lastAppliedWallTime" : ISODate("2020-11-12T07:06:"),
                "lastDurableWallTime" : ISODate("2020-11-12T07:06:")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1605164766, 1),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "electionTimeout",
                "lastElectionDate" : ISODate("2020-11-12T07:03:"),
                "electionTerm" : NumberLong(1),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1605164586, 1),
                        "t" : NumberLong(-1)
                },
                "numVotesNeeded" : 1,
                "priorityAtElection" : 1,
                "electionTimeoutMillis" : NumberLong(10000),
                "newTermStartDate" : ISODate("2020-11-12T07:03:"),
                "wMajorityWriteAvailabilityDate" : ISODate("2020-11-12T07:03:")
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 449,
                        "optime" : {
                                "ts" : Timestamp(1605164816, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-11-12T07:06:56Z"),
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1605164586, 2),
                        "electionDate" : ISODate("2020-11-12T07:03:06Z"),
                        "configVersion" : 3,
                        "configTerm" : -1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 1,
                        "name" : "",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 144,
                        "optime" : {
                                "ts" : Timestamp(1605164816, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1605164816, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-11-12T07:06:56Z"),
                        "optimeDurableDate" : ISODate("2020-11-12T07:06:56Z"),
                        "lastHeartbeat" : ISODate("2020-11-12T07:07:"),
                        "lastHeartbeatRecv" : ISODate("2020-11-12T07:07:"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 3,
                        "configTerm" : -1
                },
                {
                        "_id" : 2,
                        "name" : "",
                        "health" : 1,
                        "state" : 7,
                        "stateStr" : "ARBITER",
                        "uptime" : 123,
                        "lastHeartbeat" : ISODate("2020-11-12T07:07:"),
                        "lastHeartbeatRecv" : ISODate("2020-11-12T07:07:"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 3,
                        "configTerm" : -1
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1605164816, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1605164816, 1)
}

配置节点副本集的创建

第一步:准备存放数据和日志的目录:

#-----------configrs
#建立数据节点data和日志目录
mkdir -p /home/mongodb/sharded_cluster/myconfigrs_27019/log & mkdir -p /home/mongodb/sharded_cluster/myconfigrs_27019/data/db
mkdir -p /home/mongodb/sharded_cluster/myconfigrs_27119/log & mkdir -p /home/mongodb/sharded_cluster/myconfigrs_27119/data/db
mkdir -p /home/mongodb/sharded_cluster/myconfigrs_27219/log & mkdir -p /home/mongodb/sharded_cluster/myconfigrs_27219/data/db

新建或修改配置文件:

# vim /home/mongodb/sharded_cluster/myconfigrs_27019/
systemLog:
  destination: file
  path: /home/mongodb/sharded_cluster/myconfigrs_27019/log/mongod.log
  logAppend: true
storage:
  dbPath: /home/mongodb/sharded_cluster/myconfigrs_27019/data/db
  journal:
    enabled: true
processManagement:
  fork: true
  pidFilePath: /home/mongodb/sharded_cluster/myconfigrs_27019/log/mongod.pid
net:
  bindIp: 192.168.0.253
  port: 27019
replication:
  replSetName: myconfigrs
sharding:
  clusterRole: configsvr

# vim /home/mongodb/sharded_cluster/myconfigrs_27119/
systemLog:
  destination: file
  path: /home/mongodb/sharded_cluster/myconfigrs_27119/log/mongod.log
  logAppend: true
storage:
  dbPath: /home/mongodb/sharded_cluster/myconfigrs_27119/data/db
  journal:
    enabled: true
processManagement:
  fork: true
  pidFilePath: /home/mongodb/sharded_cluster/myconfigrs_27119/log/mongod.pid
net:
  bindIp: 192.168.0.253
  port: 27119
replication:
  replSetName: myconfigrs
sharding:
  clusterRole: configsvr


# vim /home/mongodb/sharded_cluster/myconfigrs_27219/
systemLog:
  destination: file
  path: /home/mongodb/sharded_cluster/myconfigrs_27219/log/mongod.log
  logAppend: true
storage:
  dbPath: /home/mongodb/sharded_cluster/myconfigrs_27219/data/db
  journal:
    enabled: true
processManagement:
  fork: true
  pidFilePath: /home/mongodb/sharded_cluster/myconfigrs_27219/log/mongod.pid
net:
  bindIp: 192.168.0.253
  port: 27219
replication:
  replSetName: myconfigrs
sharding:
  clusterRole: configsvr

启动配置副本集:一主两副本

依次启动三个mongod服务:

/usr/bin/mongod -f /home/mongodb/sharded_cluster/myconfigrs_27019/
/usr/bin/mongod -f /home/mongodb/sharded_cluster/myconfigrs_27119/
/usr/bin/mongod -f /home/mongodb/sharded_cluster/myconfigrs_27219/

查看服务是否启动:

# ps -ef |grep mongod          
root      4080     1  0 14:45 ?        00:00:17 /usr/bin/mongod -f /home/mongodb/sharded_cluster/myshardrs01_27018/
root      4133     1  0 14:45 ?        00:00:15 /usr/bin/mongod -f /home/mongodb/sharded_cluster/myshardrs01_27118/
root      4186     1  0 14:45 ?        00:00:11 /usr/bin/mongod -f /home/mongodb/sharded_cluster/myshardrs01_27218/
root      4527     1  0 14:59 ?        00:00:11 /usr/bin/mongod -f /home/mongodb/sharded_cluster/myshardrs02_27318/
root      4580     1  0 14:59 ?        00:00:09 /usr/bin/mongod -f /home/mongodb/sharded_cluster/myshardrs02_27418/
root      4633     1  0 14:59 ?        00:00:07 /usr/bin/mongod -f /home/mongodb/sharded_cluster/myshardrs02_27518/
root      5026     1  3 15:17 ?        00:00:01 /usr/bin/mongod -f /home/mongodb/sharded_cluster/myconfigrs_27019/
root      5087     1  4 15:17 ?        00:00:01 /usr/bin/mongod -f /home/mongodb/sharded_cluster/myconfigrs_27119/
root      5148     1  5 15:18 ?        00:00:01 /usr/bin/mongod -f /home/mongodb/sharded_cluster/myconfigrs_27219/

# ss -tulnp |grep 27         
tcp    LISTEN     0      128                     *:*                   users:(("mongod",pid=4080,fd=12))
tcp    LISTEN     0      128                     *:*                   users:(("mongod",pid=5026,fd=12))
tcp    LISTEN     0      128                     *:*                   users:(("mongod",pid=4133,fd=12))
tcp    LISTEN     0      128                     *:*                   users:(("mongod",pid=5087,fd=12))
tcp    LISTEN     0      128                     *:*                   users:(("mongod",pid=4186,fd=12))
tcp    LISTEN     0      128                     *:*                   users:(("mongod",pid=5148,fd=12))
tcp    LISTEN     0      128                     *:*                   users:(("mongod",pid=4527,fd=12))
tcp    LISTEN     0      128                     *:*                   users:(("mongod",pid=4580,fd=12))
tcp    LISTEN     0      128                     *:*                   users:(("mongod",pid=4633,fd=12))

(1)初始化副本集和创建主节点:
使用客户端命令连接任意一个节点,但这里尽量要连接主节点:

/usr/bin/mongo --host 192.168.0.253 --port 27019

执行初始化副本集命令:

> ()
{
        "info2" : "no configuration specified. Using a default configuration for the set",
        "me" : "",
        "ok" : 1,
        "$gleStats" : {
                "lastOpTime" : Timestamp(1605165618, 1),
                "electionId" : ObjectId("000000000000000000000000")
        },
        "lastCommittedOpTime" : Timestamp(0, 0),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1605165618, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1605165618, 1)
}

查看副本集情况:

myconfigrs:PRIMARY> ()
{
        "set" : "myconfigrs",
        "date" : ISODate("2020-11-12T07:20:"),
        "myState" : 1,
        "term" : NumberLong(1),
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "configsvr" : true,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 1,
        "writeMajorityCount" : 1,
        "votingMembersCount" : 1,
        "writableVotingMembersCount" : 1,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1605165638, 1),
                        "t" : NumberLong(1)
                },
                "lastCommittedWallTime" : ISODate("2020-11-12T07:20:"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1605165638, 1),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityWallTime" : ISODate("2020-11-12T07:20:"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1605165638, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1605165638, 1),
                        "t" : NumberLong(1)
                },
                "lastAppliedWallTime" : ISODate("2020-11-12T07:20:"),
                "lastDurableWallTime" : ISODate("2020-11-12T07:20:")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1605165619, 11),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "electionTimeout",
                "lastElectionDate" : ISODate("2020-11-12T07:20:"),
                "electionTerm" : NumberLong(1),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1605165618, 1),
                        "t" : NumberLong(-1)
                },
                "numVotesNeeded" : 1,
                "priorityAtElection" : 1,
                "electionTimeoutMillis" : NumberLong(10000),
                "newTermStartDate" : ISODate("2020-11-12T07:20:"),
                "wMajorityWriteAvailabilityDate" : ISODate("2020-11-12T07:20:")
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 167,
                        "optime" : {
                                "ts" : Timestamp(1605165638, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-11-12T07:20:38Z"),
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "Could not find member to sync from",
                        "electionTime" : Timestamp(1605165618, 2),
                        "electionDate" : ISODate("2020-11-12T07:20:18Z"),
                        "configVersion" : 1,
                        "configTerm" : 1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                }
        ],
        "ok" : 1,
        "$gleStats" : {
                "lastOpTime" : Timestamp(1605165618, 1),
                "electionId" : ObjectId("7fffffff0000000000000001")
        },
        "lastCommittedOpTime" : Timestamp(1605165638, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1605165638, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1605165638, 1)
}

(2)主节点配置查看:

myconfigrs:PRIMARY> ()
{
        "_id" : "myconfigrs",
        "version" : 1,
        "term" : 1,
        "configsvr" : true,
        "protocolVersion" : NumberLong(1),
        "writeConcernMajorityJournalDefault" : true,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "catchUpTimeoutMillis" : -1,
                "catchUpTakeoverDelayMillis" : 30000,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("5face231793c0e43a638ac4b")
        }
}

( 3)添加副本节点:

myconfigrs:PRIMARY> ("")
{
        "ok" : 1,
        "$gleStats" : {
                "lastOpTime" : {
                        "ts" : Timestamp(1605165745, 1),
                        "t" : NumberLong(1)
                },
                "electionId" : ObjectId("7fffffff0000000000000001")
        },
        "lastCommittedOpTime" : Timestamp(1605165745, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1605165746, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1605165745, 1)
}
myconfigrs:PRIMARY> ("")
{
        "ok" : 1,
        "$gleStats" : {
                "lastOpTime" : {
                        "ts" : Timestamp(1605165756, 1),
                        "t" : NumberLong(1)
                },
                "electionId" : ObjectId("7fffffff0000000000000001")
        },
        "lastCommittedOpTime" : Timestamp(1605165757, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1605165757, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1605165756, 1)
}

查看副本集的配置情况:

myconfigrs:PRIMARY> ()
{
        "_id" : "myconfigrs",
        "version" : 3,
        "term" : 1,
        "configsvr" : true,
        "protocolVersion" : NumberLong(1),
        "writeConcernMajorityJournalDefault" : true,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 1,
                        "host" : "",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 2,
                        "host" : "",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "catchUpTimeoutMillis" : -1,
                "catchUpTakeoverDelayMillis" : 30000,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("5face231793c0e43a638ac4b")
        }
}


myconfigrs:PRIMARY> ()
{
        "set" : "myconfigrs",
        "date" : ISODate("2020-11-12T07:23:"),
        "myState" : 1,
        "term" : NumberLong(1),
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "configsvr" : true,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "votingMembersCount" : 3,
        "writableVotingMembersCount" : 3,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1605165801, 1),
                        "t" : NumberLong(1)
                },
                "lastCommittedWallTime" : ISODate("2020-11-12T07:23:"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1605165801, 1),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityWallTime" : ISODate("2020-11-12T07:23:"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1605165801, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1605165801, 1),
                        "t" : NumberLong(1)
                },
                "lastAppliedWallTime" : ISODate("2020-11-12T07:23:"),
                "lastDurableWallTime" : ISODate("2020-11-12T07:23:")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1605165800, 1),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "electionTimeout",
                "lastElectionDate" : ISODate("2020-11-12T07:20:"),
                "electionTerm" : NumberLong(1),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1605165618, 1),
                        "t" : NumberLong(-1)
                },
                "numVotesNeeded" : 1,
                "priorityAtElection" : 1,
                "electionTimeoutMillis" : NumberLong(10000),
                "newTermStartDate" : ISODate("2020-11-12T07:20:"),
                "wMajorityWriteAvailabilityDate" : ISODate("2020-11-12T07:20:")
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 330,
                        "optime" : {
                                "ts" : Timestamp(1605165801, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-11-12T07:23:21Z"),
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1605165618, 2),
                        "electionDate" : ISODate("2020-11-12T07:20:18Z"),
                        "configVersion" : 3,
                        "configTerm" : 1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 1,
                        "name" : "",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 56,
                        "optime" : {
                                "ts" : Timestamp(1605165801, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1605165801, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-11-12T07:23:21Z"),
                        "optimeDurableDate" : ISODate("2020-11-12T07:23:21Z"),
                        "lastHeartbeat" : ISODate("2020-11-12T07:23:"),
                        "lastHeartbeatRecv" : ISODate("2020-11-12T07:23:"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 3,
                        "configTerm" : 1
                },
                {
                        "_id" : 2,
                        "name" : "",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 46,
                        "optime" : {
                                "ts" : Timestamp(1605165801, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1605165801, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-11-12T07:23:21Z"),
                        "optimeDurableDate" : ISODate("2020-11-12T07:23:21Z"),
                        "lastHeartbeat" : ISODate("2020-11-12T07:23:"),
                        "lastHeartbeatRecv" : ISODate("2020-11-12T07:23:"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : 1,
                        "infoMessage" : "",
                        "configVersion" : 3,
                        "configTerm" : 1
                }
        ],
        "ok" : 1,
        "$gleStats" : {
                "lastOpTime" : {
                        "ts" : Timestamp(1605165756, 1),
                        "t" : NumberLong(1)
                },
                "electionId" : ObjectId("7fffffff0000000000000001")
        },
        "lastCommittedOpTime" : Timestamp(1605165801, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1605165801, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1605165801, 1)
}

路由节点的创建和操作

第一个路由节点的创建和连接

第一步:准备存放日志的目录:

#-----------mongos01
mkdir -p /home/mongodb/sharded_cluster/mymongos_27017/log

新建或修改配置文件:

# vim /home/mongodb/sharded_cluster/mymongos_27017/
systemLog:
  destination: file
  path: /home/mongodb/sharded_cluster/mymongos_27017/log/mongod.log
  logAppend: true
processManagement:
  fork: true
  pidFilePath: /home/mongodb/sharded_cluster/mymongos_27017/log/mongod.pid
net:
  bindIp: 192.168.0.253
  port: 27017
sharding:
  configDB: myconfigrs/,,

启动mongos:

/usr/bin/mongos -f /home/mongodb/sharded_cluster/mymongos_27017/

提示:启动如果失败,可以查看 log目录下的日志,查看失败原因。

客户端登录mongos

# /usr/bin/mongo --host 192.168.0.253 --port 27017  
MongoDB shell version v
connecting to: mongodb://192.168.0.253:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("ec6d17bf-2ae9-40c5-b690-7b0f0c30d40f") }
MongoDB server version: 
---
The server generated these startup warnings when booting: 
        2020-11-12T15:34:52.266+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2020-11-12T15:34:52.266+08:00: You are running this process as the root user, which is not recommended
---
mongos> 

此时,写不进去数据,如果写数据会报错,因为通过路由节点操作,现在只是连接了配置节点,还没有连接分片数据节点,因此无法写入业务数据。

在路由节点上进行分片配置操作

使用命令添加分片:
(1)添加分片:
语法:

("IP:Port")

将第一套分片副本集添加进来:

mongos> ("myshardrs01/,,")
{
        "shardAdded" : "myshardrs01",
        "ok" : 1,
        "operationTime" : Timestamp(1605166795, 5),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1605166795, 5),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}

查看分片状态情况:

mongos> ()
--- Sharding Status --- 
  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("5face233793c0e43a638ac50")
  }
  shards:
        {  "_id" : "myshardrs01",  "host" : "myshardrs01/,",  "state" : 1 }
  active mongoses:
        "" : 1
  autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours: 
                No recent migrations
  databases:
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }

继续将第二套分片副本集添加进来:

mongos> ("myshardrs02/,,")
{
        "shardAdded" : "myshardrs02",
        "ok" : 1,
        "operationTime" : Timestamp(1605166905, 3),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1605166905, 3),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}

查看分片状态:

mongos> ()
--- Sharding Status --- 
  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("5face233793c0e43a638ac50")
  }
  shards:
        {  "_id" : "myshardrs01",  "host" : "myshardrs01/,",  "state" : 1 }
        {  "_id" : "myshardrs02",  "host" : "myshardrs02/,",  "state" : 1 }
  active mongoses:
        "" : 1
  autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours: 
                No recent migrations
  databases:
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }

提示:如果添加分片失败,需要先手动移除分片,检查添加分片的信息的正确性后,再次添加分片。

移除分片参考(了解):

use admin
db.runCommand({removeShard:"myshardrs02" })

注意:如果只剩下最后一个 shard,是无法删除的,移除时会自动转移分片数据,需要一个时间过程。完成后,再次执行删除分片命令才能真正删除。

(2)开启分片功能:("库名")、("库名.集合名",{"key":1})
在mongos上的articledb数据库配置sharding:

mongos> ("articledb")
{
        "ok" : 1,
        "operationTime" : Timestamp(1605167077, 4),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1605167077, 4),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}

查看分片状态:

mongos> ()
--- Sharding Status --- 
  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("5face233793c0e43a638ac50")
  }
  shards:
        {  "_id" : "myshardrs01",  "host" : "myshardrs01/,",  "state" : 1 }
        {  "_id" : "myshardrs02",  "host" : "myshardrs02/,",  "state" : 1 }
  active mongoses:
        "" : 1
  autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  yes
        Collections with active migrations: 
                config.system.sessions started at Thu Nov 12 2020 15:45:02 GMT+0800 (CST)
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours: 
                59 : Success
  databases:
        {  "_id" : "articledb",  "primary" : "myshardrs01",  "partitioned" : true,  "version" : {  "uuid" : UUID("238a84e2-d22e-4159-8cf2-4d38e77a0bba"),  "lastMod" : 1 } }
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
                config.system.sessions
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                myshardrs01     964
                                myshardrs02     60
                        too many chunks to print, use verbose if you want to force print

(3)集合分片
对集合分片,你必须使用 () 方法指定集合和分片键。
语法:

(namespace, key, unique)

参数:af469e3a25d0d527fe22b4acabf3b743.png

对集合进行分片时,你需要选择一个 片键(Shard Key) , shard key 是每条记录都必须包含的,且建立了索引的单个字段或复合字段,MongoDB按照片键将数据划分到不同的 数据块 中,并将 数据块 均衡地分布到所有分片中.为了按照片键划分数据块,MongoDB使用 基于哈希的分片方式(随机平均分配)或者基于范围的分片方式(数值大小分配) 。

用什么字段当片键都可以,如:nickname作为片键,但一定是必填字段。

分片规则一:哈希策略
对于 基于哈希的分片 ,MongoDB计算一个字段的哈希值,并用这个哈希值来创建数据块.
在使用基于哈希分片的系统中,拥有”相近”片键的文档 很可能不会 存储在同一个数据块中,因此数据的分离性更好一些.
使用nickname作为片键,根据其值的哈希值进行数据分片

mongos> ("",{"nickname":"hashed"})
{
        "collectionsharded" : "",
        "collectionUUID" : UUID("87f5c846-4222-4d27-858f-073b7569be2d"),
        "ok" : 1,
        "operationTime" : Timestamp(1605167364, 13),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1605167364, 13),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}

查看分片状态:()

mongos> ()
--- Sharding Status --- 
  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("5face233793c0e43a638ac50")
  }
  shards:
        {  "_id" : "myshardrs01",  "host" : "myshardrs01/,",  "state" : 1 }
        {  "_id" : "myshardrs02",  "host" : "myshardrs02/,",  "state" : 1 }
  active mongoses:
        "" : 1
  autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours: 
                200 : Success
  databases:
        {  "_id" : "articledb",  "primary" : "myshardrs01",  "partitioned" : true,  "version" : {  "uuid" : UUID("238a84e2-d22e-4159-8cf2-4d38e77a0bba"),  "lastMod" : 1 } }
                
                        shard key: { "nickname" : "hashed" }
                        unique: false
                        balancing: true
                        chunks:
                                myshardrs01     2
                                myshardrs02     2
                        { "nickname" : { "$minKey" : 1 } } -->> { "nickname" : NumberLong("-4611686018427387902") } on : myshardrs01 Timestamp(1, 0) 
                        { "nickname" : NumberLong("-4611686018427387902") } -->> { "nickname" : NumberLong(0) } on : myshardrs01 Timestamp(1, 1) 
                        { "nickname" : NumberLong(0) } -->> { "nickname" : NumberLong("4611686018427387902") } on : myshardrs02 Timestamp(1, 2) 
                        { "nickname" : NumberLong("4611686018427387902") } -->> { "nickname" : { "$maxKey" : 1 } } on : myshardrs02 Timestamp(1, 3) 
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
                config.system.sessions
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                myshardrs01     824
                                myshardrs02     200
                        too many chunks to print, use verbose if you want to force print

分片规则二:范围策略
对于 基于范围的分片 ,MongoDB按照片键的范围把数据分成不同部分.假设有一个数字的片键:想象一个从负无穷到正无穷的直线,每一个片键的值都在直线上画了一个点.MongoDB把这条直线划分为更短的不重叠的片段,并称之为 数据块 ,每个数据块包含了片键在一定范围内的数据.
在使用片键做范围划分的系统中,拥有”相近”片键的文档很可能存储在同一个数据块中,因此也会存储在同一个分片中.
如使用作者年龄字段作为片键,按照点赞数的值进行分片:

mongos> ("",{"age":1})
{
        "collectionsharded" : "",
        "collectionUUID" : UUID("15698590-02ae-45ca-9a91-ebb695c9b2ad"),
        "ok" : 1,
        "operationTime" : Timestamp(1605167437, 18),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1605167437, 18),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}

注意的是:
1)一个集合只能指定一个片键,否则报错。
2)一旦对一个集合分片,分片键和分片值就不可改变。 如:不能给集合选择不同的分片键、不能更新分片键的值。
3)根据age索引进行分配数据。

查看分片状态:

mongos> ()
--- Sharding Status --- 
  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("5face233793c0e43a638ac50")
  }
  shards:
        {  "_id" : "myshardrs01",  "host" : "myshardrs01/,",  "state" : 1 }
        {  "_id" : "myshardrs02",  "host" : "myshardrs02/,",  "state" : 1 }
  active mongoses:
        "" : 1
  autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours: 
                244 : Success
  databases:
        {  "_id" : "articledb",  "primary" : "myshardrs01",  "partitioned" : true,  "version" : {  "uuid" : UUID("238a84e2-d22e-4159-8cf2-4d38e77a0bba"),  "lastMod" : 1 } }
                
                        shard key: { "age" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                myshardrs01     1
                        { "age" : { "$minKey" : 1 } } -->> { "age" : { "$maxKey" : 1 } } on : myshardrs01 Timestamp(1, 0) 
                
                        shard key: { "nickname" : "hashed" }
                        unique: false
                        balancing: true
                        chunks:
                                myshardrs01     2
                                myshardrs02     2
                        { "nickname" : { "$minKey" : 1 } } -->> { "nickname" : NumberLong("-4611686018427387902") } on : myshardrs01 Timestamp(1, 0) 
                        { "nickname" : NumberLong("-4611686018427387902") } -->> { "nickname" : NumberLong(0) } on : myshardrs01 Timestamp(1, 1) 
                        { "nickname" : NumberLong(0) } -->> { "nickname" : NumberLong("4611686018427387902") } on : myshardrs02 Timestamp(1, 2) 
                        { "nickname" : NumberLong("4611686018427387902") } -->> { "nickname" : { "$maxKey" : 1 } } on : myshardrs02 Timestamp(1, 3) 
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
                config.system.sessions
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                myshardrs01     780
                                myshardrs02     244
                        too many chunks to print, use verbose if you want to force print

基于范围的分片方式与基于哈希的分片方式性能对比:
基于范围的分片方式提供了更高效的范围查询,给定一个片键的范围,分发路由可以很简单地确定哪个数据块存储了请求需要的数据,并将请求转发到相应的分片中.
不过,基于范围的分片会导致数据在不同分片上的不均衡,有时候,带来的消极作用会大于查询性能的积极作用.比如,如果片键所在的字段是线性增长的,一定时间内的所有请求都会落到某个固定的数据块中,最终导致分布在同一个分片中.在这种情况下,一小部分分片承载了集群大部分的数据,系统并不能很好地进行扩展.

与此相比,基于哈希的分片方式以范围查询性能的损失为代价,保证了集群中数据的均衡.哈希值的随机性使数据随机分布在每个数据块中,因此也随机分布在不同分片中.但是也正由于随机性,一个范围查询很难确定应该请求哪些分片,通常为了返回需要的结果,需要请求所有分片.如无特殊情况,一般推荐使用 Hash Sharding。

而使用 _id 作为片键是一个不错的选择,因为它是必有的,你可以使用数据文档 _id 的哈希作为片键。这个方案能够是的读和写都能够平均分布,并且它能够保证每个文档都有不同的片键所以数据块能够很精细。似乎还是不够完美,因为这样的话对多个文档的查询必将命中所有的分片。虽说如此,这也是一种比较好的方案了。

理想化的 shard key 可以让 documents 均匀地在集群中分布:98607884d83415418fbfa42513bf72a3.png

显示集群的详细信息:

mongos> ()
--- Sharding Status --- 
  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("5face233793c0e43a638ac50")
  }
  shards:
        {  "_id" : "myshardrs01",  "host" : "myshardrs01/,",  "state" : 1 }
        {  "_id" : "myshardrs02",  "host" : "myshardrs02/,",  "state" : 1 }
  active mongoses:
        "" : 1
  autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours: 
                298 : Success
  databases:
        {  "_id" : "articledb",  "primary" : "myshardrs01",  "partitioned" : true,  "version" : {  "uuid" : UUID("238a84e2-d22e-4159-8cf2-4d38e77a0bba"),  "lastMod" : 1 } }
                
                        shard key: { "age" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                myshardrs01     1
                        { "age" : { "$minKey" : 1 } } -->> { "age" : { "$maxKey" : 1 } } on : myshardrs01 Timestamp(1, 0) 
                
                        shard key: { "nickname" : "hashed" }
                        unique: false
                        balancing: true
                        chunks:
                                myshardrs01     2
                                myshardrs02     2
                        { "nickname" : { "$minKey" : 1 } } -->> { "nickname" : NumberLong("-4611686018427387902") } on : myshardrs01 Timestamp(1, 0) 
                        { "nickname" : NumberLong("-4611686018427387902") } -->> { "nickname" : NumberLong(0) } on : myshardrs01 Timestamp(1, 1) 
                        { "nickname" : NumberLong(0) } -->> { "nickname" : NumberLong("4611686018427387902") } on : myshardrs02 Timestamp(1, 2) 
                        { "nickname" : NumberLong("4611686018427387902") } -->> { "nickname" : { "$maxKey" : 1 } } on : myshardrs02 Timestamp(1, 3) 
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
                config.system.sessions
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                myshardrs01     726
                                myshardrs02     298
                        too many chunks to print, use verbose if you want to force print

查看均衡器是否工作(需要重新均衡时系统才会自动启动,不用管它):

mongos> ()
false

查看当前 Balancer状态:

mongos> ()
true

分片后插入数据测试

测试一(哈希规则):登录mongs后,向comment循环插入1000条数据做测试:

mongos> use articledb
switched to db articledb

mongos> for(var i=1;i<=1000;i++){({_id:i+"",nickname:"BoBo"+i})}
WriteResult({ "nInserted" : 1 })

mongos> ()
1000

提示: js的语法,因为mongo的shell是一个JavaScript的shell。
注意:从路由上插入的数据,必须包含片键,否则无法插入。

分别登陆两个片的主节点,统计文档数量:

# 第一个分片副本集
myshardrs01:PRIMARY> use articledb
switched to db articledb
myshardrs01:PRIMARY> ()
507

# 第二个分片副本集
myshardrs02:PRIMARY> use articledb
switched to db articledb
myshardrs02:PRIMARY> ()
493

可以看到, 1000条数据近似均匀的分布到了2个shard上。是根据片键的哈希值分配的。
这种分配方式非常易于水平扩展:一旦数据存储需要更大空间,可以直接再增加分片即可,同时提升了性能。
使用()查看单个集合的完整情况,mongos执行该命令可以查看该集合的数据分片的情况。
使用()查看本库内所有集合的分片信息。

测试二(范围规则):登录mongs后,向comment循环插入1000条数据做测试:

mongos> use articledb
switched to db articledb

mongos> for(var i=1;i<=20000;i++){({"name":"BoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBoBo"+i,"age":NumberInt(i%120)})}
WriteResult({ "nInserted" : 1 })

mongos> ()
20000

插入成功后,仍然要分别查看两个分片副本集的数据情况。
分片效果:

# 第一个分片副本集
myshardrs01:PRIMARY> db
articledb
myshardrs01:PRIMARY> ()
20000

# 第二个分片副本集
myshardrs02:PRIMARY> db
articledb
myshardrs02:PRIMARY> ()
0

提示:
如果查看状态发现没有分片,则可能是由于以下原因造成了:
1)系统繁忙,正在分片中。
2)数据块(chunk)没有填满,默认的数据块尺寸(chunksize)是64M,填满后才会考虑向其他片的数据块填充数据,因此,为了测试,可以将其改小,这里改为1M,操作如下:

use config
({_id:"chunksize",value:1})

测试完改回来:

({_id:"chunksize",value: 64})

注意:要先改小,再设置分片。为了测试,可以先删除集合,重新建立集合的分片策略,再插入数据测试即可。

再增加一个路由节点

创建保存日志的文件夹:

#-----------mongos02
mkdir -p /home/mongodb/sharded_cluster/mymongos_27117/log

新建或修改配置文件:

# vim /home/mongodb/sharded_cluster/mymongos_27117/
systemLog:
  destination: file
  path: /home/mongodb/sharded_cluster/mymongos_27117/log/mongod.log
  logAppend: true
processManagement:
  fork: true
  pidFilePath: /home/mongodb/sharded_cluster/mymongos_27117/log/mongod.pid
net:
  bindIp: 192.168.0.253
  port: 27117
sharding:
  configDB: myconfigrs/,,

启动mongos2:

/usr/bin/mongos -f /home/mongodb/sharded_cluster/mymongos_27117/

使用mongo客户端登录27117,发现,第二个路由无需配置,因为分片配置都保存到了配置服务器中了。

Compass 连接分片集群

详细看文档:

清除所有的节点数据(备用)

如果在搭建分片的时候有操作失败或配置有问题,需要重新来过的,可以进行如下操作:
第一步:查询出所有的测试服务节点的进程,根据上述的进程编号,使用kill -9 pid依次中断进程
第二步:清除所有的节点的数据,也就是删除存储数据的目录下的内容
第三步:查看或修改有问题的配置
第四步:依次启动所有节点,不包括路由节点
第五步:对两个数据分片副本集和一个配置副本集进行初始化和相关配置
第六步:检查路由mongos的配置,并启动mongos
第七步:mongo登录mongos,在其上进行相关操作

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值