【MongoDB】集群搭建实战 | 副本集 Replica-Set | 分片集群 Shard-Cluster | 安全认证

文章目录

    • MongoDB 集群架构
      • 副本集
        • 主节点选举原则
        • 搭建副本集
          • 主节点
          • 从节点
          • 仲裁节点
        • 连接节点
          • 添加副本从节点
          • 添加仲裁者节点
          • 删除节点
        • 副本集读写操作
        • 副本集中的方法
      • 分片集群
        • 分片集群架构目标
          • 第一个副本集
          • 第二个副本集
          • 配置集
          • 初始化副本集
          • 路由集
            • 添加分片
            • 开启分片
            • 集合分片
            • 删除分片
      • 安全认证
        • MongoDB用户和角色权限
        • 副本集安全认证
          • 创建副本集认证的key文件
          • 修改配置文件指定keyfile
        • 分片集群安全认证

MongoDB 集群架构

副本集

MongoDB副本集(Replica Set)是一组维护相同数据集的 MongoDB 实例,它可以提供数据的冗余和高可用性。

  • 副本集由一个主节点(Primary)和多个从节点(Secondary)组成。

    • 客户端所有写操作都会发送到主节点,主节点会记录oplog,然后将写操作同步到从节点。
    • 从节点可以进行读操作和选举新的主节点,从而在主节点故障时实现高可用性。
  • 副本集可以有一个或多个仲裁节点(Arbiter)。

    • 仲裁节点不持有数据,只参与投票来进行选举。

类似于有自动故障恢复功能的主从集群。用多台机器进行同一数据的异步和同步,从而使得多台机器拥有同一数据的多个副本。并且当主库宕机时不需要用户干预的情况下自动切换其他备份服务器做主库。还可以利用副本服务器做只读服务器,实现读写分离,提高负载。通常在生产环境中使用3或5节点副本集,且部署主从节点到不同机器上,可以提供更高的数据冗余和可用性。

副本集使用(主-从-仲裁)节点的架构:

  • 数据冗余: 数据同步到从节点上,提高数据可用性
  • 高可用性: 主节点故障时可以快速选举新的主节点
  • 读写分离: 不同类型的压力分别在不同的节点上执行,从节点可以承担读操作的负载
  • 数据分发: 将数据从一个区域复制到另一个区域,减少另一个区域的读延迟
  • 异地容灾: 在数据中心故障时快速切换到异地

主从复制和副本集区别:

主从集群和副本集最大区别就是副本集没有固定的“主节点”;整个集群会选出一个“主节点”,当其挂掉后,又在剩下的从节点中选中其他节点为“主节点”,副本集总有一个活跃点 {主、primary} 和一个或多个备份节点 {从、secondary}。


主节点选举原则

MongoDB在副本集中,会自动进行主节点的选举,主节点选举的触发条件

  • 主节点故障
  • 主节点网络不可达(默认心跳信息为10秒)
  • 人工干预(rs.stepDown(600))

一旦触发选举,就要根据一定的规则来选主节点。

选举规则是根据票数来决定谁获胜:

  • 票数最高,且获得了"大多数"成员的投票支持的节点获胜

“大多数”的定义为:假设副本集内投票成员数量为N,则大多数为 N/2+1。例如:3个投票成员,则大多数的值是2。当副本集内存活的数量不足大多数时,整个副本集将无法选举出Primary,副本集将无法提供写服务,处于只读状态。

  • 若票数相同,且都获得了"大多数"成员的投票支持的,数据新的节点获胜。

数据的新旧是通过操作日志oplog来对比的。


搭建副本集

目标:搭建一主一副本一仲裁:

【MongoDB】集群搭建实战 | 副本集 Replica-Set | 分片集群 Shard-Cluster | 安全认证_第1张图片

主节点
  • 建立存放数据和日志的目录
mkdir -p ~/mongodb/replicaset/rs_27017/log ~/mongodb/replicaset/rs_27017/data/db
  • 新建或修改配置文件
