MongoDB分片集群部署及实践

news2024/9/21 17:46:24

文章目录

  • MongoDB分片集群部署及实践
  • 一、前言
  • 二、了解分片集群
    • 1.简介
    • 2.分片集群组件
    • 3.副本集模式
  • 三.安装部署
    • 1.分片集群环境
    • 2.安装
      • 1)CPU 检测
      • 2)修改机器名称
      • 3)配置 hosts 文件
      • 4)配置防火墙
      • 5)创建目录
      • 6)安装 MongoDB
    • 3.Config Server 配置
      • 1)mgd-config-01 机器
      • 2)mgd-config-02 机器
      • 3)mgd-config-03 机器
      • 4)配置config副本集
    • 4.Shard1 配置
      • 1)mgd-shard-01 机器
      • 2)mgd-shard-02 机器
      • 3)mgd-shard-03 机器
      • 4)配置shard1副本集
    • 5.Shard2 配置
      • 1)mgd-shard-04 机器
      • 2)mgd-shard-05 机器
      • 3)mgd-shard-06 机器
      • 4)配置shard2副本集
    • 6.Mongos 配置
      • 1)mgd-mongos-01 机器
      • 2)mgd-mongos-02 机器
      • 3)配置分片集群
    • 7.测试分片集群
  • 三、监控
    • 安装
    • 说明
  • 四、性能测试
    • 安装
    • 使用
  • 五、应用实践
    • 1.分片键
    • 2.索引
    • 3.分片集群平衡器
    • 4.节点数据修复
    • 5.节点间数据拷贝
    • 6.内存淘汰策略
    • 7.节点内存释放
    • 8.运维

MongoDB分片集群部署及实践

一、前言

  • 部署环境:CentOS-7-x86_64-DVD-1804.iso
  • 监控环境:windows 10
  • MongoDB版本:5.0.9
  • 官方

MongoDB 下载:https://www.mongodb.com/download-center/community/releases/archive

MongoDB 词汇表:https://www.mongodb.com/docs/manual/reference/glossary

MongoDB 副本集:https://www.mongodb.com/docs/manual/replication/

MongoDB 分片:https://www.mongodb.com/docs/manual/sharding/

MongoDB 存储 - 常见问题:https://www.mongodb.com/docs/manual/faq/storage/

MongoDB Database Tools:https://www.mongodb.com/docs/database-tools/

mongostat:https://www.mongodb.com/docs/database-tools/mongostat/

性能最佳实践:https://www.mongodb.com/blog/post/performance-best-practices-hardware-and-os-configuration

  • 技术分享

千亿级数据迁移mongodb成本节省及性能优化实践:https://zhuanlan.zhihu.com/p/382398295

MongoDB 备份与恢复:https://www.runoob.com/mongodb/mongodb-mongodump-mongorestore.html

CentOS 基础操作命令:https://blog.csdn.net/u011424614/article/details/94555916

对换home分区与root分区的存储空间:https://blog.csdn.net/u011424614/article/details/125853913

CentOS 分区扩容:https://blog.csdn.net/u011424614/article/details/113275862

Dell R740安装CentOS:https://blog.csdn.net/u011424614/article/details/113306808

二、了解分片集群

1.简介

  • 分片是一种跨多台机器分布数据的方法。MongoDB 使用分片来支持具有非常大的数据集和高吞吐量操作的部署
  • 分片优势:
  1. 读和写:提升读和写的性能
  2. 存储容量:实现水平扩容
  3. 高可用:各组件集群部署,实现高可用

2.分片集群组件

  • config servers(配置服务):存储集群的元数据和配置设置。从 MongoDB 3.4 开始,配置服务器必须部署为副本集 (CSRS)
  • shard(分片):存储数据。从 MongoDB 3.6 开始,分片必须部署为副本集
  • mongos(查询路由):为客户端应用程序和分片集群之间提供接口

在这里插入图片描述

3.副本集模式

  • 副本集成员的最小数量是三个成员
  • Primary(主节点):主节点是副本集中唯一接收写入操作的成员,从节点通过 oplog 同步数据
  • Secondary(从节点):同步复制主节点的数据,实现高可用的特性;从节点的所有成员都可以接受读取操作,但是默认情况下,应用程序会将其读取操作定向到主节点
  • Arbiter(仲裁者):仲裁者只在选举中投票;仲裁者不会存储数据,也不能成为主节点

(1)一主两从(PSS)

  • 三个数据存储节点

  • 如果主节点不可用,则副本集选择某个从节点作为主节点,并继续正常操作。旧的主节点在可用时,可重新加入到副本集

在这里插入图片描述

(2)一主一从一仲裁(PSA)

  • 两个数据存储节点
  • 主节点、从节点和仲裁者的部署,可确保在主节点或从节点不可用时,副本集仍然可用
  • 仲裁者消耗非常少的服务器资源

一主一从一仲裁

三.安装部署

