简介
为何使用分片集
当MongoDB存储海量的数据时,一台机器可能不足以存储数据,也可能不足以提供可接受的读写吞吐量。这时,我们就可以通过在多台机器上分割数据,使得数据库系统能存储和处理更多的数据。
复制所有的写入操作到主节点
延迟的敏感数据会在主节点查询
单个副本集限制在12个节点
当请求量巨大时会出现内存不足。
本地磁盘不足
垂直扩展价格昂贵
架构
A MongoDB sharded cluster consists of the following components:
- shard: Each shard contains a subset of the sharded data. Each shard can be deployed as a replica set.
- mongos: The mongos acts as a query router, providing an interface between client applications and the sharded cluster. Starting in MongoDB 4.4, mongos can support hedged reads to minimize latencies.
- config servers: Config servers store metadata and configuration settings for the cluster.
1.前置条件(官方推荐生产环境使用此种)
To avoid configuration updates due to IP address changes, use DNS hostnames instead of IP addresses. It is particularly important to use a DNS hostname instead of an IP address when configuring replica set members or sharded cluster members.
Use hostnames instead of IP addresses to configure clusters across a split network horizon. Starting in MongoDB 5.0, nodes that are only configured with an IP address will fail startup validation and will not start.
2.配置 Config Server
配置成副本集模式。config1,config2,config3
2.1创建文件夹
mkdir -p /data/dbs/config1/data
mkdir -p /data/dbs/config1/logs
2.2 vim /data/dbs/config1/mongod.conf
systemLog:
destination: file
logAppend: true
path: /data/dbs/config1/logs/mongod.log
storage:
dbPath: /data/dbs/config1/data
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /data/dbs/config1/mongod.pid
net:
port: 27018
bindIp: 0.0.0.0
sharding:
clusterRole: configsvr
replication:
replSetName: config
2.3启动
/home/mongodb/mongodb/bin/mongod -f /data/dbs/config1/mongod.conf
/home/mongodb/mongodb/bin/mongod -f /data/dbs/config2/mongod.conf
/home/mongodb/mongodb/bin/mongod -f /data/dbs/config3/mongod.conf
2.4 初始化
/home/mongodb/mongodb/bin/mongo --port 27018
rs.initiate(
{
_id: "config",
configsvr: true,
members: [
{ _id : 0, host : "10.6.8.176:27018" },
{ _id : 1, host : "10.6.8.176:27028" },
{ _id : 2, host : "10.6.8.176:27038" }
]
}
)
3.配置Shard
配置成副本集模式。shard1,shard2,shard3
3.1创建文件夹
mkdir -p /data/dbs/shard1/data
mkdir -p /data/dbs/shard1/logs
3.2 vim /data/dbs/config1/mongod.conf
systemLog:
destination: file
logAppend: true
path: /data/dbs/shard1/logs/mongod.log
storage:
dbPath: /data/dbs/shard1/data
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /data/dbs/shard1/mongod.pid
net:
port: 27019
bindIp: 0.0.0.0
sharding:
clusterRole: shardsvr
replication:
replSetName: shard1
3.3 启动
/home/mongodb/mongodb/bin/mongod -f /data/dbs/shard1/mongod.conf
/home/mongodb/mongodb/bin/mongod -f /data/dbs/shard2/mongod.conf
/home/mongodb/mongodb/bin/mongod -f /data/dbs/shard3/mongod.conf
3.4 初始化
shard1
/home/mongodb/mongodb/bin/mongo --port 27019
rs.initiate(
{
_id: "shard1",
members: [
{ _id : 0, host : "10.6.8.176:27019" }
]
}
)
shard2
/home/mongodb/mongodb/bin/mongo --port 27020
rs.initiate(
{
_id: "shard2",
members: [
{ _id : 0, host : "10.6.8.176:27020" }
]
}
)
shard3
/home/mongodb/mongodb/bin/mongo --port 27021
rs.initiate(
{
_id: "shard3",
members: [
{ _id : 0, host : "10.6.8.176:27021" }
]
}
)
4. 配置mongos-router
配置成副本集模式或者单个都可以。
4.1 创建文件夹
mkdir -p /data/dbs/router1/data
mkdir -p /data/dbs/router1/logs
4.2 vim /data/dbs/router1/mongod.conf
systemLog:
destination: file
logAppend: true
path: /data/dbs/router1/logs/mongod.log
storage:
dbPath: /data/dbs/router1/data
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /data/dbs/router1/mongod.pid
net:
port: 27017
bindIp: 0.0.0.0
sharding:
configDB: config/10.6.8.186:27018
4.3 启动
/home/mongodb/mongodb/bin/mongos -f /data/dbs/router1/mongod.conf
4.4 初始化
/home/mongodb/mongodb/bin/mongo --port 27017
sh.addShard( "shard1/10.6.8.176:27019")
sh.addShard( "shard2/10.6.8.176:27020")
sh.addShard( "shard3/10.6.8.176:27021")
4.5 删除分片
这时候如果该shard为你要删除的对象,那么需要先删除或者移动这些对象,删除不用解释,正式环境也不允许你操作,下面看下movePrimary,
db.runCommand( { removeShard: "shard1" } )
{
"msg" : "draining started successfully",
"state" : "started",
"shard" : "shard1",
"note" : "you need to drop or movePrimary these databases",
"dbsToMove" : [
"friend",
"users"
],
"ok" : 1
}
移动数据到其他分片
use admin
db.runCommand( { movePrimary: "friend", to: "shard2" })
db.runCommand( { movePrimary: "users", to: "shard2" })
最后再进行删除
db.runCommand( { removeShard: "shard1" } )
db.adminCommand( { removeShard : "shard1" } )
5.验证分片
/home/mongodb/mongodb/bin/mongo --port 27017
5.1为集合启用分片,设置分片规则
use db0001;
sh.enableSharding("db0001")
sh.shardCollection("db0001.student", {"_id": "hashed" })
5.2插入数据
for (i = 1; i <= 1000; i=i+1){
db.student.insert({'xh': i,'name':'name'+i})
}
5.3查看各分片数量
/home/mongodb/mongodb/bin/mongo --port 27029
/home/mongodb/mongodb/bin/mongo --port 27020
/home/mongodb/mongodb/bin/mongo --port 27021
use db0001;
db.student.find().count()