centos7.9 部署mongodb-4.4.18 分片副本集群

lixinliang / 2023-08-05 / 原文

准备基本环境

服务器node1(10.0.3.1) 服务器node2(10.0.3.2) 服务器node3(10.0.3.3)
mongos 20000 mongos 20000 mongos 20000
config server 21000 config server 21000 config server 21000
shard server1 主节点27001 shard server1 副节点 27001 shard server1 仲裁27001
shard server2 仲裁27002 shard server2 主节点27002 shard server2 副节点27002
shard server3 副节点27003 shard server3 仲裁27003 shard server3 主节点27003

搭建步骤

三台设备均操作

  • 3台设备修改 hosts 文件
[mongod@mongodb-node-1 ~]$ cat /etc/hosts
10.0.3.1	mongodb-node-1
10.0.3.2	mongodb-node-2
10.0.3.3	mongodb-node-3
  • 3台设备创建数据目录和日志目录
[mongod@mongodb-node-1 ~]$ mkdir -p /export/mongodb-4.4.18/cluster/{config,mongos,shard1,shard2,shard3}/{data,logs}
  • 3台设备安装mongodb tar包所需依赖
[mongod@mongodb-node-1 ~]$ yum install -y libcurl openssl xz-libs
  • 3台设备下载mongodb二进制包并解压
[root@mongodb-node-1 ~]#  wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.18.tgz 
[root@mongodb-node-1 ~]# tar -xf mongodb-linux-x86_64-rhel70-4.4.18.tgz -C /export/
[root@mongodb-node-1 ~]# cd /export/
[root@mongodb-node-1 export]# mv mongodb-linux-x86_64-rhel70-4.4.18 mongodb-4.4.18
  • 3台设备配置环境变量
[root@mongodb-node-1 export]# cat /etc/profile.d/mongodb.sh 
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/export/mongodb-4.4.18/bin

source /etc/profile.d/mongodb.sh
mongo --version
  • 3台设备创建mongodb启动用户
[root@mongodb-node-1 ~]# useradd  -d /export/mongod mongod
[root@mongodb-node-1 ~]# chown mongod:mongod /export/mongodb-4.4.18/ -R

第一台设备 10.0.3.1 操作

# 准备configServer配置文件
[mongod@mongodb-node-1 config]$ cd /export/mongodb-4.4.18/cluster/config
[mongod@mongodb-node-1 config]$ cat config.conf 
systemLog:
  destination: file
  logAppend: true
  path: /export/mongodb-4.4.18/cluster/config/logs/config.log

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

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

# network interfaces
net:
  port: 21000
  bindIp: 0.0.0.0

sharding:
  clusterRole: configsvr

replication:
  replSetName: config


# 准备 shard1分片配置文件
[mongod@mongodb-node-1 shard1]$ cd /export/mongodb-4.4.18/cluster/shard1
[mongod@mongodb-node-1 shard1]$ cat shard1.conf 
systemLog:
  destination: file
  logAppend: true
  path: /export/mongodb-4.4.18/cluster/shard1/logs/shard1.log

# Where and how to store data.
storage:
  dbPath: /export/mongodb-4.4.18/cluster/shard1/data
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
      cacheSizeGB: 2
# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /export/mongodb-4.4.18/cluster/shard1/shard1.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27001
  bindIp: 0.0.0.0

sharding:
    clusterRole: shardsvr
    
replication:
    replSetName: shard1


# 准备 shard2分片配置文件
[mongod@mongodb-node-1 shard2]$ cd /export/mongodb-4.4.18/cluster/shard2
[mongod@mongodb-node-1 shard2]$ cat shard2.conf 
systemLog:
  destination: file
  logAppend: true
  path: /export/mongodb-4.4.18/cluster/shard2/logs/shard2.log

# Where and how to store data.
storage:
  dbPath: /export/mongodb-4.4.18/cluster/shard2/data
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
      cacheSizeGB: 2
# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /export/mongodb-4.4.18/cluster/shard2/shard2.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27002
  bindIp: 0.0.0.0