1.分片集群环境

  • config server 副本集(3台机器) 、2个 shard(每个分片3台机器=6台机器)、2 个 mongos (2台机器)

  • 资源占用情况

  1. config server 副本集:主要存储路由相关元数据,会消耗少量硬盘、内存和CPU
  2. mongos:主要负责路由转发,只消耗CPU,对内存和硬盘的性能要求不高
  3. shard:主要进行数据存储等一系列数据操作,对CPU、内存和硬盘的性能要求较高
  4. shard 副本集(PSA模式)的 arbiter:只在选举中投票,对CPU、内存和硬盘的性能都要求不高
  • 表格的硬件配置仅供参考,以实际为准
角色机器名IP端口内存 (GB)CPU (核)硬盘
config servermgd-config-01192.168.2.11270188850 G
config servermgd-config-02192.168.2.12270188850 G
config servermgd-config-03192.168.2.13270188850 G
shard【shard1】mgd-shard-01192.168.2.142701964164 T
shard【shard1】mgd-shard-02192.168.2.152701964164 T
shard【shard1】mgd-shard-03192.168.2.162701964164 T
shard【shard2】mgd-shard-04192.168.2.172702064164 T
shard【shard2】mgd-shard-05192.168.2.182702064164 T
shard【shard2】mgd-shard-06192.168.2.192702064164 T
mongosmgd-mongos-01192.168.2.20270178820 G
mongosmgd-mongos-02192.168.2.21270178820 G
  • 参考《性能最佳实践:硬件和操作系统配置》
  1. 为了获取最佳性能,每个机器应该只运行一个 MongoDB 进程

  2. 通过使用虚拟化或容器技术进行适当的大小调整和资源分配,多个 MongoDB 进程可以安全地在单个物理服务器上运行,而无需争抢资源

  3. 用户在同一主机上部署多个 MongoDB 进程。在这种情况下,您必须进行多项配置更改以确保每个进程都有足够的资源

  • 节省成本的方案(减少机器数量)
  1. 使用 6 台高性能机器部署 PSS 模式的 shard ,config server 和 mongos 组件使用虚拟化或容器技术进行部署
  2. 使用 4 台高性能机器部署 PSA 模式的 shard ,shard 的仲裁者、config server 和 mongos 组件使用虚拟化或容器技术进行部署
  3. 全部组件使用虚拟化或容器技术进行部署

2.安装

  • 安装目录:/data/mongodb
  • 分片集群组件的配置和数据目录:/data/mongdb/cluster

1)CPU 检测

  • 检测 CPU 是否支持 AVX 指令集
grep avx /proc/cpuinfo
  • 输出标红的 avx 字符即为支持,否则无法正常启动 MongoDB

2)修改机器名称

如果机器 IP 变化后,更新 hosts 映射文件即可;MongoDB 配置文件使用机器名进行绑定

  • 分别在相应的机器上执行指令
#-- mgd-config-01
hostnamectl set-hostname mgd-config-01 --static
hostnamectl set-hostname mgd-config-01 --transient
#-- mgd-config-02
hostnamectl set-hostname mgd-config-02 --static
hostnamectl set-hostname mgd-config-02 --transient
#-- mgd-config-03
hostnamectl set-hostname mgd-config-03 --static
hostnamectl set-hostname mgd-config-03 --transient
#-- mgd-shard-01
hostnamectl set-hostname mgd-shard-01 --static
hostnamectl set-hostname mgd-shard-01 --transient
#-- mgd-shard-02
hostnamectl set-hostname mgd-shard-02 --static
hostnamectl set-hostname mgd-shard-02 --transient
#-- mgd-shard-03
hostnamectl set-hostname mgd-shard-03 --static
hostnamectl set-hostname mgd-shard-03 --transient
#-- mgd-shard-04
hostnamectl set-hostname mgd-shard-04 --static
hostnamectl set-hostname mgd-shard-04 --transient
#-- mgd-shard-05
hostnamectl set-hostname mgd-shard-05 --static
hostnamectl set-hostname mgd-shard-05 --transient
#-- mgd-shard-06
hostnamectl set-hostname mgd-shard-06 --static
hostnamectl set-hostname mgd-shard-06 --transient
#-- mgd-mongos-01
hostnamectl set-hostname mgd-mongos-01 --static
hostnamectl set-hostname mgd-mongos-01 --transient
#-- mgd-mongos-02
hostnamectl set-hostname mgd-mongos-02 --static
hostnamectl set-hostname mgd-mongos-02 --transient
  • 查询状态
hostnamectl status

3)配置 hosts 文件

  • 在全部机器上执行
cat > /etc/hosts <<EOF
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.2.11 mgd-config-01
192.168.2.12 mgd-config-02
192.168.2.13 mgd-config-03
192.168.2.14 mgd-shard-01
192.168.2.15 mgd-shard-02
192.168.2.16 mgd-shard-03
192.168.2.17 mgd-shard-04
192.168.2.18 mgd-shard-05
192.168.2.19 mgd-shard-06
192.168.2.20 mgd-mongos-01
192.168.2.21 mgd-mongos-02
EOF

4)配置防火墙

  • 当前环境为关闭防火墙的方式;生产环境请使用配置端口的方式,操作参考《CentOS 基础操作命令》
  • 在全部机器上执行