vim ~/mongodb/replicaset/rs_27017/mongod.conf

mongod.conf:

  • 注意: /home/cauchy 这里是用户的家目录不能用 ~ 代替,cd && pwd 查看家目录路径。
# Where and how to store data.
storage:
  dbPath: /home/cauchy/mongodb/replicaset/rs_27017/data/db

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /home/cauchy/mongodb/replicaset/rs_27017/log/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0

# how the process runs
processManagement:
  fork: true
  timeZoneInfo: /usr/share/zoneinfo
  pidFilePath: /home/cauchy/mongodb/replicaset/rs_27017/log/mongod.pid

replication:
  replSetName: rs

#sharding:

# security:
#   authorization: enabled
  • 启动节点服务
sudo mongod -f ~/mongodb/replicaset/rs_27017/mongod.conf

在这里插入图片描述


从节点
mkdir -p ~/mongodb/replicaset/rs_27018/log ~/mongodb/replicaset/rs_27018/data/db
vim ~/mongodb/replicaset/rs_27018/mongod.conf

mongod.conf:

# Where and how to store data.
storage:
  dbPath: /home/cauchy/mongodb/replicaset/rs_27018/data/db

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /home/cauchy/mongodb/replicaset/rs_27018/log/mongod.log

# network interfaces
net:
  port: 27018
  bindIp: 0.0.0.0

# how the process runs
processManagement:
  fork: true
  timeZoneInfo: /usr/share/zoneinfo
  pidFilePath: /home/cauchy/mongodb/replicaset/rs_27018/log/mongod.pid

replication:
  replSetName: rs

#sharding:

# security:
#   authorization: enabled
sudo mongod -f ~/mongodb/replicaset/rs_27018/mongod.conf

仲裁节点
mkdir -p ~/mongodb/replicaset/rs_27019/log ~/mongodb/replicaset/rs_27019/data/db
vim ~/mongodb/replicaset/rs_27019/mongod.conf

mongod.conf:

# Where and how to store data.
storage:
  dbPath: /home/cauchy/mongodb/replicaset/rs_27019/data/db

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /home/cauchy/mongodb/replicaset/rs_27019/log/mongod.log

# network interfaces
net:
  port: 27019
  bindIp: 0.0.0.0

# how the process runs
processManagement:
  fork: true
  timeZoneInfo: /usr/share/zoneinfo
  pidFilePath: /home/cauchy/mongodb/replicaset/rs_27019/log/mongod.pid

replication:
  replSetName: rs

#sharding:

# security:
#   authorization: enabled
sudo mongod -f ~/mongodb/replicaset/rs_27019/mongod.conf
连接节点
  • 连接端口(27017)节点,使之成为主节点
mongosh --port=27017
  • 初始化副本集
rs.initiate() # 使用默认配置初始化

在这里插入图片描述

  • 查看副本集信息

使用 rs.conf()rs.status() 查看相应的信息

【MongoDB】集群搭建实战 | 副本集 Replica-Set | 分片集群 Shard-Cluster | 安全认证_第2张图片
可以看到目前副本集中只有一个成员(members),主机(ac34057c51af:27017),仲裁节点(arbiterOnly:false),优先级权值(priority:1),并且当前(27017)节点初始化后变为主节点(primary)。


添加副本从节点

在主节点中添加从节点,将其他成员加入到副本集中

语法:

rs.add(host, arbiterOnly)
Parameter Type Description
host string or document 要添加到副本集的新成员。指定为字符串或配置文档:1)如果是一个字符串,则需要指定新成员的主机名和可选的端口号;2)如果是一个文档,请指定在members数组中找到的副本集成员配置文档。您必须在成员配置文档中指定主机字段。有关文档配置字段的说明,详见下方文档:“主机成员的配置文档”
arbiterOnly boolean 可选的。仅在值为字符串时适用。如果为true,则添加的主机是仲裁者。

主机成员的配置文档:(可通过 rs.conf() 查看)