sharding:
    clusterRole: shardsvr
    
replication:
    replSetName: shard2


# 准备shard3分片配置文件
[mongod@mongodb-node-1 shard3]$ cd /export/mongodb-4.4.18/cluster/shard3
[mongod@mongodb-node-1 shard3]$ cat shard3.conf 
systemLog:
  destination: file
  logAppend: true
  path: /export/mongodb-4.4.18/cluster/shard3/logs/shard3.log

# Where and how to store data.
storage:
  dbPath: /export/mongodb-4.4.18/cluster/shard3/data
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
      cacheSizeGB: 2
# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /export/mongodb-4.4.18/cluster/shard3/shard3.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27003
  bindIp: 0.0.0.0

sharding:
    clusterRole: shardsvr
    
replication:
    replSetName: shard3

# 准备 mongos-router配置文件,注意mongos没有storage部分配置
[mongod@mongodb-node-1 mongos]$ cd /export/mongodb-4.4.18/cluster/mongos
[mongod@mongodb-node-1 mongos]$ cat mongos.conf 
systemLog:
  destination: file
  logAppend: true
  path: /export/mongodb-4.4.18/cluster/mongos/logs/mongos.log

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

# network interfaces
net:
  port: 20000
  bindIp: 0.0.0.0

sharding:
  configDB: config/10.0.3.1:21000,10.0.3.2:21000,10.0.3.3:21000

  • 第二台设备 10.0.3.2 操作
# 准备configServer配置文件
[mongod@mongodb-node-2 config]$ cd /export/mongodb-4.4.18/cluster/config
[mongod@mongodb-node-2 config]$ cat config.conf 
systemLog:
  destination: file
  logAppend: true
  path: /export/mongodb-4.4.18/cluster/config/logs/config.log

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

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

# network interfaces
net:
  port: 21000
  bindIp: 0.0.0.0

sharding:
  clusterRole: configsvr

replication:
  replSetName: config

# 准备 shard1分片配置文件
[mongod@mongodb-node-2 shard1]$ cd /export/mongodb-4.4.18/cluster/shard1
[mongod@mongodb-node-2 shard1]$ cat shard1.conf 
systemLog:
  destination: file
  logAppend: true
  path: /export/mongodb-4.4.18/cluster/shard1/logs/shard1.log

# Where and how to store data.
storage:
  dbPath: /export/mongodb-4.4.18/cluster/shard1/data
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
      cacheSizeGB: 2
# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /export/mongodb-4.4.18/cluster/shard1/shard1.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27001
  bindIp: 0.0.0.0

sharding:
    clusterRole: shardsvr
    
replication:
    replSetName: shard1

# 准备 shard2分片配置文件
[mongod@mongodb-node-2 shard2]$ cd /export/mongodb-4.4.18/cluster/shard2
[mongod@mongodb-node-2 shard2]$ cat shard2.conf 
systemLog:
  destination: file
  logAppend: true
  path: /export/mongodb-4.4.18/cluster/shard2/logs/shard2.log

# Where and how to store data.
storage:
  dbPath: /export/mongodb-4.4.18/cluster/shard2/data
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
      cacheSizeGB: 2
# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /export/mongodb-4.4.18/cluster/shard2/shard2.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27002
  bindIp: 0.0.0.0

sharding:
    clusterRole: shardsvr
    
replication:
    replSetName: shard2

# 准备shard3分片配置文件
[mongod@mongodb-node-2 shard3]$ cd /export/mongodb-4.4.18/cluster/shard3
[mongod@mongodb-node-2 shard3]$ cat shard3.conf 
systemLog:
  destination: file
  logAppend: true
  path: /export/mongodb-4.4.18/cluster/shard3/logs/shard3.log

# Where and how to store data.
storage:
  dbPath: /export/mongodb-4.4.18/cluster/shard3/data
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
      cacheSizeGB: 2
# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /export/mongodb-4.4.18/cluster/shard3/shard3.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27003
  bindIp: 0.0.0.0

sharding:
    clusterRole: shardsvr
    
replication:
    replSetName: shard3