#-- 禁用防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service

#-- 防火墙状态
systemctl status firewalld.service

#-- (备用-可忽略)启用防火墙
systemctl enable firewalld.service
systemctl start firewalld.service

5)创建目录

  • mgd-config-01 至 mgd-config-03 的 3 台机器上执行
mkdir -p /data/mongodb/cluster/config/{data,logs}
  • mgd-shard-01 至 mgd-shard-03 的 3 台机器上执行
mkdir -p /data/mongodb/cluster/shard1/{data,logs}
  • mgd-shard-04 至 mgd-shard-06 的 3 台机器上执行
mkdir -p /data/mongodb/cluster/shard2/{data,logs}
  • mgd-mongos-01 和 mgd-mongos-02 的 2 台机器上执行
mkdir -p /data/mongodb/cluster/mongos/{data,logs}

6)安装 MongoDB

  • 以下指令,在全部机器上执行

  • 请先在官网 下载 MongoDB ,然后,上传到服务器;或者使用 wget 进行下载

#-- 安装依赖包
yum install -y libcurl openssl xz-libs
#-- 解压
tar -zxvf mongodb-linux-x86_64-rhel70-5.0.9.tgz -C /data/mongodb --strip=1
  • 配置环境变量
echo "PATH=$PATH:/data/mongodb/bin" > /etc/profile.d/mongodb.sh
source /etc/profile.d/mongodb.sh
  • 查询版本号
mongo --version
  • 创建用户组和用户
groupadd mongod
useradd -g mongod mongod
  • 文件夹授权
chown -R mongod:mongod /data/mongodb

3.Config Server 配置

1)mgd-config-01 机器

  • 创建配置文件
cat > /data/mongodb/cluster/config/mongod.conf <<EOF
systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb/cluster/config/logs/mongod.log

# Where and how to store data.
storage:
  dbPath: /data/mongodb/cluster/config/data
  journal:
    enabled: true

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /data/mongodb/cluster/config/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27018
  bindIp: mgd-config-01

sharding:
  clusterRole: configsvr

replication:
  replSetName: config
EOF
  • 启动
mongod -f /data/mongodb/cluster/config/mongod.conf

2)mgd-config-02 机器

  • 创建配置文件
cat > /data/mongodb/cluster/config/mongod.conf <<EOF
systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb/cluster/config/logs/mongod.log

# Where and how to store data.
storage:
  dbPath: /data/mongodb/cluster/config/data
  journal:
    enabled: true

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /data/mongodb/cluster/config/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27018
  bindIp: mgd-config-02

sharding:
  clusterRole: configsvr

replication:
  replSetName: config
EOF
  • 启动
mongod -f /data/mongodb/cluster/config/mongod.conf

3)mgd-config-03 机器

  • 创建配置文件
cat > /data/mongodb/cluster/config/mongod.conf <<EOF
systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb/cluster/config/logs/mongod.log

# Where and how to store data.
storage:
  dbPath: /data/mongodb/cluster/config/data
  journal:
    enabled: true

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /data/mongodb/cluster/config/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27018
  bindIp: mgd-config-03

sharding:
  clusterRole: configsvr

replication:
  replSetName: config
EOF
  • 启动
mongod -f /data/mongodb/cluster/config/mongod.conf

4)配置config副本集

  • 连接其中一台 MongoDB
mongo 192.168.2.11 --port 27018
  • 配置副本集
rs.initiate(
  {
    _id: "config",
    configsvr: true,
    members: [
      { _id : 0, host : "mgd-config-01:27018" },
      { _id : 1, host : "mgd-config-02:27018" },
      { _id : 2, host : "mgd-config-03:27018" }
    ]
  }
)
  • 查询副本集状态
rs.status()

4.Shard1 配置

1)mgd-shard-01 机器

  • 创建 shard1 的配置文件,注意:cacheSizeGB,根据实际内存情况修改
cat > /data/mongodb/cluster/shard1/mongod.conf <<EOF
systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb/cluster/shard1/logs/mongod.log

# Where and how to store data.
storage:
  dbPath: /data/mongodb/cluster/shard1/data
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
      cacheSizeGB: 55

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /data/mongodb/cluster/shard1/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27019
  bindIp: mgd-shard-01

sharding:
    clusterRole: shardsvr
    
replication:
    replSetName: shard1
EOF
  • 启动
mongod -f /data/mongodb/cluster/shard1/mongod.conf

2)mgd-shard-02 机器

  • 创建 shard1 的配置文件,注意:cacheSizeGB,根据实际内存情况修改
cat > /data/mongodb/cluster/shard1/mongod.conf <<EOF
systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb/cluster/shard1/logs/mongod.log

# Where and how to store data.
storage:
  dbPath: /data/mongodb/cluster/shard1/data
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
      cacheSizeGB: 55

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /data/mongodb/cluster/shard1/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27019
  bindIp: mgd-shard-02

sharding:
    clusterRole: shardsvr
    
replication:
    replSetName: shard1
EOF
  • 启动
mongod -f /data/mongodb/cluster/shard1/mongod.conf