{
  _id:<int>,
  host:<string>,
  arbiterOnly:<boolean>,
  buildIndexes:<boolean>,
  hidden:<boolean>,
  priority:<number>,
  tags:<document>,
  slaveDelay:<int>,
  votes:<number>
}
  • 将27018的副本节点加添加到副本集中

主机名直接用主机成员的配置文档中的即可

rs.add("ac34057c51af:27018", false)

【MongoDB】集群搭建实战 | 副本集 Replica-Set | 分片集群 Shard-Cluster | 安全认证_第3张图片


添加仲裁者节点
rs.add(host,arbiterOnly) 或者 rs.addArb(host)

如下添加仲裁节点 rs.addArb("ac34057c51af:27019")

在这里插入图片描述

【错误信息】:【Reconfig attempted to install a config that would change the implicit default write concern. Use the setDefaultRWConcern command to set a cluster-wide write concern and try the reconfig again.】

【解决】:设置全局默认写入关注点,详细的 Write Concern 和 Read Concern 可参考官网文档。

  • 以下操作将全局写入关注设置为:

    • w: 2
db.adminCommand({
  "setDefaultRWConcern" : 1,
  "defaultWriteConcern" : {
    "w" : 2
  }
})

设置完成后,再次执行:

rs.addArb("ac34057c51af:27019")

【MongoDB】集群搭建实战 | 副本集 Replica-Set | 分片集群 Shard-Cluster | 安全认证_第4张图片


删除节点

从副本集中删除节点

rs.remove(host)

副本集读写操作
  • 主节点
use test
db.comment.insert({userid: "001", username: "cauchy", content: "专家有言:早上空腹不能吃饭", timestamp: new Date()})

成功读写操作:

【MongoDB】集群搭建实战 | 副本集 Replica-Set | 分片集群 Shard-Cluster | 安全认证_第5张图片

  • 从节点
mongosh --port=27018

在这里插入图片描述
目前无法读取任何数据,需要主动承认自己是从节点:

db.getMongo().setReadPref("primaryPreferred")

执行成功,从节点只读不可写:

【MongoDB】集群搭建实战 | 副本集 Replica-Set | 分片集群 Shard-Cluster | 安全认证_第6张图片


Read Preference:Read Preference — MongoDB Manual

readPreference 主要控制客户端 driver 从副本集(Replica Set)读数据的时候如何路由,如下图,这个特性可以方便的配置读写分离、就近去读等策略。

【MongoDB】集群搭建实战 | 副本集 Replica-Set | 分片集群 Shard-Cluster | 安全认证_第7张图片

读策略各模式:

  1. primary:只从 primary 节点读数据(默认模式)

  2. primaryPreferred:先主后从。优先从 primary 读取,primary 不可用时从 secondary 读

  3. secondary:只从副本集中 secondary 节点读数据

  4. secondaryPreferred:先从后主。优先从 secondary 读取,如果 secondary 不可用时就从 primary 读

  5. nearest:就近。根据网络距离,就近读取,根据客户端与服务端的 PingTime 实现


副本集中的方法

可以通过 rs.help() 返回副本集功能的基本帮助文档

【MongoDB】集群搭建实战 | 副本集 Replica-Set | 分片集群 Shard-Cluster | 安全认证_第8张图片


分片集群

分片是一种跨多台机器分布数据的方法,MongoDB使用分片来支持具有非常大的数据集和高吞吐量操作的部署。
换句话说:分片就是将数据拆分,将其分散到不同的机器上的过程。

  • 分片包含的组件

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

【MongoDB】集群搭建实战 | 副本集 Replica-Set | 分片集群 Shard-Cluster | 安全认证_第9张图片


分片集群架构目标

【MongoDB】集群搭建实战 | 副本集 Replica-Set | 分片集群 Shard-Cluster | 安全认证_第10张图片


第一个副本集
  • 创建存放数据和日志的目录
