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

准备基本环境

服务器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和配置副本集
# 连接3个节点执行以下命令分别启动configServer
[mongod@mongodb-node-1 mongos]$ mongod -f /export/mongodb-4.4.18/cluster/config/config.conf

# 连接到第一个节点mongodb-node-1
[mongod@mongodb-node-1 ~]$ mongo 10.0.3.1 --port 21000 
rs.initiate(
  {
    _id: "config",
    configsvr: true,
    members: [
      { _id : 0, host : "10.0.3.1:21000" },
      { _id : 1, host : "10.0.3.2:21000" },
      { _id : 2, host : "10.0.3.3:21000" }
    ]
  }
)

# 查看节点状态
rs.status()

  • 三台服务器启动shard 分片并初始化shard分片
# 3个节点每个节点执行以下命令启动3个shard分片副本集
[mongod@mongodb-node-1 ~]$ mongod -f /export/mongodb-4.4.18/cluster/shard1/shard1.conf
[mongod@mongodb-node-1 ~]$ mongod -f /export/mongodb-4.4.18/cluster/shard2/shard2.conf
[mongod@mongodb-node-1 ~]$ mongod -f /export/mongodb-4.4.18/cluster/shard3/shard3.conf 

# 连接到第一个shard分片并初始化
[mongod@mongodb-node-1 ~]$ mongo 10.0.3.1 --port 27001
config = { _id : "shard1",members : [{_id : 0, host : "10.0.3.1:27001" ,priority: 2 },{_id : 1, host : "10.0.3.2:27001" ,priority: 1 },{_id : 2, host : "10.0.3.3:27001",arbiterOnly: true}]}//(“priority”优先级,数字越大,优先等级越高;“arbiterOnly”冲裁节点;冲裁节点根据优先等级判断哪个节点作为主节点)
rs.initiate(config)


> rs.initiate(config)
{ "ok" : 1 }
shard1:SECONDARY> 

#查看节点状态
rs.status()


# 连接到第二个shard分片并初始化
[mongod@mongodb-node-1 ~]$ mongo 10.0.3.1 --port 27002
config = { _id : "shard2",members : [{_id : 0, host : "10.0.3.1:27002" ,arbiterOnly: true },{_id : 1, host : "10.0.3.2:27002" ,priority: 2 },{_id : 2, host : "10.0.3.3:27002",priority: 1}]}
rs.initiate(config)

# 连接到第三个shard分片并初始化
[mongod@mongodb-node-1 ~]$ mongo 10.0.3.1 --port 27003
use admin
config = { _id : "shard3",members : [{_id : 0, host : "10.0.3.1:27003" ,priority: 1 },{_id : 1, host : "10.0.3.2:27003" ,arbiterOnly: true },{_id : 2, host : "10.0.3.3:27003",priority: 2}]}
rs.initiate(config)

# 查看各个分片副本集节点状态
rs.status()

  • 三台服务器 启动并初始化mongos
# 3个节点启动mongos-router,注意启动时使用mongos命令而非mongod命令:
[mongod@mongodb-node-1 ~]$ mongos -f /export/mongodb-4.4.18/cluster/mongos/mongos.conf 

# 连接到第一个mongos节点mongodb01并初始化
[mongod@mongodb-node-1 ~]$ mongo 10.0.3.1:20000

# 执行以下操作将3个分片副本集添加到集群:
sh.addShard("shard1/10.0.3.1:27001,10.0.3.2:27001,10.0.3.3:27001")
sh.addShard("shard2/10.0.3.1:27002,10.0.3.2:27002,10.0.3.3:27002")
sh.addShard("shard3/10.0.3.1:27003,10.0.3.2:27003,10.0.3.3:27003")

# 查看mongos状态
sh.status()


开启密码认证

  • 登陆 mongo01 10.0.3.1 创建账号密码(提前创建好账号密码)
[mongod@mongodb-node-1 cluster]$ mongo   10.0.3.1:20000 
> use admin
##创建账号/密码##
db.createUser({ user: 'admin', pwd: '123456', roles: [ { role: "root", db: "admin" } ] });
  • 在 mongo01 10.0.3.1 上创建keyfile密钥文件
[mongod@mongodb-node-1 cluster]$ cd /export/mongodb-4.4.18/cluster
[mongod@mongodb-node-1 cluster]$ openssl rand -base64 756 >keyFile.key 
[mongod@mongodb-node-1 cluster]$ chmod 400 keyFile.key 
[mongod@mongodb-node-1 cluster]$ scp keyFile.key 10.0.3.2:/export/mongodb-4.4.18/cluster
[mongod@mongodb-node-1 cluster]$ scp keyFile.key 10.0.3.3:/export/mongodb-4.4.18/cluster


  • 在 config、shard1、shard2、shard3、mongos添加认证配置
# 要先关闭所有 mongos 进程,修改配置文件,增加认证信息,然后再依次启动进程

# config 配置文件
[mongod@mongodb-node-1 cluster]$ cat config/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

security:
  keyFile: /export/mongodb-4.4.18/cluster/keyFile.key 
  authorization: enabled



# shard1配置文件
[mongod@mongodb-node-1 cluster]$ cat shard1/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

security:
  keyFile: /export/mongodb-4.4.18/cluster/keyFile.key 
  authorization: enabled


# shard2配置文件
[mongod@mongodb-node-1 cluster]$ cat shard2/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

security:
  keyFile: /export/mongodb-4.4.18/cluster/keyFile.key 
  authorization: enabled