3)mgd-shard-03 机器

  • 创建 shard1 的配置文件,注意:cacheSizeGB,根据实际内存情况修改
cat > /data/mongodb/cluster/shard1/mongod.conf <<EOF
systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb/cluster/shard1/logs/mongod.log

# Where and how to store data.
storage:
  dbPath: /data/mongodb/cluster/shard1/data
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
      cacheSizeGB: 55

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /data/mongodb/cluster/shard1/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27019
  bindIp: mgd-shard-03

sharding:
    clusterRole: shardsvr
    
replication:
    replSetName: shard1
EOF
  • 启动
mongod -f /data/mongodb/cluster/shard1/mongod.conf

4)配置shard1副本集

  • 连接其中一台 MongoDB
mongo 192.168.2.14 --port 27019
  • 配置 PSS 模式的副本集
rs.initiate(
  {
    _id: "shard1",
    members: [
      { _id : 0, host : "mgd-shard-01:27019" },
      { _id : 1, host : "mgd-shard-02:27019" },
      { _id : 2, host : "mgd-shard-03:27019" }
    ]
  }
)
  • (备用-可忽略)配置 PSA 模式的副本集
rs.initiate(
  {
    _id: "shard1",
    members: [
      { _id : 0, host : "mgd-shard-01:27019" },
      { _id : 1, host : "mgd-shard-02:27019" },
      { _id : 2, host : "mgd-shard-03:27019","arbiterOnly":true }
    ]
  }
)
  • 复制集状态
rs.status()

5.Shard2 配置

1)mgd-shard-04 机器

  • 创建 shard2 的配置文件,注意:cacheSizeGB,根据实际内存情况修改
cat > /data/mongodb/cluster/shard2/mongod.conf <<EOF
systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb/cluster/shard2/logs/mongod.log

# Where and how to store data.
storage:
  dbPath: /data/mongodb/cluster/shard2/data
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
      cacheSizeGB: 55

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /data/mongodb/cluster/shard2/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27020
  bindIp: mgd-shard-04

sharding:
    clusterRole: shardsvr
    
replication:
    replSetName: shard2
EOF
  • 启动
mongod -f /data/mongodb/cluster/shard2/mongod.conf

2)mgd-shard-05 机器

  • 创建 shard2 的配置文件,注意:cacheSizeGB,根据实际内存情况修改
cat > /data/mongodb/cluster/shard2/mongod.conf <<EOF
systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb/cluster/shard2/logs/mongod.log

# Where and how to store data.
storage:
  dbPath: /data/mongodb/cluster/shard2/data
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
      cacheSizeGB: 55

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /data/mongodb/cluster/shard2/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27020
  bindIp: mgd-shard-05

sharding:
    clusterRole: shardsvr
    
replication:
    replSetName: shard2
EOF
  • 启动
mongod -f /data/mongodb/cluster/shard2/mongod.conf

3)mgd-shard-06 机器

  • 创建 shard2 的配置文件,注意:cacheSizeGB,根据实际内存情况修改
cat > /data/mongodb/cluster/shard2/mongod.conf <<EOF
systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb/cluster/shard2/logs/mongod.log

# Where and how to store data.
storage:
  dbPath: /data/mongodb/cluster/shard2/data
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
      cacheSizeGB: 55

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /data/mongodb/cluster/shard2/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27020
  bindIp: mgd-shard-06

sharding:
    clusterRole: shardsvr
    
replication:
    replSetName: shard2
EOF
  • 启动
mongod -f /data/mongodb/cluster/shard2/mongod.conf

4)配置shard2副本集

  • 连接其中一台 MongoDB
mongo 192.168.2.17 --port 27020
  • 配置 PSS 模式的副本集
rs.initiate(
  {
    _id: "shard2",
    members: [
      { _id : 0, host : "mgd-shard-04:27020" },
      { _id : 1, host : "mgd-shard-05:27020" },
      { _id : 2, host : "mgd-shard-06:27020" }
    ]
  }
)
  • (备用-可忽略)配置 PSA 模式的副本集
rs.initiate(
  {
    _id: "shard2",
    members: [
      { _id : 0, host : "mgd-shard-04:27020" },
      { _id : 1, host : "mgd-shard-05:27020" },
      { _id : 2, host : "mgd-shard-06:27020","arbiterOnly":true }
    ]
  }
)
  • 复制集状态
rs.status()

6.Mongos 配置

1)mgd-mongos-01 机器

  • 创建配置文件,注意:configDB,根据实际情况修改
cat > /data/mongodb/cluster/mongos/mongod.conf <<EOF
systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb/cluster/mongos/logs/mongod.log

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /data/mongodb/cluster/mongos/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27017
  bindIp: mgd-mongos-01

sharding:
  configDB: config/mgd-config-01:27018,mgd-config-02:27018,mgd-config-03:27018
EOF
  • 启动
mongos -f /data/mongodb/cluster/mongos/mongod.conf

2)mgd-mongos-02 机器

  • 创建配置文件,注意:configDB,根据实际情况修改