mkdir -p ~/mongodb/sharded_cluster/shardrs_01_27018/data/db ~/mongodb/sharded_cluster/shardrs_01_27018/log
mkdir -p ~/mongodb/sharded_cluster/shardrs_01_27118/data/db ~/mongodb/sharded_cluster/shardrs_01_27118/log
mkdir -p ~/mongodb/sharded_cluster/shardrs_01_27218/data/db ~/mongodb/sharded_cluster/shardrs_01_27218/log
  • 新建或修改配置文件
vim ~/mongodb/sharded_cluster/shardrs_01_27018/mongod.conf
vim ~/mongodb/sharded_cluster/shardrs_01_27118/mongod.conf
vim ~/mongodb/sharded_cluster/shardrs_01_27218/mongod.conf
  1. shardrs_01_27018/mongod.conf
# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /home/cauchy/mongodb/sharded_cluster/shardrs_01_27018/data/db
#  engine:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /home/cauchy/mongodb/sharded_cluster/shardrs_01_27018/log/mongod.log

# network interfaces
net:
  port: 27018
  bindIp: 0.0.0.0

# how the process runs
processManagement:
  fork: true
  timeZoneInfo: /usr/share/zoneinfo
  pidFilePath: /home/cauchy/mongodb/sharded_cluster/shardrs_01_27018/log/mongod.pid

# security:
#   authorization: enabled

#operationProfiling:

replication:
  replSetName: shardrs_01

sharding:
  clusterRole: shardsvr

#sharding:

## Enterprise-Only Options:

#auditLog:
  1. shardrs_01_27118/mongod.conf
  2. shardrs_01_27218/mongod.conf

同理修改即可。


第二个副本集
mkdir -p ~/mongodb/sharded_cluster/shardrs_02_27318/data/db ~/mongodb/sharded_cluster/shardrs_02_27318/log
mkdir -p ~/mongodb/sharded_cluster/shardrs_02_27418/data/db ~/mongodb/sharded_cluster/shardrs_02_27418/log
mkdir -p ~/mongodb/sharded_cluster/shardrs_02_27518/data/db ~/mongodb/sharded_cluster/shardrs_02_27518/log
vim ~/mongodb/sharded_cluster/shardrs_02_27318/mongod.conf
vim ~/mongodb/sharded_cluster/shardrs_02_27418/mongod.conf
vim ~/mongodb/sharded_cluster/shardrs_02_27518/mongod.conf
  1. shardrs_02_27318/mongod.conf
# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /home/cauchy/mongodb/sharded_cluster/shardrs_02_27318/data/db
#  engine:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /home/cauchy/mongodb/sharded_cluster/shardrs_02_27318/log/mongod.log

# network interfaces
net:
  port: 27318
  bindIp: 0.0.0.0

# how the process runs
processManagement:
  fork: true
  timeZoneInfo: /usr/share/zoneinfo
  pidFilePath: /home/cauchy/mongodb/sharded_cluster/shardrs_02_27318/log/mongod.pid

# security:
#   authorization: enabled

#operationProfiling:

replication:
  replSetName: shardrs_02

sharding:
  clusterRole: shardsvr

#sharding:

## Enterprise-Only Options:

#auditLog:
  1. shardrs_02_27418/mongod.conf
  2. shardrs_02_27518/mongod.conf

同理修改即可。


配置集
mkdir -p ~/mongodb/sharded_cluster/configrs_27019/data/db ~/mongodb/sharded_cluster/configrs_27019/log
mkdir -p ~/mongodb/sharded_cluster/configrs_27119/data/db ~/mongodb/sharded_cluster/configrs_27119/log
mkdir -p ~/mongodb/sharded_cluster/configrs_27219/data/db ~/mongodb/sharded_cluster/configrs_27219/log
  1. configrs_27019/mongod.conf
# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /home/cauchy/mongodb/sharded_cluster/configrs_27019/data/db
#  engine:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /home/cauchy/mongodb/sharded_cluster/configrs_27019/log/mongod.log

# network interfaces
net:
  port: 27019
  bindIp: 0.0.0.0