# shard3配置文件
[mongod@mongodb-node-1 cluster]$ cat shard3/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

security:
  keyFile: /export/mongodb-4.4.18/cluster/keyFile.key 
  authorization: enabled


# mongos 配置文件,注意没有 authorization: enabled
[mongod@mongodb-node-1 cluster]$ cat mongos/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


security:
  keyFile: /export/mongodb-4.4.18/cluster/keyFile.key 


  • 依次3台机器服务
mongod -f /export/mongodb-4.4.18/cluster/config/config.conf 
mongod -f /export/mongodb-4.4.18/cluster/shard1/shard1.conf 
mongod -f /export/mongodb-4.4.18/cluster/shard2/shard2.conf 
mongod -f /export/mongodb-4.4.18/cluster/shard3/shard3.conf 
mongos -f /export/mongodb-4.4.18/cluster/mongos/mongos.conf 

#  按照先后顺序来处理关闭,mongos>config>shadr3>shadr2>shadr1

mongodb使用方法

# mongodb 使用方法:
1.查看数据分布
use testdb;
show tables;
db.order.getShardDistribution()

2.创建片健
在 指定testdb分片生效
sh.enableSharding("testdb")

3.片键有两种模式:hash模式,range模式
使用hash模式分片:  记录在各片上的分布比较平均
sh.shardCollection( "testdb.person", { "_id": "hashed" } )

使用range模式分片
mongos> sh.shardCollection( "testdb.person", { "name": 1 } )


要分片的库原来有数据的情况下,先建index,然后再指定片键
mongos> sh.enableSharding("new2")
mongos> use new2
mongos> db.user2.createIndex( { "username": 1 } )
mongos> sh.shardCollection( "new2.user2", { "username": 1 } )

发片后,片健不可改变;一个集合只能有一个片健;片健必须有索引


4.分片的元数据
不要直接到config server上查看分片集群的元数据信息,这些数据非常重要,安全的方式是通过mongos连接到config数据查看,或者使用sh辅助函数查看。
使用sh辅助函数查看
连接到mongos查看config数据库中的集合 
mongos> use config
mongos> db.shards.find()
{ "_id" : "shard1", "host" : "shard1/10.0.3.1:27001,10.0.3.2:27001", "state" : 1 }
{ "_id" : "shard2", "host" : "shard2/10.0.3.2:27002,10.0.3.3:27002", "state" : 1 }
{ "_id" : "shard3", "host" : "shard3/10.0.3.1:27003,10.0.3.3:27003", "state" : 1 }

databases集合保存分片集群中所有数据库的信息,不管数据库是否分片
mongos> db.databases.find()
{ "_id" : "testdb", "primary" : "shard1", "partitioned" : true, "version" : { "uuid" : UUID("b748ce93-b28d-43ad-a0fa-843813d9e9ff"), "lastMod" : 1 } }
{ "_id" : "test", "primary" : "shard1", "partitioned" : true, "version" : { "uuid" : UUID("c98e8734-92ad-4644-968d-d5aed0044e89"), "lastMod" : 1 } }

测试

#连接任意一台mongos节点
mongo  -u admin --port 20000 
use admin

#为数据库启用分片
为testdb数据库启用分片
sh.enableSharding("testdb")

#为集合启用分片
为order集合设置分片规则
sh.shardCollection("testdb.order", {"_id": "hashed" })

#插入100000条数据进行验证
use testdb
for (i = 1; i <= 100000; i=i+1){
    db.order.insert({'price': 1})
}

#查看插入的数据量
db.order.find().count()

#查看分片情况
db.order.stats();
"ns" : "testdb.order",
        "count" : 99999,
        "size" : 8388810,
        "storageSize" : 4153344,
        "totalIndexSize" : 9805824,
        "totalSize" : 13959168,
        "indexSizes" : {
                "_id_" : 4915200,
                "_id_hashed" : 4890624
        },
        "avgObjSize" : 83,
        "maxSize" : NumberLong(0),
        "nindexes" : 2,
        "scaleFactor" : 1,
        "nchunks" : 6,
        "shards" : {
                "shard1" : {
                        "ns" : "testdb.order",
                        "size" : 2757979,
                        "count" : 32877,
                        "avgObjSize" : 83,
                        "storageSize" : 1368064,
                        "freeStorageSize" : 679936,
                        "capped" : false,
                        "wiredTiger" : {
                                "metadata" : {
                                        "formatVersion" : 1
                                }
                                .........
                                
                "shard2" : {
                        "ns" : "testdb.order",
                        "size" : 2816622,
                        "count" : 33576,
                        "avgObjSize" : 83,
                        "storageSize" : 1396736,
                        "freeStorageSize" : 692224,
                        "capped" : false,
                        "wiredTiger" : {
                                "metadata" : {
                                        "formatVersion" : 1
                                }
                                .........
                "shard3" : {
                        "ns" : "testdb.order",
                        "size" : 2814209,
                        "count" : 33546,
                        "avgObjSize" : 83,
                        "storageSize" : 1388544,
                        "freeStorageSize" : 688128,
                        "capped" : false,
                        "wiredTiger" : {
                                "metadata" : {
                                        "formatVersion" : 1
                                }
                                .........
              
              
     以看到数据分到3个分片,各自分片数量为: shard1 “count” : 32877,shard2 “count” : 33576,shard3 “count” : 33547


  • 参考文献
https://blog.csdn.net/msh6453/article/details/131161845
https://blog.csdn.net/weixin_49724150/article/details/121748365
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章