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

相关推荐
智_永无止境几秒前
Redis 8.0携新功能,重新开源
数据库·redis·开源
阿乾之铭22 分钟前
Spring Boot 参数验证
java·数据库·mysql
唐人街都是苦瓜脸1 小时前
MySQL创建了一个索引表,如何来验证这个索引表是否使用了呢?
数据库·mysql
前进的程序员1 小时前
SQLite 数据库常见问题及解决方法
数据库·sqlite
zhcong_1 小时前
MySQL数据库操作
数据库·mysql
xq5148633 小时前
Linux系统下安装mongodb
linux·mongodb
极小狐3 小时前
极狐GitLab 容器镜像仓库功能介绍
java·前端·数据库·npm·gitlab
极小狐3 小时前
如何构建容器镜像并将其推送到极狐GitLab容器镜像库?
开发语言·数据库·机器学习·gitlab·ruby
阿四啊4 小时前
【Redis实战篇】分布式锁-Redisson
数据库·redis·分布式
_星辰大海乀4 小时前
数据库约束
java·数据结构·数据库·sql·链表