MongoDB 扩缩容实战:涵盖节点配置、服务启动与移除操作

#作者:任少近

文章目录

一、扩容

在原三台247,248,249上集群上,扩容一台245节点。

服务器247 服务器248 服务器249 服务器245
mongos mongos mongos mongos
config server config server config server config server
shard1 主节点 shard1 副节点 shard1 仲裁 shard1 副节点
shard2 仲裁 shard2 主节点 shard2 副节点 shard2 副节点
shard3 副节点 shard3 仲裁 shard3 主节点 shard3 副节点

环境配置参考mongodb部署安装

新建目录:

复制代码
mkdir -p /usr/local/mongodb/config/{conf,data,log,run}
mkdir -p /usr/local/mongodb/mongos/{conf,data,log,run}
mkdir -p /usr/local/mongodb/shard{1,2,3}/{conf,data,log}

在245节点上配置

配置config server:

复制代码
cat > /usr/local/mongodb/config/conf/mongod.conf  <<  EOF
# 日志设置
systemLog:
  destination: file                # 日志写入文件
  path: /usr/local/mongodb/config/log/mongod.log # 日志文件路径
  logAppend: true                  # 追加日志
  logRotate: rename                # 日志轮转方式,支持 rename 或 reopen

# 网络设置
net:
  port: 27017                     # MongoDB 默认端口
  bindIp: 0.0.0.0                 # 允许从所有 IP 访问,生产环境建议限制

# 数据目录
storage:
  dbPath: /usr/local/mongodb/config/data  # 数据文件存放路径
  wiredTiger:
    engineConfig:
      cacheSizeGB: 1               # 根据情况配置内存

# 进程设置
processManagement:
  fork: true                       # 以后台进程方式运行
  pidFilePath: /usr/local/mongodb/config/run/mongod.pid # PID 文件路径

#复制集名称
replication:
  replSetName: "myconfigset"


#作为配置服务
sharding:
  clusterRole: configsvr

EOF

配置mongos

sharding:

configDB: 此处要增加server4:27017

复制代码
cat > /usr/local/mongodb/config/conf/mongod.conf  <<  EOF

# 日志设置
systemLog:
  destination: file                # 日志写入文件
  path: /usr/local/mongodb/mongos/log/mongos.log # 日志文件路径
  logAppend: true                  # 追加日志
  logRotate: rename                # 日志轮转方式,支持 rename 或 reopen

# 网络设置
net:
  port: 27000                    # MongoDB 默认端口
  bindIp: 0.0.0.0                 # 允许从所有 IP 访问,生产环境建议限制

# 进程设置
processManagement:
  fork: true                       # 以后台进程方式运行
  pidFilePath: /usr/local/mongodb/mongos/run/mongos.pid # PID 文件路径

#网络延迟阈值
replication:
  localPingThresholdMs: 15

#关联配置服务
sharding:
  configDB: myconfigset/server1:27017,server2:27017,server3:27017,server4:27017

EOF

注:原247,248,249的工作mongos上要新增配置:

复制代码
#关联配置服务
sharding:
  configDB: myconfigset/server1:27017,server2:27017,server3:27017,server4:27017

启动config server

复制代码
mongod --config /usr/local/mongodb/config/conf/mongod.conf

安装工具mongosh

安装好mongosh工具,方便初始化副本集使用,

复制代码
# mongosh mongodb://server1:27017。成功登录如下图:默认的提示符是'test>'

大内存页 关闭 hugepage

echo "never" > /sys/kernel/mm/transparent_hugepage/enabled

echo "never" > /sys/kernel/mm/transparent_hugepage/defrag

添加245新节点到副本集

启动config server后,登录到主节点上进行操作。

复制代码
#7.0.14版本登录时在test>数据库下,切换到admin数据库
test> use admin
switched to db admin
admin>

rs.status()

已新增

配置分片副本集

增加shard1,shard2,shard3配置

Shard1

复制代码
cat > /usr/local/mongodb/shard1/conf/shard1.conf  << EOF
# 日志设置
systemLog:
  destination: file                # 日志写入文件
  path: /usr/local/mongodb/shard1/log/shard1.log # 日志文件路径
  logAppend: true                  # 追加日志
  logRotate: rename                # 日志轮转方式,支持 rename 或 reopen

# 网络设置
net:
  port: 27001                     # MongoDB shard1端口
  bindIp: 0.0.0.0                 # 允许从所有 IP 访问,生产环境建议限制

# 数据目录
storage:
  dbPath: /usr/local/mongodb/shard1/data          # 数据文件存放路径
  wiredTiger:
    engineConfig:
      cacheSizeGB: 5                              # 根据情况配置内存


# 进程设置
processManagement:
  fork: true                       # 以后台进程方式运行
  pidFilePath: /usr/local/mongodb/shard1/data/shard1.pid # PID 文件路径


#复制集名称
replication:
  replSetName: "shard1"

#慢查询
operationProfiling:
  slowOpThresholdMs : 100
  mode: "slowOp"

#作为分片服务
sharding:
  clusterRole: shardsvr

EOF

Shard2

复制代码
cat > /usr/local/mongodb/shard2/conf/shard2.conf  << EOF

# 日志设置
systemLog:
  destination: file                # 日志写入文件
  path: /usr/local/mongodb/shard2/log/shard2.log # 日志文件路径
  logAppend: true                  # 追加日志
  logRotate: rename                # 日志轮转方式,支持 rename 或 reopen

