帮助中心/最新通知

质量为本、客户为根、勇于拼搏、务实创新

< 返回文章列表

【服务器相关】MongoDB分片集群部署详解

发表时间:2025-06-16 03:46:00 小编:主机乐-Yutio

 一、环境说明

1、我们prod环境MongoDB的集群架构是做的分片集群的部署,但是目前我们没有分片,即所有数据都在一个分片上,后期如果数量大,需要分配,集群随时可以分片,对业务方透明

2、各个角色的部署情况

角色IP端口复制集名称
mongos172.21.244.101,172.21.244.102,172.21.244.9427000
config server172.21.244.101,172.21.244.102,172.21.244.9427100repl_configsvr
存储节点(shard)172.21.244.101,172.21.244.102,172.21.244.9427101shard1

3、MongoDB版本

二、基础信息准备

0、系统优化

三、集群初始化

1、启动 config server 服务

四、交付业务方

1、建立应用账号

五、数据库启用分片

如果后期业务量大,需要开启分片,配置如下


#指定需要分片的数据库
mongos> sh.enableSharding(“chj_db”)
{
“ok” : 1,
“operationTime” : Timestamp(1557546835, 3),
“$clusterTime” : {
“clusterTime” : Timestamp(1557546835, 3),
“signature” : {
“hash” : BinData(0,”bkrrr8Kxrr9j9udrDc/hURHld38=”),
“keyId” : NumberLong(“6689575940508352541”)
}
}
}
#在chj_db数据库和users集合中创建了name和age为升序的片键
mongos> sh.shardCollection(“chj_db.users”,{name:1,age:1})
{
“collectionsharded” : “chj_db.users”,
“collectionUUID” : UUID(“59c0b99f-efff-4132-b489-f6c7e3d98f42”),
“ok” : 1,
“operationTime” : Timestamp(1557546861, 12),
“$clusterTime” : {
“clusterTime” : Timestamp(1557546861, 12),
“signature” : {
“hash” : BinData(0,”UBB1A/YODnmXwG5eAhgNLcKVzug=”),
“keyId” : NumberLong(“6689575940508352541”)
}
}
}
#查看分片情况
mongos> sh.status()
— Sharding Status —
sharding version: {
“_id” : 1,
“minCompatibleVersion” : 5,
“currentVersion” : 6,
“clusterId” : ObjectId(“5cd625e0da695346d740f749”)
}
shards:
{ “_id” : “shard1”, “host” : “shard1/172.21.244.101:27101,172.21.244.102:27101”, “state” : 1 }
active mongoses:
“4.0.4-62-g7e345a7” : 3
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
{ “_id” : “chj_db”, “primary” : “shard1”, “partitioned” : true, “version” : { “uuid” : UUID(“82088bc7-7b98-4033-843d-7058d8d959f6”), “lastMod” : 1 } }
chj_db.users
shard key: { “name” : 1, “age” : 1 }
unique: false
balancing: true
chunks:
shard1 1
{ “name” : { “$minKey” : 1 }, “age” : { “$minKey” : 1 } } –>> { “name” : { “$maxKey” : 1 }, “age” : { “$maxKey” : 1 } } on : shard1 Timestamp(1, 0)
{ “_id” : “config”, “primary” : “config”, “partitioned” : true }
config.system.sessions
shard key: { “_id” : 1 }
unique: false
balancing: true
chunks:
shard1 1
{ “_id” : { “$minKey” : 1 } } –>> { “_id” : { “$maxKey” : 1 } } on : shard1 Timestamp(1, 0)

本篇文章到此结束,如果您有相关技术方面疑问可以联系我们技术人员远程解决,感谢大家支持本站!


联系我们
返回顶部