版本矩阵

类别配置项
MongoDB 版本Community 7.0.x / 6.0.x
操作系统Ubuntu 22.04/24.04, CentOS 7+

端口规划

  • Config Server (cfg): 17017 / 17018 / 17019
  • Shard1: 37017-37019
  • Shard2: 47017-47019
  • Mongos: 27017

分片集群核心组件

  1. 分片(Shards):实际存储数据的节点
  2. 配置服务器(Config Servers):存储集群元数据
  3. 查询路由器(Mongos):应用程序的访问入口

配置节点搭建

dbpath=config/config1
port=17017
configsvr=true
replSet=configsvr

初始化

var cfg = {
  "_id":"configsvr",
  "members":[
    {"_id":1,"host":"10.10.52.38:17017"},
    {"_id":2,"host":"10.10.52.38:17018"},
    {"_id":3,"host":"10.10.52.38:17019"}
  ]
};
rs.initiate(cfg);

Mongos路由节点

port=27017
configdb=configsvr/10.10.52.38:17017,10.10.52.38:17018,10.10.52.38:17019

添加分片

sh.addShard("shard1/10.10.52.38:37017,10.10.52.38:37018,10.10.52.38:37019");
sh.addShard("shard2/10.10.52.38:47017,10.10.52.38:47018,10.10.52.38:47019");

开启分片

sh.enableSharding("wzk_test")
sh.shardCollection("wzk_test.wzk_test_data",{"name": "hashed"})

错误速查

症状/报错可能原因修复方法
mongos连不上configsvr未就绪等待PRIMARY选举完成
addShard失败副本集名不一致统一副本集名称
数据只落一个分片未开启balancer开启balancer
jumbo chunk搬不动chunk过大先拆分chunk