# 网络设置
net:
  port: 27002                     # shard2端口
  bindIp: 0.0.0.0                 # 允许从所有 IP 访问,生产环境建议限制

# 数据目录
storage:
  dbPath: /usr/local/mongodb/shard2/data          # 数据文件存放路径
  wiredTiger:
    engineConfig:
      cacheSizeGB: 5                              # 根据情况配置内存

# 进程设置
processManagement:
  fork: true                       # 以后台进程方式运行
  pidFilePath: /usr/local/mongodb/shard2/data/shard2.pid # PID 文件路径


#复制集名称
replication:
  replSetName: "shard2"


#慢查询
operationProfiling:
  slowOpThresholdMs : 100
  mode: "slowOp"


#作为分片服务
sharding:
  clusterRole: shardsvr

EOF

Shard3

复制代码
cat > /usr/local/mongodb/shard3/conf/shard3.conf  << EOF

# 日志设置
systemLog:
  destination: file                # 日志写入文件
  path: /usr/local/mongodb/shard3/log/shard3.log # 日志文件路径
  logAppend: true                  # 追加日志
  logRotate: rename                # 日志轮转方式,支持 rename 或 reopen

# 网络设置
net:
  port: 27003                     # MongoDB shard3 默认端口
  bindIp: 0.0.0.0                 # 允许从所有 IP 访问,生产环境建议限制

# 数据目录
storage:
  dbPath: /usr/local/mongodb/shard3/data          # 数据文件存放路径
  wiredTiger:
    engineConfig:
      cacheSizeGB: 5                             # 根据情况配置内存

# 进程设置
processManagement:
  fork: true                       # 以后台进程方式运行
  pidFilePath: /usr/local/mongodb/shard3/data/shard3.pid # PID 文件路径


#复制集名称
replication:
  replSetName: "shard3"

#慢查询
operationProfiling:
  slowOpThresholdMs : 100
  mode: "slowOp"

#作为分片服务
sharding:
  clusterRole: shardsvr

EOF

启动shard server

复制代码
mongod --config  /usr/local/mongodb/shard1/conf/shard1.conf
mongod --config  /usr/local/mongodb/shard2/conf/shard2.conf
mongod --config  /usr/local/mongodb/shard3/conf/shard3.conf

登录主节点,注意端口号,注意安装了mongosh

复制代码
mongosh mongodb://server1:27001
mongosh mongodb://server2:27002
mongosh mongodb://server3:27003

登录主shard1节点,添加新shard1到分片副本集中

复制代码
#7.0.14版本登录时在test>数据库下,切换到admin数据库
test> use admin
switched to db admin
admin>rs.add({ host: "server4:27001" });

登录主shard2节点,添加新shard2到分片副本集中

复制代码
#7.0.14版本登录时在test>数据库下,切换到admin数据库
test> use admin
switched to db admin
admin>rs.add({ host: "server4:27002" });

rs.status();

登录主shard3节点,添加新shard3到分片副本集中

复制代码
#7.0.14版本登录时在test>数据库下,切换到admin数据库
test> use admin
switched to db admin
admin>rs.add({ host: "server4:27003" });

rs.status();

启动路由并分片

启动服务器的mongos

复制代码
mongos --config  /usr/local/mongodb/mongos/conf/mongos.conf

登录任意路由节点,注意端口号,注意安装了mongosh

复制代码
mongosh mongodb://server1:27000

添加分片

复制代码
#7.0.14版本登录时在test>数据库下,切换到admin数据库
test> use admin
switched to db admin
admin>

##添加分片
sh.addShard("shard1/server1:27001,server2:27001,server3:27001,server4:27001")
sh.addShard("shard2/server1:27002,server2:27002,server3:27002,server4:27002")
sh.addShard("shard3/server1:27003,server2:27003,server3:27003,server4:27003")

二、缩容

Conf server上去掉server4

主登录到主节点上,操作:

rs.remove("server4:27017");

shard上去掉server4

登录到主节点上,分别到shard1,shard2,shard3上删除


mongos上去掉server4

修改改mongos.conf文件,去掉

重启mongos

相关推荐
Databend30 分钟前
Databend 亮相 RustChinaConf 2025,分享基于 Rust 构建商业化数仓平台的探索
数据库
得物技术1 小时前
破解gh-ost变更导致MySQL表膨胀之谜|得物技术
数据库·后端·mysql
似水流年流不尽思念5 小时前
MongoDB 有哪些索引?适用场景?
后端·mongodb
Raymond运维6 小时前
MariaDB源码编译安装(二)
运维·数据库·mariadb
沢田纲吉6 小时前
🗄️ MySQL 表操作全面指南
数据库·后端·mysql
RestCloud21 小时前
SQL Server到Hive:批处理ETL性能提升30%的实战经验
数据库·api
RestCloud1 天前
为什么说零代码 ETL 是未来趋势?
数据库·api
ClouGence1 天前
CloudCanal + Paimon + SelectDB 从 0 到 1 构建实时湖仓
数据库
DemonAvenger1 天前
NoSQL与MySQL混合架构设计:从入门到实战的最佳实践
数据库·mysql·性能优化
AAA修煤气灶刘哥2 天前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql