【MongoDB分片集群部署及实践】

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

   
   
  • 1
  • 输出标红的 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

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 查询状态
hostnamectl status

   
   
  • 1
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

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
4)配置防火墙
  • 当前环境为关闭防火墙的方式;生产环境请使用配置端口的方式,操作参考《CentOS 基础操作命令
  • 在全部机器上执行
#-- 禁用防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service

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

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
5)创建目录
  • mgd-config-01 至 mgd-config-03 的 3 台机器上执行
mkdir -p /data/mongodb/cluster/config/{data,logs}

 
 
  • 1
  • mgd-shard-01 至 mgd-shard-03 的 3 台机器上执行
mkdir -p /data/mongodb/cluster/shard1/{data,logs}

 
 
  • 1
  • mgd-shard-04 至 mgd-shard-06 的 3 台机器上执行
mkdir -p /data/mongodb/cluster/shard2/{data,logs}

 
 
  • 1
  • mgd-mongos-01 和 mgd-mongos-02 的 2 台机器上执行
mkdir -p /data/mongodb/cluster/mongos/{data,logs}

 
 
  • 1
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

 
 
  • 1
  • 2
  • 3
  • 4
  • 配置环境变量
echo "PATH=$PATH:/data/mongodb/bin" > /etc/profile.d/mongodb.sh
source /etc/profile.d/mongodb.sh

 
 
  • 1
  • 2
  • 查询版本号
mongo --version

 
 
  • 1
  • 创建用户组和用户
groupadd mongod
useradd -g mongod mongod

 
 
  • 1
  • 2
  • 文件夹授权
chown -R mongod:mongod /data/mongodb

 
 
  • 1

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 启动
mongod -f /data/mongodb/cluster/config/mongod.conf

 
 
  • 1
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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 启动
mongod -f /data/mongodb/cluster/config/mongod.conf

 
 
  • 1
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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 启动
mongod -f /data/mongodb/cluster/config/mongod.conf

 
 
  • 1
4)配置config副本集
  • 连接其中一台 MongoDB
mongo 192.168.2.11 --port 27018

 
 
  • 1
  • 配置副本集
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" }
    ]
  }
)

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 查询副本集状态
rs.status()

 
 
  • 1

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 启动
mongod -f /data/mongodb/cluster/shard1/mongod.conf

 
 
  • 1
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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 启动
mongod -f /data/mongodb/cluster/shard1/mongod.conf

 
 
  • 1
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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 启动
mongod -f /data/mongodb/cluster/shard1/mongod.conf

 
 
  • 1
4)配置shard1副本集
  • 连接其中一台 MongoDB
mongo 192.168.2.14 --port 27019

 
 
  • 1
  • 配置 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" }
    ]
  }
)

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • (备用-可忽略)配置 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 }
    ]
  }
)

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 复制集状态
rs.status()

 
 
  • 1

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 启动
mongod -f /data/mongodb/cluster/shard2/mongod.conf

 
 
  • 1
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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 启动
mongod -f /data/mongodb/cluster/shard2/mongod.conf

 
 
  • 1
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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 启动
mongod -f /data/mongodb/cluster/shard2/mongod.conf

 
 
  • 1
4)配置shard2副本集
  • 连接其中一台 MongoDB
mongo 192.168.2.17 --port 27020

 
 
  • 1
  • 配置 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" }
    ]
  }
)

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • (备用-可忽略)配置 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 }
    ]
  }
)

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 复制集状态
rs.status()

 
 
  • 1

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 启动
mongos -f /data/mongodb/cluster/mongos/mongod.conf

 
 
  • 1
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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 启动
mongos -f /data/mongodb/cluster/mongos/mongod.conf

 
 
  • 1
3)配置分片集群
  • 连接其中一台 Mongos
mongo 192.168.2.20 --port 27017

 
 
  • 1
  • 添加分片到分片集群
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")

 
 
  • 1
  • 2
  • 查询分片集群状态
sh.status()

 
 
  • 1

7.测试分片集群

  • 连接其中一台 mongos
mongo 192.168.2.20 --port 27017

 
 
  • 1
  • 使用 admin 数据库,指定数据库启用分片,集合中设置分片键和分片规则
