当前位置: 首页 > news >正文

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

#作者:任少近

文章目录

  • 一、扩容
    • 在245节点上配置
      • 配置config server:
      • 配置mongos
      • 启动config server
      • 安装工具mongosh
      • 添加245新节点到副本集
      • 配置分片副本集
      • 启动路由并分片
  • 二、缩容
      • Conf server上去掉server4
      • shard上去掉server4
      • mongos上去掉server4

一、扩容

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

服务器247服务器248服务器249服务器245
mongosmongosmongosmongos
config serverconfig serverconfig serverconfig 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: configsvrEOF

配置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:27017EOF

注:原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 : 100mode: "slowOp"#作为分片服务
sharding:clusterRole: shardsvrEOF

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 : 100mode: "slowOp"#作为分片服务
sharding:clusterRole: shardsvrEOF

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 : 100mode: "slowOp"#作为分片服务
sharding:clusterRole: shardsvrEOF

启动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

http://www.lryc.cn/news/537424.html

相关文章:

  • Python学习心得字符串拼接的几种方法
  • USB2.03.0摄像头区分UVC相机在linux中的常用命令
  • electron 学习
  • 美术教程2025
  • CPT205 计算机图形学 OpenGL 3D实践(CW2)
  • 基于单片机的开关电源设计(论文+源码)
  • autogen_core中的DataclassJsonMessageSerializer类
  • floodfill算法系列一>太平洋大西洋水流问题
  • DeepSeek 助力 Vue 开发:打造丝滑的无限滚动(Infinite Scroll)
  • JavaScript 内置对象-Math对象
  • 硕成C语言22【一些算法和数组的概念】
  • Halcon相机标定
  • 部门管理(体验,最原始方法来做,Django+mysql)
  • clickhouse集群搭建
  • 250214-java类集框架
  • 二叉树(C语言版)
  • ASP.NET Core 面试宝典【刷题系列】
  • 案例-02.部门管理-查询
  • src和href区别
  • Java每日精进·45天挑战·Day19
  • 区块链的交易管理和共识机制
  • 最新国内 ChatGPT Plus/Pro 获取教程
  • Apollo 9.0 速度动态规划决策算法 – path time heuristic optimizer
  • Apache Iceberg 与 Apache Hudi:数据湖领域的双雄对决
  • 【LeetCode Hot100 普通数组】最大子数组和、合并区间、旋转数组、除自身以外数组的乘积、缺失的第一个正整数
  • 共享存储-一步一步部署ceph分布式文件系统
  • 19.Python实战:实现对博客文章的点赞系统
  • 【stm32】定时器输出PWM波形(hal库)
  • 当Ollama遇上划词翻译:我的Windows本地AI服务搭建日记
  • Linux上Elasticsearch 集群部署指南