MongoDB分片集群部署,使用

一、准备环境

  • 1、建议使用新安装的虚拟机,不然会报各种离谱错误。
  • 2、建议使用同一个版本的数据库,不然数据字典不一致可能导致各种问题。

1 服务器详情

1.1 服务器配置(示例)
信息
操作系统 Red Hat Enterprise Linux Server release 7.8 (Maipo)
CPU 1(Thread) × 1(Core) × 4(Socket)
内存 4GB
磁盘 40G
1.2 部署情况(示例)
服务器IP OS登录信息 节点状态、部署服务及对应端口号
192.168.6.107 mongod/mongod shard1,sard2,config,mongos
192.168.6.108 mongod/mongod shard1,sard2,config,mongos
192.168.6.111 mongod/mongod shard1,sard2,config,mongos

生产中使用的常见分片集群架构:
在这里插入图片描述

  • 数据分片:分片用于存储真正的数据,并提供最终的数据读写访问。分片仅仅是一个逻辑的概念,它可以是一个单独的mongod实例,也可以是一个复制集。图中的Shard1、Shard2都是一个复制集分片。在生产环境中也一般会使用复制集的方式,这是为了防止数据节点出现单点故障

  • 配置服务器(Config Server):配置服务器包含多个节点,并组成一个复制集结构,对应于图中的ConfigReplSet。配置复制集中保存了整个分片集群中的元数据,其中包含各个集合的分片策略,以及分片的路由表等。

  • 查询路由(mongos):mongos是分片集群的访问入口,其本身并不持久化数据。mongos启动后,会从配置服务器中加载元数据。之后mongos开始提供访问服务,并将用户的请求正确路由到对应的分片。在分片集群中可以部署多个mongos以分担客户端请求的压力。

2 版本

服务名称 版本信息
MongoDB “6.0.16”

3 部署目录

名称 目录位置
shard目录 /data/shard/
config目录 /data/config/
mongos目录 /data/mongos/

二、搭建分片集群

在这里插入图片描述
图片来源:

https://s2.51cto.com/images/blog/202405/25000834_6650bb82170d373715.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/format,webp/resize,m_fixed,w_1184

2.1、使用DNS服务解析IP

在3台虚拟机上执行以下命令,防止ip变动导致连接失败

echo "192.168.6.107  mongo1 mongo01.com mongo02.com" >> /etc/hosts
echo "192.168.6.108 mongo2 mongo03.com mongo04.com" >> /etc/hosts
echo "192.168.6.111 mongo3 mongo05.com mongo06.com" >> /etc/hosts

创建安装目录

[root@postgres /]# mkdir -p /data/shard1/db  /data/shard1/log   /data/config/db  /data/config/log
[root@postgres /]# mkdir -p /data/shard2/db  /data/shard2/log   /data/mongos/
[root@postgres /]# cd data
[root@postgres data]# ls
config  mongos  shard1  shard2

2.2、安装集群

环境准备,请参考之前的文章

2.2.1、在三个节点上执行相同的命令,安装shard1分片

[root@postgres /]# mongod --bind_ip 0.0.0.0 --replSet shard1 --dbpath /data/shard1/db --logpath /data/shard1/log/mongod.log --port 27010 --fork --shardsvr
about to fork child process, waiting until server is ready for connections.
forked process: 4408
child process started successfully, parent exiting

##查看监听端口,判断数据库实例是否启动成功

[root@postgres /]# netstat -tunl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:27010           0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN     
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN   

登录shard1分片的mongo01节点,初始化主备复制集群

命令:

# 进入mongo shell
mongo mongo01.com:27010
#shard1复制集节点初始化
rs.initiate({
   
    _id: "shard1",
    "members" : [
    {
   
        "_id": 0,
        "host" : "mongo01.com:27010"
    },
    {
   
        "_id": 1,
        "host" : "mongo03.com:27010"
    },
    {
   
        "_id": 2,
        "host" : "mongo05.com:27010"
    }
    ]
})
#查看复制集状态
rs.status()
[root@postgres /]# mongosh mongo01.com:27010
Current Mongosh Log ID: 66a1a8f87459571da0482f8a
Connecting to:          mongodb://mongo01.com:27010/?directConnection=true&appName=mongosh+2.2.12
Using MongoDB:          6.0.16
Using Mongosh:          2.2.12

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

------
   The server generated these startup warnings when booting
   2024-07-25T09:22:13.739+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
   2024-07-25T09:22:13.739+08:00: You are running this process as the root user, which is not recommended
   2024-07-25T09:22:13.739+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never' in this binary version
   2024-07-25T09:22:13.739+08:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never' in this binary version
   2024-07-25T09:22:13.739+08:00: Soft rlimits for open file descriptors too low