# 准备 mongos-router配置文件,注意mongos没有storage部分配置
[mongod@mongodb-node-2 mongos]$ cd /export/mongodb-4.4.18/cluster/mongos
[mongod@mongodb-node-2 mongos]$ cat mongos.conf 
systemLog:
  destination: file
  logAppend: true
  path: /export/mongodb-4.4.18/cluster/mongos/logs/mongos.log

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

# network interfaces
net:
  port: 20000
  bindIp: 0.0.0.0

sharding:
  configDB: config/10.0.3.1:21000,10.0.3.2:21000,10.0.3.3:21000

  • 第三台设备 10.0.3.3 操作
# 准备configServer配置文件
[mongod@mongodb-node-3 config]$ cd /export/mongodb-4.4.18/cluster/config
[mongod@mongodb-node-3 config]$ cat config.conf 
systemLog:
  destination: file
  logAppend: true
  path: /export/mongodb-4.4.18/cluster/config/logs/config.log

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

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

# network interfaces
net:
  port: 21000
  bindIp: 0.0.0.0

sharding:
  clusterRole: configsvr

replication:
  replSetName: config

# 准备 shard1分片配置文件
[mongod@mongodb-node-3 shard1]$ cd /export/mongodb-4.4.18/cluster/shard1
[mongod@mongodb-node-3 shard1]$ cat shard1.conf 
systemLog:
  destination: file
  logAppend: true
  path: /export/mongodb-4.4.18/cluster/shard1/logs/shard1.log

# Where and how to store data.
storage:
  dbPath: /export/mongodb-4.4.18/cluster/shard1/data
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
      cacheSizeGB: 2
# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /export/mongodb-4.4.18/cluster/shard1/shard1.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27001
  bindIp: 0.0.0.0

sharding:
    clusterRole: shardsvr
    
replication:
    replSetName: shard1


# 准备 shard2分片配置文件
[mongod@mongodb-node-2 shard2]$ cd /export/mongodb-4.4.18/cluster/shard2
[mongod@mongodb-node-2 shard2]$ cat shard2.conf 
systemLog:
  destination: file
  logAppend: true
  path: /export/mongodb-4.4.18/cluster/shard2/logs/shard2.log

# Where and how to store data.
storage:
  dbPath: /export/mongodb-4.4.18/cluster/shard2/data
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
      cacheSizeGB: 2
# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /export/mongodb-4.4.18/cluster/shard2/shard2.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27002
  bindIp: 0.0.0.0

sharding:
    clusterRole: shardsvr
    
replication:
    replSetName: shard2

# 准备shard3分片配置文件
[mongod@mongodb-node-2 shard3]$ cd /export/mongodb-4.4.18/cluster/shard3
[mongod@mongodb-node-2 shard3]$ cat shard3.conf 
systemLog:
  destination: file
  logAppend: true
  path: /export/mongodb-4.4.18/cluster/shard3/logs/shard3.log

# Where and how to store data.
storage:
  dbPath: /export/mongodb-4.4.18/cluster/shard3/data
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
      cacheSizeGB: 2
# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /export/mongodb-4.4.18/cluster/shard3/shard3.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27003
  bindIp: 0.0.0.0

sharding:
    clusterRole: shardsvr
    
replication:
    replSetName: shard3

# 准备 mongos-router配置文件,注意mongos没有storage部分配置
[mongod@mongodb-node-2 mongos]$ cd /export/mongodb-4.4.18/cluster/mongos
[mongod@mongodb-node-2 mongos]$ cat mongos.conf 
systemLog:
  destination: file
  logAppend: true
  path: /export/mongodb-4.4.18/cluster/mongos/logs/mongos.log

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

# network interfaces
net:
  port: 20000
  bindIp: 0.0.0.0

sharding:
  configDB: config/10.0.3.1:21000,10.0.3.2:21000,10.0.3.3:21000

启动服务并初始化

  • 启动三台服务器的config server和配置副本集
[mongod@mongodb-node-1 mongos]$ mongod -f /export/mongodb-4.4.18/cluster/config/config.conf