cat > /data/mongodb/cluster/mongos/mongod.conf <<EOF
systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb/cluster/mongos/logs/mongod.log

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /data/mongodb/cluster/mongos/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27017
  bindIp: mgd-mongos-02

sharding:
  configDB: config/mgd-config-01:27018,mgd-config-02:27018,mgd-config-03:27018
EOF
  • 启动
mongos -f /data/mongodb/cluster/mongos/mongod.conf

3)配置分片集群

  • 连接其中一台 Mongos
mongo 192.168.2.20 --port 27017
  • 添加分片到分片集群
sh.addShard( "shard1/mgd-shard-01:27019,mgd-shard-02:27019,mgd-shard-03:27019")
sh.addShard( "shard2/mgd-shard-04:27020,mgd-shard-05:27020,mgd-shard-06:27020")
  • 查询分片集群状态
sh.status()

7.测试分片集群

  • 连接其中一台 mongos
mongo 192.168.2.20 --port 27017
  • 使用 admin 数据库,指定数据库启用分片,集合中设置分片键和分片规则
use admin
sh.enableSharding("testdb")
sh.shardCollection("testdb.order", {"_id": "hashed" })
  • 插入测试数据
use testdb
for (i = 1; i <= 10000; i=i+1){
    db.order.insert({'price': 1})
}
  • 查看集合的数据量和分片情况 (shard1,shard2)
use testdb
db.order.count()
db.order.stats()

三、监控

软件:MongoDB Database Tools

部署环境:Windows 10

安装

  • 修改 hosts 文件(属性中取消勾选只读),文件夹路径:C:\Windows\System32\drivers\etc
192.168.2.11 mgd-config-01
192.168.2.12 mgd-config-02
192.168.2.13 mgd-config-03
192.168.2.14 mgd-shard-01
192.168.2.15 mgd-shard-02
192.168.2.16 mgd-shard-03
192.168.2.17 mgd-shard-04
192.168.2.18 mgd-shard-05
192.168.2.19 mgd-shard-06
192.168.2.20 mgd-mongos-01
192.168.2.21 mgd-mongos-02
  • 下载 MongoDB Database Tools ,并安装
  • cmd 窗口,cd 安装工具的文件夹,执行指令连接 mongos

mongostat:https://www.mongodb.com/docs/database-tools/mongostat/

mongostat -h 192.168.2.21:27017 --discover

说明

参数说明:https://www.mongodb.com/docs/database-tools/mongostat/#fields

参数说明
insert每秒插入数据库的对象数
query每秒查询操作数
update每秒更新操作的数量
delete每秒删除操作的次数
getmore每秒游标批处理操作的数量
command每秒的命令数
flushes每个轮询间隔之间触发的 WiredTiger 检查点的数量
dirtyWiredTiger 缓存中包含脏字节的百分比
used正在使用的 WiredTiger 缓存的百分比
vsize进程在最后一次使用时使用的虚拟内存量(以 MB 为单位)
res进程在最后一次使用时使用的常驻内存量(以 MB 为单位)
locked全局写锁的时间百分比
conn打开的连接总数

四、性能测试

软件:YCSB

部署环境:mgd-mongos-02 机器

使用:https://github.com/brianfrankcooper/YCSB/wiki/Running-a-Workload

参数说明:https://github.com/brianfrankcooper/YCSB/wiki/Core-Properties

安装

  • 下载安装包,创建安装目录,解压
curl -O --location https://github.com/brianfrankcooper/YCSB/releases/download/0.17.0/ycsb-0.17.0.tar.gz

mkdir /opt/ycsb

tar -zxvf ycsb-0.17.0.tar.gz -C /opt/ycsb --strip=1

使用

  • 使用 admin 数据库,指定数据库启用分片,集合中设置分片键和分片规则
use admin
sh.enableSharding("ycsb")
sh.shardCollection("ycsb.usertable", {"_id": "hashed" })
  • 查询状态
use ycsb
db.usertable.stats()
  • 插入数据

recordcount:插入文档数量;threadcount:运行线程数;

fieldcount:字段数量;outputLoad.txt:输出结果(可选)

cd /opt/ycsb

./bin/ycsb load mongodb -s -P workloads/workloada -p  mongodb.url=mongodb://192.168.2.21:27017/ycsb?w=0 -p threadcount=1 -p fieldcount=20 -p recordcount=100000 > outputLoad.txt
  • 测试性能

workloads/workloada:加载场景配置文件

例如:workloada 是 50% 读操作和 50% 更新操作的场景

其它场景文件,可以结合参数说明进行解读

cd /opt/ycsb

./bin/ycsb run mongodb -s -P workloads/workloada -p  mongodb.url=mongodb://192.168.2.21:27017/ycsb?w=0 -p threadcount=1 -p operationcount=1000 > outputRun.txt
  • 输出 outputRun.txt 结果