use admin
sh.enableSharding("testdb")
sh.shardCollection("testdb.order", {"_id": "hashed" })

 
 
  • 1
  • 2
  • 3
  • 插入测试数据
use testdb
for (i = 1; i <= 10000; i=i+1){
    db.order.insert({'price': 1})
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 查看集合的数据量和分片情况 (shard1,shard2)
use testdb
db.order.count()
db.order.stats()

 
 
  • 1
  • 2
  • 3

三、监控

软件: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

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 下载 MongoDB Database Tools ,并安装
  • cmd 窗口,cd 安装工具的文件夹,执行指令连接 mongos

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

mongostat -h 192.168.2.21:27017 --discover

 
 
  • 1

说明

参数说明: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

  • 1
  • 2
  • 3
  • 4
  • 5

使用

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

 
 
  • 1
  • 2
  • 3
  • 查询状态
use ycsb
db.usertable.stats()

 
 
  • 1
  • 2
  • 插入数据

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

  • 1
  • 2
  • 3
  • 测试性能

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

  • 1
  • 2
  • 3
  • 输出 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
...

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 总执行时间为 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" })

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

 
 
  • 1
  • 2
  • 3
  • 4

2.索引

索引:Indexes — MongoDB Manual

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

  • 单字段索引
# userid 升序
db.user.createIndex({"userid":1})

 
 
  • 1
  • 2
  • 复合索引
# name 升序,age 降序
db.user.createIndex({ "name": 1, "age": -1 })

 
 
  • 1
  • 2
# 注意:
# 过滤条件为 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()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 地理索引
db.user.createIndex({"location":"2dsphere"})

 
 
  • 1
  • 完整例子
# 切换到 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”})

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

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 }
)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 禁用平衡器
# 连接其中一台 mongos
mongo 192.168.2.20 --port 27017

# 切换到 config 库
use config

# 禁用平衡器
sh.stopBalancer()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

4.节点数据修复

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

报错:Aborted (core dumped)

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

 
 
  • 1
  • 2

5.节点间数据拷贝

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

 
 
  • 1

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

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 执行
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)"})

 
 
  • 1
  • 动态调整其它参数
db.adminCommand({"setParameter": 1, "wiredTigerEngineRuntimeConfig": "cache_size=8G"})

 
 
  • 1

7.节点内存释放

  • 查看 Mongo 服务是否被 CentOS 杀掉进程
cd /var/log
grep "mongod" messages

 
 
  • 1
  • 2
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’

  • 1
  • 2
  • 3
  • 4
  • 暂时未找到比较好的内存释放方法
sync && sync && sync
echo 3 > /proc/sys/vm/drop_caches
swapoff -a
swapon -a

 
 
  • 1
  • 2
  • 3
  • 4

8.运维

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

  • 查看进程信息

ps -ef | grep mongod | grep -v grep

 
 
  • 1
  • 内存占用
top

 
 
  • 1
            </div><div data-report-view="{&quot;mod&quot;:&quot;1585297308_001&quot;,&quot;spm&quot;:&quot;1001.2101.3001.6548&quot;,&quot;dest&quot;:&quot;https://blog.csdn.net/u011424614/article/details/129001334&quot;,&quot;extend1&quot;:&quot;pc&quot;,&quot;ab&quot;:&quot;new&quot;}"><div></div></div>
            <link href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/markdown_views-98b95bb57c.css" rel="stylesheet">
            <link href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/style-c216769e99.css" rel="stylesheet">
    </div><div id="article_content" class="article_content clearfix">
    <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
    <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-dc4a025e85.css">
            <div id="content_views" class="markdown_views prism-atom-one-light">
                <svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
                    <path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
                </svg>
                <p></p> 

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

 
 
  • 1
  • 输出标红的 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

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 查询状态
hostnamectl status

 
 
  • 1
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

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
4)配置防火墙
  • 当前环境为关闭防火墙的方式;生产环境请使用配置端口的方式,操作参考《CentOS 基础操作命令
  • 在全部机器上执行
#-- 禁用防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service

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

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
5)创建目录
  • mgd-config-01 至 mgd-config-03 的 3 台机器上执行
mkdir -p /data/mongodb/cluster/config/{data,logs}

 
 
  • 1
  • mgd-shard-01 至 mgd-shard-03 的 3 台机器上执行
