mongodb分片集群搭建包含三种角色:
1. 配置服务器,负责整个集群的配置管理;
2. 分片服务器,负责处理分片内的数据;
3. 路由服务器,负责接收请求,并转发给合适的分片服务器进行业务处理;
1. 安装包下载地址
MongoDB Community Download | MongoDB
这里下载压缩版,不选择msi版本。
将压缩包解压后,拷贝10份,分别命名目录为:
?
?mongo-config是配置服务器,后面的数字27018,27019,27020是端口号。
mongo-router是路由服务器,后面的数字27024是端口号。
mongo-shard是分片服务器,后面的数字是端口号,其中27021,27022,27023作为一个副本集,27031,27032,27033作为另外一个副本集。
2. 配置服务器部署
以管理员打开命令行窗口,切换到配置服务器目录下,新建data目录和log目录,在bin目录下创建配置文件mongod.cfg。
在mongo-config-27018下的bin目录下的配置文件mongod.cfg中添加如下内容:
storage:
dbPath: F:\Apps\mongo-cluster\mongo-config-27018\data
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: F:\Apps\mongo-cluster\mongo-config-27018\log\mongod.log
# network interfaces
net:
port: 27018
bindIp: 127.0.0.1
#processManagement:
#security:
#operationProfiling:
replication:
replSetName: 'mongodb-test'
sharding:
clusterRole: configsvr
执行安装服务命令:
mongod --config F:\Apps\mongo-cluster\mongo-config-27018\bin\mongod.cfg --serviceName "MongoDB27018" --serviceDisplayName "MongoDB27018" --install
mongod --config F:\Apps\mongo-cluster\mongo-config-27019\bin\mongod.cfg --serviceName "MongoDB27019" --serviceDisplayName "MongoDB27019" --install
mongod --config F:\Apps\mongo-cluster\mongo-config-27020\bin\mongod.cfg --serviceName "MongoDB27020" --serviceDisplayName "MongoDB27020" --install
执行启动服务命令:
net start MongoDB27018
net start MongoDB27019
net start MongoDB27020
?测试配置服务可用性:
mongo --host 127.0.0.1 --port 27018
mongo --host 127.0.0.1 --port 27019
mongo --host 127.0.0.1 --port 27020
?mongo-config-27019,mongo-config-27020参考上面的方式进行配置和启动,完成后添加副本集。
添加副本集
rs.initiate({
_id: 'mongodb-test',
configsvr: true,
members: [
{_id: 0, host: '127.0.0.1:27018'},
{_id: 1, host: '127.0.0.1:27019'},
{_id: 2, host: '127.0.0.1:27020'}]
})
3. 分片服务器部署
以管理员打开命令行窗口,切换到分片服务器目录下,新建data目录和log目录,在bin目录下创建配置文件mongo.config。
在mongo-config-27021下的bin目录下的配置文件mongo.config中添加如下内容:
# 数据保存
storage:
dbPath: F:\Apps\mongo-cluster\mongo-shard-27021\data
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# 日志保存到哪
systemLog:
destination: file
logAppend: true
path: F:\Apps\mongo-cluster\mongo-shard-27021\log\mongod.log
# 绑定的IP和端口号
net:
port: 27021
bindIp: 127.0.0.1
# 复制集名称
replication:
replSetName: 'test-shards'
# 复制集的作用:是一个分片服务器
sharding:
clusterRole: shardsvr
在mongo-config-27031下的bin目录下的配置文件mongo.config中添加如下内容:
# 数据保存
storage:
dbPath: F:\Apps\mongo-cluster\mongo-shard-27031\data
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# 日志保存到哪
systemLog:
destination: file
logAppend: true
path: F:\Apps\mongo-cluster\mongo-shard-27031\log\mongod.log
# 绑定的IP和端口号
net:
port: 27031
bindIp: 127.0.0.1
# 复制集名称
replication:
replSetName: 'demo-shards'
# 复制集的作用:是一个分片服务器
sharding:
clusterRole: shardsvr
执行安装服务命令:
mongod --config F:\Apps\mongo-cluster\mongo-shard-27021\bin\mongo.config --serviceName "MongoDB27021" --serviceDisplayName "MongoDB27021" --install
mongod --config F:\Apps\mongo-cluster\mongo-shard-27022\bin\mongo.config --serviceName "MongoDB27022" --serviceDisplayName "MongoDB27022" --install
mongod --config F:\Apps\mongo-cluster\mongo-shard-27023\bin\mongo.config --serviceName "MongoDB27023" --serviceDisplayName "MongoDB27023" --install
mongod --config F:\Apps\mongo-cluster\mongo-shard-27031\bin\mongo.config --serviceName "MongoDB27031" --serviceDisplayName "MongoDB27031" --install
mongod --config F:\Apps\mongo-cluster\mongo-shard-27032\bin\mongo.config --serviceName "MongoDB27032" --serviceDisplayName "MongoDB27032" --install
mongod --config F:\Apps\mongo-cluster\mongo-shard-27033\bin\mongo.config --serviceName "MongoDB27033" --serviceDisplayName "MongoDB27033" --install
启动服务
net start MongoDB27021
net start MongoDB27022
net start MongoDB27023
net start MongoDB27031
net start MongoDB27032
net start MongoDB27033
测试分片服务可用性:
mongo --host 127.0.0.1 --port 27021
mongo --host 127.0.0.1 --port 27022
mongo --host 127.0.0.1 --port 27023
mongo --host 127.0.0.1 --port 27031
mongo --host 127.0.0.1 --port 27032
mongo --host 127.0.0.1 --port 27033
或者下面这种方式:?
rs.initiate({
_id: 'test-shards',
members: [
{_id: 0, host: '127.0.0.1:27021'},
{_id: 1, host: '127.0.0.1:27022'},
{_id: 2, host: '127.0.0.1:27023'}]
})
rs.initiate({
_id: 'demo-shards',
members: [
{_id: 0, host: '127.0.0.1:27021'},
{_id: 1, host: '127.0.0.1:27022'},
{_id: 2, host: '127.0.0.1:27023'}]
})
4. 路由服务器部署
以管理员打开命令行窗口,切换到分片服务器目录下,新建data目录和log目录,在bin目录下创建配置文件mongo.config。
在mongo-config-27024下的bin目录下的配置文件mongo.config中添加如下内容:
# 日志保存
systemLog:
destination: file
logAppend: true
path: F:\Apps\mongo-cluster\mongo-router-27024\log\mongod.log
# 绑定的IP和端口号
net:
port: 27024
bindIp: 127.0.0.1
# 配置服务器地址
sharding:
configDB: mongodb-test/127.0.0.1:27018,127.0.0.1:27019,127.0.0.1:27020
注册路由服务
mongos --config F:\Apps\mongo-cluster\mongo-router-27024\bin\mongo.config --serviceName "MongoDB27024" --serviceDisplayName "MongoDB27024" --install
启动服务
net start MongoDB27024
测试服务
mongo --host 127.0.0.1 --port 27024
添加分片副本服务器:
sh.addShard( "test-shards/127.0.0.1:27021")
sh.addShard( "test-shards/127.0.0.1:27022")
sh.addShard( "test-shards/127.0.0.1:27023")
sh.addShard( "demo-shards/127.0.0.1:27031")
sh.addShard( "demo-shards/127.0.0.1:27032")
sh.addShard( "demo-shards/127.0.0.1:27033")
5. 配置数据库分片
给指定库demo开启分片:
sh.enableSharding("demo")
分片配置:
> sh.shardCollection("demo.user",{'age':1})
{
"collectionsharded" : "demo.user",
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1655968571, 5),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1655968570, 8)
}
> sh.shardCollection("demo.user",{'name':'hashed'})
{
"ok" : 0,
"errmsg" : "sharding already enabled for collection demo.user",
"code" : 23,
"codeName" : "AlreadyInitialized",
"$clusterTime" : {
"clusterTime" : Timestamp(1655968744, 10),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1655968744, 6)
}
查看状态:
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("62b409460f7f8d24eb00ed39")
}
shards:
{ "_id" : "demo-shards", "host" : "demo-shards/127.0.0.1:27031,127.0.0.1:27032,127.0.0.1:27033", "state" : 1, "topologyTime" : Timestamp(1655975907, 1) }
{ "_id" : "test-shards", "host" : "test-shards/127.0.0.1:27021,127.0.0.1:27022,127.0.0.1:27023", "state" : 1, "topologyTime" : Timestamp(1655968292, 2) }
active mongoses:
"5.0.9" : 1
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:
512 : Success
databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
config.system.sessions
shard key: { "_id" : 1 }
unique: false
balancing: true
chunks:
demo-shards 512
test-shards 512
too many chunks to print, use verbose if you want to force print
{ "_id" : "demo", "primary" : "test-shards", "partitioned" : true, "version" : { "uuid" : UUID("fd13df1e-7bb6-41f5-93aa-2d026a595a54"), "timestamp" : Timestamp(1655968493, 1), "lastMod" : 1 } }
demo.user
shard key: { "age" : 1 }
unique: false
balancing: true
chunks:
test-shards 1
{ "age" : { "$minKey" : 1 } } -->> { "age" : { "$maxKey" : 1 } } on : test-shards Timestamp(1, 0)
{ "_id" : "mongo-shard-demo", "primary" : "test-shards", "partitioned" : false, "version" : { "uuid" : UUID("d3dab826-da91-4634-888f-bb289de3de58"), "timestamp" : Timestamp(1655969219, 1), "lastMod" : 1 } }
{ "_id" : "test", "primary" : "demo-shards", "partitioned" : false, "version" : { "uuid" : UUID("e6d52d9e-a565-49d8-9bc7-b656d1068698"), "timestamp" : Timestamp(1655977282, 8), "lastMod" : 1 } }
可以看到已经有两个分片副本集demo-shards和test-shards。
6. 服务删除方法
如果服务添加错了,需要删除服务时,可以使用下面的命令。
#停止服务运行
net Stop ServiceName
#删除服务
sc delete ServiceName
7. 创建数据库并开启分片的过程
#数据库 启用 分片
sh.enableSharding("mongodbDemo")
# _id 字段进行哈希分片:
sh.shardCollection("mongodbDemo.order", {"_id": "hashed" })
#刷新路由
db.adminCommand("flushRouterConfig")
#让当前分片支持平衡
sh.enableBalancing("mongodbDemo.order")
#开启平衡
sh.startBalancer()
#查看详细分片信息
sh.status({"verbose":1})
#插入数据
use mongodbDemo
for (i = 1;i <= 100;i=i+1){
db.order.insert({'price': 1})
}
#查看该表分片数据信息
db.order.getShardDistribution()
综合上述可知,如果需要使用mongodb的分片功能,首先需要搭建分片集群,只有在集群环境下,才能使用分片功能,其次,还需要开启数据库的分片功能,并设置好分片键,默认情况下,数据库是不进行分片的。
|