[OVERALL],RunTime(ms), 10110
[OVERALL],Throughput(ops/sec), 98.91196834817013
[UPDATE], Operations, 491
[UPDATE], AverageLatency(ms), 0.054989816700611
[UPDATE], MinLatency(ms), 0
[UPDATE], MaxLatency(ms), 1
[UPDATE], 95thPercentileLatency(ms), 1
[UPDATE], 99thPercentileLatency(ms), 1
[UPDATE], Return=0, 491
[UPDATE], 0, 464
[UPDATE], 1, 27
[UPDATE], 2, 0
[UPDATE], 3, 0
[UPDATE], 4, 0
...
  • 总执行时间为 10.11 秒
  • 平均吞吐量为 98.9 次操作/秒(跨所有线程)
  • 有 491 次更新操作,以及相关的平均、最小、最大、第 95 和第 99 个百分位延迟
  • 所有 491 更新操作的返回码为零(在这种情况下成功)
  • 464 个操作在不到 1 毫秒的时间内完成,而 27 个操作在 1 到 2 毫秒之间完成。

五、应用实践

1.分片键

分片键:Shard Keys — MongoDB Manual

分片键可实现在集群中的各个分片之间平均分配数据块

  • 哈希分片
# 建库时,指定分片键
use admin
sh.enableSharding("testdb")
sh.shardCollection("testdb.user", {"_id": "hashed" })
  • 范围分片
# 建库时,指定分片键
use admin
sh.enableSharding("testdb")
sh.shardCollection("testdb.user", {"userid":1, "createTime":1})

2.索引

索引:Indexes — MongoDB Manual

切记提前创建索引,因为当单个集合数据量变大时,创建索引会非常耗时

  • 单字段索引
# userid 升序
db.user.createIndex({"userid":1})
  • 复合索引
# name 升序,age 降序
db.user.createIndex({ "name": 1, "age": -1 })
# 注意:
# 过滤条件为 name,或包含 name 的查询会使用索引(索引的第一个字段),以下都会用到索引
db.user.find({ name: '张三' }).explain()
db.user.find({ name: "张三", level: 10 }).explain()
db.user.find({ name: "张三", age: 23 }).explain()
 
# 查询条件为 age 时,不会使用上边创建的索引,而是使用的全表扫描
db.user.find({ age: 23 }).explain()
  • 地理索引
db.user.createIndex({"location":"2dsphere"})
  • 完整例子
# 切换到 admin 库
use admin
sh.status()
# 建库
sh.enableSharding("testdb")
# 创建集合和分片键
sh.shardCollection("testdb.user", {"userid":1, "createTime":1})

# 切换到 testdb 库
use testdb
# 创建集合索引
db.user.createIndex({"userid":1, "createTime":1})
db.user.createIndex({"userid":1})
db.user.createIndex({"name":1})
db.user.createIndex({"age":1})
db.user.createIndex({"location":"2dsphere"})

3.分片集群平衡器

分片集群平衡器:Sharded Cluster Balancer — MongoDB Manual

MongoDB 平衡器是一个后台进程,用于监控 每个分片集合的数据。

  • 基础指令
指令作用
sh.getBalancerState()检查平衡器是否启用
sh.isBalancerRunning()检查平衡器是否在运行
sh.stopBalancer()禁用平衡器
sh.startBalancer() 、sh.setBalancerState(true)启用平衡器
sh.disableBalancing(“databaseName.collectionName”)指定集合禁用平衡器
sh.enableBalancing(“databaseName.collectionName”)指定集合启用平衡器
  • 启用平衡器
# 连接其中一台 mongos
mongo 192.168.2.20 --port 27017

# 切换到 config 库
use config

# 启用平衡器
sh.setBalancerState(true)
# 设置启动时间段
db.settings.update(
   { _id: "balancer" },
   { $set: { activeWindow : { start : "00:00", stop : "06:00" } } },
   { upsert: true }
)
  • 禁用平衡器
# 连接其中一台 mongos
mongo 192.168.2.20 --port 27017

# 切换到 config 库
use config

# 禁用平衡器
sh.stopBalancer()

4.节点数据修复

  • Mongo shard 服务启动失败或报错时,可以先修复数据,在重启服务

报错:Aborted (core dumped)

# 先删除 datapath 下的全部 .lock 文件,在执行修复指令
mongod --repair --dbpath /data/mongodb/cluster/shard1/data

5.节点间数据拷贝

  • 如果数据修复失败,用于拷贝其它节点的数据(需要停止被拷贝节点的服务)
scp -r /data/mongodb/cluster/shard1/data  root@mgd-shard-03:/data/mongodb/cluster/shard1

6.内存淘汰策略

  • 默认参数和值
参数默认值含义
eviction_target80当 cache used 超过 eviction_target,后台evict线程开始淘汰 CLEAN PAGE
eviction_trigger95当 cache used 超过 eviction_trigger,用户线程也开始淘汰 CLEAN PAGE
eviction_dirty_target5当 cache dirty 超过 eviction_dirty_target,后台evict线程开始淘汰 DIRTY PAGE
eviction_dirty_trigger20当 cache dirty 超过 eviction_dirty_trigger, 用户线程也开始淘汰 DIRTY PAGE
evict.threads_min4
evict.threads_max4
  • 优化