mkdir -p /data/mongodb/cluster/shard1/{data,logs}

 
 
  • 1
  • mgd-shard-04 至 mgd-shard-06 的 3 台机器上执行
mkdir -p /data/mongodb/cluster/shard2/{data,logs}

 
 
  • 1
  • mgd-mongos-01 和 mgd-mongos-02 的 2 台机器上执行
mkdir -p /data/mongodb/cluster/mongos/{data,logs}

 
 
  • 1
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

 
 
  • 1
  • 2
  • 3
  • 4
  • 配置环境变量
echo "PATH=$PATH:/data/mongodb/bin" > /etc/profile.d/mongodb.sh
source /etc/profile.d/mongodb.sh

 
 
  • 1
  • 2
  • 查询版本号
mongo --version

 
 
  • 1
  • 创建用户组和用户
groupadd mongod
useradd -g mongod mongod

 
 
  • 1
  • 2
  • 文件夹授权
chown -R mongod:mongod /data/mongodb

 
 
  • 1

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 启动
mongod -f /data/mongodb/cluster/config/mongod.conf

 
 
  • 1
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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 启动
mongod -f /data/mongodb/cluster/config/mongod.conf

 
 
  • 1
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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 启动
mongod -f /data/mongodb/cluster/config/mongod.conf

 
 
  • 1
4)配置config副本集
  • 连接其中一台 MongoDB
mongo 192.168.2.11 --port 27018

 
 
  • 1
  • 配置副本集
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" }
    ]
  }
)

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 查询副本集状态
rs.status()

 
 
  • 1

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 启动
mongod -f /data/mongodb/cluster/shard1/mongod.conf

 
 
  • 1
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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 启动
mongod -f /data/mongodb/cluster/shard1/mongod.conf

 
 
  • 1
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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 启动
mongod -f /data/mongodb/cluster/shard1/mongod.conf

 
 
  • 1
4)配置shard1副本集
  • 连接其中一台 MongoDB
mongo 192.168.2.14 --port 27019

 
 
  • 1
  • 配置 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" }
    ]
  }
)

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • (备用-可忽略)配置 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 }
    ]
  }
)

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 复制集状态
rs.status()

 
 
  • 1

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 启动
mongod -f /data/mongodb/cluster/shard2/mongod.conf

 
 
  • 1
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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 启动
mongod -f /data/mongodb/cluster/shard2/mongod.conf

 
 
  • 1
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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 启动
mongod -f /data/mongodb/cluster/shard2/mongod.conf

 
 
  • 1
4)配置shard2副本集
  • 连接其中一台 MongoDB
mongo 192.168.2.17 --port 27020

 
 
  • 1
  • 配置 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" }
    ]
  }
)

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • (备用-可忽略)配置 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 }
    ]
  }
)

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 复制集状态
rs.status()

 
 
  • 1

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 启动
mongos -f /data/mongodb/cluster/mongos/mongod.conf

 
 
  • 1
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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 启动
mongos -f /data/mongodb/cluster/mongos/mongod.conf

 
 
  • 1
3)配置分片集群
  • 连接其中一台 Mongos
mongo 192.168.2.20 --port 27017

 
 
  • 1
  • 添加分片到分片集群
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")

 
 
  • 1
  • 2
  • 查询分片集群状态
sh.status()

 
 
  • 1

7.测试分片集群

  • 连接其中一台 mongos
mongo 192.168.2.20 --port 27017

 
 
  • 1
  • 使用 admin 数据库,指定数据库启用分片,集合中设置分片键和分片规则
use admin
sh.enableSharding("testdb")
sh.shardCollection("testdb.order", {"_id": "hashed" })

 
 
  • 1
  • 2
  • 3
  • 插入测试数据