------


Deprecation warnings:
  - Using mongosh on the current operating system is deprecated, and support may be removed in a future release.
See https://www.mongodb.com/docs/mongodb-shell/install/#supported-operating-systems for documentation on supported platforms.
test> rs.initiate({
   
    _id: "shard1",
...     _id: "shard1",
...     "members" : [
...     {
   
...         "_id": 0,
...         "host" : "mongo01.com:27010"
    },
...     },
    {
   
...     {
   
...         "_id": 1,
...         "host" : "mongo03.com:27010"
...     },
...     {
   
...         "_id": 2,
...         "host" : "mongo05.com:27010"
...     }
...     ]
... })
{
    ok: 1 }

查看shard1分片的主、备集群状态信息

shard1 [direct: secondary] test> rs.status()
{
   
  set: 'shard1',
  date: ISODate('2024-07-25T01:24:32.227Z'),
  myState: 1,
  term: Long('1'),
  syncSourceHost: '',
  syncSourceId: -1,
  heartbeatIntervalMillis: Long('2000'),
  majorityVoteCount: 2,
  writeMajorityCount: 2,
  votingMembersCount: 3,
  writableVotingMembersCount: 3,
  optimes: {
   
    lastCommittedOpTime: {
    ts: Timestamp({
    t: 1721870665, i: 1 }), t: Long('1') },
    lastCommittedWallTime: ISODate('2024-07-25T01:24:25.038Z'),
    readConcernMajorityOpTime: {
    ts: Timestamp({
    t: 1721870665, i: 1 }), t: Long('1') },
    appliedOpTime: {
    ts: Timestamp({
    t: 1721870665, i: 1 }), t: Long('1') },
    durableOpTime: {
    ts: Timestamp({
    t: 1721870665, i: 1 }), t: Long('1') },
    lastAppliedWallTime: ISODate('2024-07-25T01:24:25.038Z'),
    lastDurableWallTime: ISODate('2024-07-25T01:24:25.038Z')
  },
  lastStableRecoveryTimestamp: Timestamp({
    t: 1721870645, i: 1 }),
  electionCandidateMetrics: {
   
    lastElectionReason: 'electionTimeout',
    lastElectionDate: ISODate('2024-07-25T01:23:24.996Z'),
    electionTerm: Long('1'),
    lastCommittedOpTimeAtElection: {
    ts: Timestamp({
    t: 1721870594, i: 1 }), t: Long('-1') },
    lastSeenOpTimeAtElection: {
    ts: Timestamp({
    t: 1721870594, i: 1 }), t: Long('-1') },
    numVotesNeeded: 2,
    priorityAtElection: 1,
    electionTimeoutMillis: Long('10000'),
    numCatchUpOps: Long('0'),
    newTermStartDate: ISODate('2024-07-25T01:23:25.020Z'),
    wMajorityWriteAvailabilityDate: ISODate('2024-07-25T01:23:26.007Z')
  },
  members: [
    {
   
      _id: 0,
      name: 'mongo01.com:27010',
      health: 1,
      state: 1,
      stateStr: 'PRIMARY',
      uptime: 139,
      optime: {
    ts: Timestamp({
    t: 1721870665, i: 1 }), t: Long('1') },
      optimeDate: ISODate('2024-07-25T01:24:25.000Z'),
      lastAppliedWallTime: ISODate('2024-07-25T01:24:25.038Z'),
      lastDurableWallTime: ISODate('2024-07-25T01:24:25.038Z'),
      syncSourceHost: '',
      syncSourceId: -1,
      infoMessage: '',
      electionTime: Timestamp({
    t: 1721870604, i: 1 }),
      electionDate: ISODate('2024-07-25T01:23:24.000Z'),
      configVersion: 1,
      configTerm: 1,
      self: true,
      lastHeartbeatMessage: ''
    },
    {
   
      _id: 1,
      name: 'mongo03.com:27010',
      health: 1,
      state: 2,
      stateStr: 'SECONDARY',
      uptime: 77,
      optime: {
    ts: Timestamp({
    t: 1721870665, i: 1 }), t: Long('1') },
      optimeDurable: {
    ts: Timestamp({
    t: 1721870665, i: 1 }), t: Long('1') },
      optimeDate: ISODate('2024-07-25T01:24:25.000Z'),
      optimeDurableDate: ISODate('2024-07-25T01:24:25.000Z'),
      lastAppliedWallTime: ISODate('2024-07-25T01:24:25.038Z'),
      lastDurableWallTime: ISODate('2024-07-25T01:24:25.038Z'),
      lastHeartbeat: ISODate('2024-07-25T01:24:31.056Z'),
      lastHeartbeatRecv: ISODate('2024-07-25T01:24:32.057Z'),
      pingMs: Long('0'),
      lastHeartbeatMessage: '',
      syncSourceHost: 'mongo01.com:27010',
      syncSourceId: 0,
      infoMessage: '',
      configVersion: 1,
      configTerm: 1
    },
    {
   
      _id: 2,
      name: 'mongo05.com:27010',
      health: 1,
      state: 2,
      stateStr: 'SECONDARY',
      uptime: 77,
      optime: {
    ts: Timestamp({
    t: 1721870665, i: 1 }), t: Long('1') },
      optimeDurable: {
    ts: Timestamp({
    t: 1721870665, i: 1 }), t: Long('1') },
      optimeDate: ISODate('2024-07-25T01:24:25.000Z'),
      optimeDurableDate: ISODate('2024-07-25T01:24:25.000Z'),
      lastAppliedWallTime: ISODate('2024-07-25T01:24:25.038Z'),
      lastDurableWallTime: ISODate('2024-07-25T01:24:25.038Z'),
      lastHeartbeat: ISODate('2024-07-25T01:24:31.056Z'),
      lastHeartbeatRecv: ISODate('2024-07-25T01:24:32.057Z'),
      pingMs: Long('0'),
      lastHeartbeatMessage: '',
      syncSourceHost: 'mongo01.com:27010',
      syncSourceId: 0,
      infoMessage: '',
      configVersion: 1,
      configTerm: 1
    }
  ],
  ok: 1,
  '$clusterTime': {
   
    clusterTime: Timestamp({
    t: 1721870665, i: 1 }),
    signature: {
   
      hash: Binary.createFromBase64('AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 0),
      keyId: Long('0')
    }
  },
  operationTime: Timestamp({
    t: 1721870665, i: 1 })
}
shard1 [direct: primary] test> 

2.2.2、在三个节点上执行相同的命令,安装config主备集群


[root@postgres /]# mongod --bind_ip 0.0.0.0 --replSet config --dbpath /data/config/db \
--logpath /data/config/log/mongod.log --port 27019 --fork \
--configsvr

[root@postgres /]# netstat -tunl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:27010           0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:27019           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN     

初始化config主、备集群

命令:

# 进入mongo shell
mongo mongo01.com:27019
#config复制集节点初始化
rs.initiate({
   
    _id: "config",
    "members" : [
    {
   
        "_id": 0,
        "host" : "mongo01.com:27019"
    },
    {
   
        "_id": 1,
        "host" : "mongo03.com:27019"
    },
    {
   
        "_id": 2,
        "host" : "mongo05.com:27019"
    }
    ]
})
[root@postgres data]# mongosh mongo01.com:27019
Current Mongosh Log ID: 66a1bb5ca7a9cd7327482f8a
Connecting to:          mongodb://mongo01.com:27019/?directConnection=true&appName=mongosh+2.2.12
Using MongoDB:          6.0.16
Using Mongosh:          2.2.12

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

------
   The server generated these startup warnings when booting
   2024-07-25T10:39:35.878+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
   2024-07-25T10:39:35.878+08:00: You are running this process as the root user, which is not recommended
   2024-07-25T10:39:35.878+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never' in this binary version
   2024-07-25T10:39:35.878+08:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never' in this binary version
   2024-07-25T10:39:35.878+08:00: Soft rlimits for open file descriptors too low
------


Deprecation warnings:
  - Using mongosh on the current operating system is deprecated, and support may be removed in a future release.
See https://www.mongodb.com/docs/mongodb-shell/install/#supported-operating-systems for documentation on supported platforms.
test> rs.initiate({
   
    _id: "config",
...     _id: "config",
...     "members" : [
...     {
   
...         "_id": 0,
...         "host" : "mongo01.com:27019"
...     },
...     {
   
...         "_id": 1,
...         "host" : "mongo03.com:27019"
...     },
...     {
   
...         "_id": 2,
...         "host" : "mongo05.com:27019"
...     }
...     ]
... })
{
    ok: 1, lastCommittedOpTime: Timestamp({
    t: 1721875301, i: 1 }) }

查看config 集群状态信息

config [direct: other] test> rs.status()
{
   </
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值