参数默认值含义
eviction_target75当 cache used 超过 eviction_target,后台evict线程开始淘汰 CLEAN PAGE
eviction_trigger90当 cache used 超过 eviction_trigger,用户线程也开始淘汰 CLEAN PAGE
eviction_dirty_target3当 cache dirty 超过 eviction_dirty_target,后台evict线程开始淘汰 DIRTY PAGE
eviction_dirty_trigger15当 cache dirty 超过 eviction_dirty_trigger, 用户线程也开始淘汰 DIRTY PAGE
evict.threads_min4
evict.threads_max16
  • 连接 shard 的 mongo 服务
# 如果机器名无法连接,需要把机器名换成 IP
mongo 192.168.2.14 --port 27019
mongo 192.168.2.15 --port 27019
mongo 192.168.2.16 --port 27019
mongo 192.168.2.17 --port 27020
mongo 192.168.2.18 --port 27020
mongo 192.168.2.19 --port 27020
  • 执行
db.adminCommand({setParameter: 1, wiredTigerEngineRuntimeConfig: "eviction_target=75,eviction_trigger=90,eviction_dirty_target=3,eviction_dirty_trigger=15,eviction=(threads_min=4,threads_max=16),checkpoint=(wait=30)"})
  • 动态调整其它参数
db.adminCommand({"setParameter": 1, "wiredTigerEngineRuntimeConfig": "cache_size=8G"})

7.节点内存释放

  • 查看 Mongo 服务是否被 CentOS 杀掉进程
cd /var/log
grep "mongod" messages
Jul 28 01:40:20 mgd-shard-03 abrt-hook-ccpp: Process 3368 (mongod) of user 0 killed by SIGABRT - ignoring (abrtd is not running)

Aug 30 17:10:48 mgd-rc-04 abrt-hook-ccpp: Process 23380 (mongod) of user 0 killed by SIGABRT - dumping core
Aug 30 17:11:56 mgd-rc-04 abrt-server: Executable '/data/mongodb/bin/mongod' doesn't belong to any package and ProcessUnpackaged is set to 'no'
  • 暂时未找到比较好的内存释放方法
sync && sync && sync
echo 3 > /proc/sys/vm/drop_caches
swapoff -a
swapon -a

8.运维

  • 角色启动顺序:config --> shard --> mongos

  • 查看进程信息

ps -ef | grep mongod | grep -v grep
  • 内存占用
top

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/341938.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

PowerBI的使用和基本效果

1. 什么是PowerBI Power BI 是一套商业分析工具&#xff0c;用于在组织中提供见解。可连接数百个数据源、简化数据准备并提供即席分析。生成美观的报表并进行发布&#xff0c;供组织在 Web 和移动设备上使用。每个人都可创建个性化仪表板&#xff0c;获取针对其业务的全方位独…

加一-力扣66-java高效方案

一、题目描述给定一个由 整数 组成的 非空 数组所表示的非负整数&#xff0c;在该数的基础上加一。最高位数字存放在数组的首位&#xff0c; 数组中每个元素只存储单个数字。你可以假设除了整数 0 之外&#xff0c;这个整数不会以零开头。示例 1&#xff1a;输入&#xff1a;di…

PostgreSQL的下载安装教程(macOS、Windows)

postgresql是GIS服务端几乎不可避免要打交道的数据库。因为mysql的空间扩展真是不尽人意。所以想要学会GIS服务端知识,postgresql(下文简称pg)你是必须要会的。 首先要知道,pg是一个空间数据库,和普通数据库不同的是pg支持空间数据的存储与操作。这里所谓的空间数据一般指…

1、为什么要经常对数据做特征归一化

前言&#xff1a; feature scaling 即为特征归一化、标准化&#xff0c;是数据预处理中的重要技术。在实际运用中&#xff0c;使用梯度下降算法的模型都需要归一化&#xff0c;包括线性回归、逻辑回归、支持向量机、神经网络等模型。但对决策树模型并不适用&#xff0c;因为信…

【LeetCode】每日一题(3)

目录 题目&#xff1a;1234. 替换子串得到平衡字符串 - 力扣&#xff08;Leetcode&#xff09; 题目的接口&#xff1a; 解题思路&#xff1a; 代码&#xff1a; 过啦&#xff01;&#xff01;&#xff01; 写在最后&#xff1a; 题目&#xff1a;1234. 替换子串得到平衡…

handler解析(3)-同步消息、异步消息、同步屏障

Message分为3种&#xff1a;普通消息&#xff08;同步消息&#xff09;、屏障消息&#xff08;同步屏障&#xff09;和异步消息。我们通常使用的都是普通消息&#xff0c;而屏障消息就是在消息队列中插入一个屏障&#xff0c;在屏障之后的所有普通消息都会被挡着&#xff0c;不…

使用继承与派生的6大要点

概述 面向对象编程技术非常看重软件的可重用性&#xff0c;在C中&#xff0c;可重用性是通过继承机制来实现的。继承机制允许程序员在保持原有类的数据和功能的基础上进行扩展&#xff0c;增加新的数据和功能&#xff0c;从而构成一个新的类&#xff0c;也称为派生类。原有类&a…

分销系统逻辑