# how the process runs
processManagement:
  fork: true
  timeZoneInfo: /usr/share/zoneinfo
  pidFilePath: /home/cauchy/mongodb/sharded_cluster/configrs_27019/log/mongod.pid

# security:
#   authorization: enabled

#operationProfiling:

replication:
  replSetName: configrs

sharding:
  clusterRole: configsvr

## Enterprise-Only Options:

#auditLog:
  1. configrs_27119/mongod.conf
  2. configrs_27219/mongod.conf

同理修改即可。


初始化副本集
  • 两个副本集的初始化,和上文副本集中,连接节点处讲解一致

  • 配置集没有仲裁节点,将两个节点以从节点的方式加入即可


路由集

mongos服务,不是mongod服务

  • 仅需要创建日志目录
mkdir -p ~/mongodb/sharded_cluster/mongos_27017/log
mkdir -p ~/mongodb/sharded_cluster/mongos_27117/log
  • 新建或修改配置文件
vim ~/mongodb/sharded_cluster/mongos_27017/mongos.conf
vim ~/mongodb/sharded_cluster/mongos_27117/mongos.conf
  1. mongos_27017/mongos.conf
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /home/cauchy/mongodb/sharded_cluster/mongos_27017/log/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0

# how the process runs
processManagement:
  fork: true
  timeZoneInfo: /usr/share/zoneinfo
  pidFilePath: /home/cauchy/mongodb/sharded_cluster/mongos_27017/log/mongod.pid

# security:
#   authorization: enabled

#operationProfiling:

# replication:

sharding:
  configDB: configrs/ac34057c51af:27019,ac34057c51af:27119,ac34057c51af:27219

#sharding:

## Enterprise-Only Options:

#auditLog:
  1. mongos_27117/mongos.conf
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /home/cauchy/mongodb/sharded_cluster/mongos_27117/log/mongod.log

# network interfaces
net:
  port: 27117
  bindIp: 0.0.0.0

# how the process runs
processManagement:
  fork: true
  timeZoneInfo: /usr/share/zoneinfo
  pidFilePath: /home/cauchy/mongodb/sharded_cluster/mongos_27117/log/mongod.pid

# security:
#   authorization: enabled

#operationProfiling:

# replication:

sharding:
  configDB: configrs/ac34057c51af:27019,ac34057c51af:27119,ac34057c51af:27219

#sharding:

## Enterprise-Only Options:

#auditLog:
  • 启动路由节点 mongos 服务
sudo mongos -f ~/mongodb/sharded_cluster/mongos_27017/mongos.conf
sudo mongos -f ~/mongodb/sharded_cluster/mongos_27117/mongos.conf

此时路由还不能找到分片,要添加分片到路由中。


添加分片

语法:

sh.addShard("IP:Port")
  • 添加第一套副本集

【MongoDB】集群搭建实战 | 副本集 Replica-Set | 分片集群 Shard-Cluster | 安全认证_第11张图片

  • 添加第二套副本集

【MongoDB】集群搭建实战 | 副本集 Replica-Set | 分片集群 Shard-Cluster | 安全认证_第12张图片

开启分片

sh.enableSharding("库名") 需要先对对库开启分片功能

【MongoDB】集群搭建实战 | 副本集 Replica-Set | 分片集群 Shard-Cluster | 安全认证_第13张图片

集合分片

对集合分片,必须使用 sh.shardCollection() 方法指定集合和分片键

  • 片键(Shard Key)是每条记录都必须包含的,且建立了索引的单个字段或复合字段,MongoDB 按照片键将数据划分到不同的数据块中,并将数据块均衡地分布到所有分片中。

    • MongoDB 使用基于哈希的分片方式(随机平均分配)或者基于范围的分片方式(数值大小分配)

语法:

sh.shardCollection(namespace,key,unique)
Parameter Type Description
namespace string 要(分片)共享对目标集合对命名空间,格式:.
key document 用作分片键的索引规范文档。shard键决定MongoDB如何在shard之间分发文档。除非集合为空,否则索引必须在shard collection命令之前存在。如果集合为空,则MongoDB在对集合进行分片之前创建索引,前提是支持分片键的索引不存在。简单来说:由包含字段和该字段的索引遍历方向的文档组成。
unique boolean 当值为true,片键字段上会限制为确保是唯一索引,哈希策略片键不支持唯一索引,默认为false

