温馨提示×

centos mongodb分片技术入门

小樊
53
2025-08-16 15:19:40
栏目: 云计算

以下是在CentOS上实现MongoDB分片技术的基本步骤:

  1. 安装MongoDB:在所有节点上安装相同版本的MongoDB,可通过yum install -y mongodb-org命令安装。
  2. 配置配置服务器(Config Server)
    • 至少部署3个配置服务器,用于存储集群元数据。
    • 启动命令示例:mongod --configsvr --replSet configReplSet --dbpath /data/configdb --port 27019
    • 初始化副本集:连接到配置服务器,执行rs.initiate()命令。
  3. 配置分片服务器(Shard Server)
    • 每个分片需配置为副本集,存储实际数据。
    • 启动命令示例:mongod --shardsvr --replSet shardReplSet --dbpath /data/db --port 27018
    • 初始化副本集:rs.initiate()
  4. 配置路由服务器(Mongos)
    • 启动Mongos并连接配置服务器:mongos --configdb configReplSet/cfg1:27019,cfg2:27019,cfg3:27019 --port 27017
  5. 添加分片到集群
    • 连接到Mongos,执行sh.addShard("shardReplSet/shard1:27018,shard2:27018,shard3:27018")
  6. 启用数据库和集合分片
    • 启用数据库分片:sh.enableSharding("yourDatabase")
    • 为集合指定分片键:sh.shardCollection("yourDatabase.yourCollection", { "shardKey": 1 })
  7. 验证分片状态
    • 执行sh.status()查看集群状态,确认分片是否正常。

注意事项

  • 确保节点间网络互通,防火墙开放对应端口(默认27017、27019等)。
  • 选择合适的分片键(如非单调递增字段),避免数据分布不均。
  • 生产环境建议配置监控(如MongoDB Ops Manager)和定期备份。

参考资料:

0