use testdb
for (i = 1; i <= 10000; i=i+1){
    db.order.insert({'price': 1})
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 查看集合的数据量和分片情况 (shard1,shard2)
use testdb
db.order.count()
db.order.stats()

 
 
  • 1
  • 2
  • 3

三、监控

软件: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

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 下载 MongoDB Database Tools ,并安装
  • cmd 窗口,cd 安装工具的文件夹,执行指令连接 mongos

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

mongostat -h 192.168.2.21:27017 --discover

 
 
  • 1

说明

参数说明: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

  • 1
  • 2
  • 3
  • 4
  • 5

使用

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

 
 
  • 1
  • 2
  • 3
  • 查询状态
use ycsb
db.usertable.stats()

 
 
  • 1
  • 2
  • 插入数据

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

  • 1
  • 2
  • 3
  • 测试性能

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

  • 1
  • 2
  • 3
  • 输出 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
...

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 总执行时间为 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" })

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

 
 
  • 1
  • 2
  • 3
  • 4

2.索引

索引:Indexes — MongoDB Manual

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

  • 单字段索引
# userid 升序
db.user.createIndex({"userid":1})

 
 
  • 1
  • 2
  • 复合索引
# name 升序,age 降序
db.user.createIndex({ "name": 1, "age": -1 })

 
 
  • 1
  • 2
# 注意:
# 过滤条件为 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()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 地理索引
db.user.createIndex({"location":"2dsphere"})

 
 
  • 1
  • 完整例子
# 切换到 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”})

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

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 }
)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 禁用平衡器
# 连接其中一台 mongos
mongo 192.168.2.20 --port 27017

# 切换到 config 库
use config

# 禁用平衡器
sh.stopBalancer()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

4.节点数据修复

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

报错:Aborted (core dumped)

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

 
 
  • 1
  • 2

5.节点间数据拷贝

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

 
 
  • 1

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

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 执行
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)"})

 
 
  • 1
  • 动态调整其它参数
db.adminCommand({"setParameter": 1, "wiredTigerEngineRuntimeConfig": "cache_size=8G"})

 
 
  • 1

7.节点内存释放

  • 查看 Mongo 服务是否被 CentOS 杀掉进程
cd /var/log
grep "mongod" messages

 
 
  • 1
  • 2
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’

  • 1
  • 2
  • 3
  • 4
  • 暂时未找到比较好的内存释放方法
sync && sync && sync
echo 3 > /proc/sys/vm/drop_caches
swapoff -a
swapon -a

 
 
  • 1
  • 2
  • 3
  • 4

8.运维

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

  • 查看进程信息

ps -ef | grep mongod | grep -v grep

 
 
  • 1
  • 内存占用
top

 
 
  • 1
            </div><div data-report-view="{&quot;mod&quot;:&quot;1585297308_001&quot;,&quot;spm&quot;:&quot;1001.2101.3001.6548&quot;,&quot;dest&quot;:&quot;https://blog.csdn.net/u011424614/article/details/129001334&quot;,&quot;extend1&quot;:&quot;pc&quot;,&quot;ab&quot;:&quot;new&quot;}"><div></div></div>
            <link href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/markdown_views-98b95bb57c.css" rel="stylesheet">
            <link href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/style-c216769e99.css" rel="stylesheet">
    </div>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
对于MongoDB分片集群部署,您可以按照以下步骤进行操作: 1. 部署配置服务器(config server):配置服务器用于存储分片集群的元数据,可以独立部署或与其他组件共享服务器。您可以选择部署多个配置服务器以增加可用性。 2. 启动分片服务器(shard server):分片服务器用于存储数据,您可以选择在不同的物理机器或虚拟机上启动多个分片服务器。每个分片服务器都可以容纳一部分数据。 3. 启动路由器(router):路由器也称为mongos进程,它是应用程序和分片集群之间的中间件。它将客户端请求路由到正确的分片服务器,并协调数据的读写操作。 4. 添加分片:在启动了路由器和分片服务器之后,您需要将分片服务器添加到集群中。使用mongos进程连接到配置服务器,并使用`sh.addShard()`命令添加每个分片服务器。 5. 配置分片键(shard key):分片键用于将数据划分为不同的分片。选择一个适合您数据模式和查询模式的字段作为分片键,并使用`sh.shardCollection()`命令启用分片。 6. 验证部署:您可以使用`sh.status()`命令来验证集群的状态,并确保所有组件都正常工作。 这只是一个简单的概述,实际部署过程可能会更加复杂,并且取决于您的环境和需求。建议您参考MongoDB官方文档中关于分片集群部署的详细指南,以获得更全面的了解和操作指导。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值