相关概念 主营商户: 提供分销商品和佣金的商户分销商: 拥有自己的销售渠道&#xff0c;能够帮助推动产品销售的个人或商户消费者: 购买分销商品的人。佣金: 主营商户返还给经销商的比例抽成 分销功能设计 (1)分销商准入规则设计 无规则: 没有分销商的准入门槛限制&#xf…

七大设计原则之依赖倒置原则应用

目录1 依赖倒置原则2 依赖倒置应用1 依赖倒置原则 依赖倒置原则&#xff08;Dependence Inversion Principle,DIP&#xff09;是指设计代码结构时&#xff0c;高层模块不应该依赖底层模块&#xff0c;二者都应该依赖其抽象。抽象不应该依赖细节&#xff1b;细节应该依赖抽象。…

VHDL语言基础-Testbech

目录 VHDL仿真概述: 基本结构: VHDL一般仿真过程: 仿真测试平台文件: 编写测试平台文件的语言: 一个测试平台文件的基本结构如下&#xff1a; 测试平台文件包含的基本语句: 产生激励信号的方式: 时钟信号: 复位信号: 周期信性信号: 使用延迟DELAYD: 一般的激励信号…

算法leetcode|36. 有效的数独(rust重拳出击)

文章目录36. 有效的数独&#xff1a;样例 1&#xff1a;样例 2&#xff1a;提示&#xff1a;分析&#xff1a;题解&#xff1a;rustgoccpythonjava36. 有效的数独&#xff1a; 请你判断一个 9 x 9 的数独是否有效。只需要 根据以下规则 &#xff0c;验证已经填入的数字是否有效…

【C++】类和对象---需掌握的功能

目录1.初始化列表1.1构造函数赋值1.2初始化列表格式&#xff1a;编译器执行的顺序&#xff1a;特性&#xff1a;1.3explicit关键字类型替换过程多参数构造函数类型替换&#xff08;C11&#xff09;2.static成员编程题3.匿名对象4.友元4.1友元函数4.2友元类5.内部类6.拷贝对象时…

linux高级命令之获取管理员权限的相关命令

获取管理员权限的相关命令学习目标能够知道切换到管理员用户的命令1. sudo命令的使用命令说明sudo -s切换到root用户&#xff0c;获取管理员权限sudo某个命令的执行需要获取管理员权限可以在执行命令前面加上sudosudo -s效果图:sudo 命令效果图:说明:如果只是某次操作需要使用管…

面向对象与面向过程编程

从语言角度来讲&#xff1a; C是面向过程编程&#xff1b; C一半是面向过程编程&#xff0c;一半是面向对象编程&#xff1b; Java是面向对象编程。 一、什么是面向对象编程与面向过程编程&#xff1f; 面向过程&#xff08;Procedure Oriented 简称 PO&#xff09;&#xff1…

云计算ACP云服务器ECS实例题库

&#x1f618;作者简介&#xff1a;一名99年软件运维应届毕业生&#xff0c;正在自学云计算课程。&#x1f44a;宣言&#xff1a;人生就是B&#xff08;birth&#xff09;和D&#xff08;death&#xff09;之间的C&#xff08;choise&#xff09;&#xff0c;做好每一个选择。&…

2.你的程序乱码了吗?

学习的动力不止于此&#xff1a; 1. 乱码 #include <QApplication> #include <QLabel> #include <QFont> int main(int argc, char *argv[]) {QApplication a(argc, argv);QLabel lb;lb.setFont(QFont("Sans Serif", 24));lb.setText(" 乱…

Linux文件隐藏属性(修改与显示):chattr和lsattr

文件除了基本的九个权限以外还有隐藏属性存在&#xff0c;这些隐藏属性对于系统有很大的帮助&#xff0c;尤其是系统安全&#xff08;Security&#xff09;上 chattr&#xff08;配置文件隐藏属性&#xff09; chattr 【-】【ASacdistu】文件或目录名称 选项与参数&#xff1a…

Visual Navigation(一):阅读三篇经典论文

文章目录前言一、Learning to Navigate in Cities Without a Map二、Unsupervised Predictive Memory in a Goal-Directed Agent三、Zero-Shot Imitation LearningImitation Learning:前言 研究生不读论文还是不行的呀&#xff0c;在这里结合下别人的总结等下一次组会吹水。 …

大客户市场:阿里云、腾讯云、华为云“贴身肉搏”

配图来自Canva可画 近年来&#xff0c;随着中国逐渐进入数字化经济快车道&#xff0c;国内企业数字化、智能化升级已是刻不容缓。而为了帮助自身或其他企业实现数字化转型升级&#xff0c;阿里、腾讯、百度、京东、字节、网易、华为等众多国内知名企业早在多年以前&#xff0c…

【Git学习】Git如何Clone带有Submodule的仓库?

文章目录一、问题描述二、解决问题三、参考链接四、解决问题4.1 下载主模块4.2 查看主模块的配置4.2 子模块的添加4.3 查看子模块的配置4.4 查看子模块的检出状态4.5 检出submodule4.6 再次查看.git/config4.7 重新打开Android Studio运行代码一、问题描述 在GitHub上下载了一…