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

相关推荐
余衫马31 分钟前
CentOS7 离线安装 Postgresql 指南
数据库·postgresql
E___V___E1 小时前
MySQL数据库入门到大蛇尚硅谷宋红康老师笔记 高级篇 part 2
数据库·笔记·mysql
m0_748254881 小时前
mysql之如何获知版本
数据库·mysql
mikey棒棒棒2 小时前
Redis——优惠券秒杀问题(分布式id、一人多单超卖、乐悲锁、CAS、分布式锁、Redisson)
数据库·redis·lua·redisson·watchdog·cas·并发锁
水手胡巴3 小时前
oracle apex post接口
数据库·oracle
史迪仔01125 小时前
【SQL】SQL多表查询
数据库·sql
Quz6 小时前
MySQL:修改数据库默认存储目录与数据迁移
数据库·mysql
Familyism6 小时前
Redis
数据库·redis·缓存
隔壁老登6 小时前
查询hive指定数据库下所有表的建表语句并生成数据字典
数据库·hive·hadoop
sekaii6 小时前
ReDistribution plan细节
linux·服务器·数据库