测试 test_db 库的 comment 集合,以 username 为键,以哈希策略来分片:

sh.shardCollection("test_db.comment", {"username": "hashed"})

【MongoDB】集群搭建实战 | 副本集 Replica-Set | 分片集群 Shard-Cluster | 安全认证_第14张图片


  • 分片规则一:哈希策略

对于基于哈希的分片,MongoDB 计算一个学段的哈希值,并用这个哈希值来创建数据块。

在使用基于哈希分片的系统中,拥有"相近"片键的文档很可能不会存储在同一个数据块中,因此数据的分离性更好一些

测试哈希策略:

for(var i=0;i<1000;i++){db.comment.insert({userid: i, username: "cauchy"+i})}

在这里插入图片描述

在分片1中查看:

mongosh --port=27018
use test_db
db.comment.countDocuments()

在这里插入图片描述

在分片2中查看:

在这里插入图片描述


  • 分片规则二:范围策略

对于基于范围的分片,MongoDB按照片键的范围把数据分成不同部分。假设有一个数字的片键想象一个从负无穷到正无穷的直线,每一个片键的值都在直线上画了一个点。MongoDB 把这条直线划分为更短的不重叠的片段,并称之为数据块,每个数据块包含了片键在一定范围内的数据。

在使用片键做范围划分的系统中,拥有相近"片键的文档很可能存储诸在同一个数据块中,因此也会存储在同一个分片中。

sh.shardCollection("test_db.author", {"age": 1})

【MongoDB】集群搭建实战 | 副本集 Replica-Set | 分片集群 Shard-Cluster | 安全认证_第15张图片


删除分片
use config
db.databases.remove({"_id": "test_db"})
db.shards.remove({_id: "shardrs_01"})
db.shards.remove({_id: "shardrs_02"})

第二个路由节点直接同第一个创建即可,开启后不需要分配,由配置服务自动同步


安全认证

MongoDB用户和角色权限

参考上篇博客

副本集安全认证
创建副本集认证的key文件

【MongoDB】集群搭建实战 | 副本集 Replica-Set | 分片集群 Shard-Cluster | 安全认证_第16张图片

  • 生成一个key文件到当前文件夹中

可以使用任何方法生成密钥文件。例如,以下操作使用 openssl 生成密码文件,然后使用 chmod 来更改文件权限
仅为文件所有者提供读取权限:

openssl rand -base64 128 > ./mongo.keyfile
chmod 400 ./mongo.keyfile

所有副本集节点都必须要用同一份keyfile,一般是在一台机器上生成,然后拷贝到其他机器上,且必须有读的权
限。
一定要保证密钥文件一致,文件位置随便。但是为了方便查找,建议每台机器都防放到一个固定的位置,都防放到和配
置文件一起的目录中。

  • 将该文件分别拷贝到多个目录中
cp mongo.keyfile ~/mongodb/replicaset/rs_27017
cp mongo.keyfile ~/mongodb/replicaset/rs_27018
cp mongo.keyfile ~/mongodb/replicaset/rs_27019

修改配置文件指定keyfile
...
security:
  keyFile: /home/cauchy/mongodb/replicaset/rs_27017/mongo.keyfile
  authorization: enabled
...
...
security:
  keyFile: /home/cauchy/mongodb/replicaset/rs_27018/mongo.keyfile
  authorization: enabled
...
...
security:
  keyFile: /home/cauchy/mongodb/replicaset/rs_27019/mongo.keyfile
  authorization: enabled
...
分片集群安全认证

每个节点上都要添加 keyfile,其他步骤同副本集一样。


参考:https://www.bilibili.com/video/BV1bJ411x7mq

你可能感兴趣的:(MongoDB,mongodb,安全,数据库,集群,分布式)