首页 > 数据库 > NoSQL > shared Server 分片
这是一种将海量的数据水平扩展的数据库集群系统,数据分表存储在 sharding 的各个节点
上,使用者通过简单的配置就可以很方便地构建一个分布式 MongoDB 集群。
MongoDB 的数据分块称为 chunk。每个 chunk 都是 Collection 中一段连续的数据记录,通
常最大尺寸是 200MB,超出则生成新的数据块。
要构建一个 MongoDB Sharding Cluster,需要三种角色:
Shard Server
即存储实际数据的分片,每个 Shard 可以是一个 mongod 实例,也可以是一组 mongod 实例
构成的 Replica Set。为了实现每个 Shard 内部的 auto-failover, MongoDB 官方建议每个 Shard
为一组 Replica Set。
Config Server
为了将一个特定的 collection 存储在多个 shard 中,需要为该 collection 指定一个 shard key,
例如{age: 1} , shard key 可以决定该条记录属于哪个 chunk。 Config Servers 就是用来存储:
所有 shard 节点的配置信息、每个 chunk 的 shard key 范围、 chunk 在各 shard 的分布情况、
该集群中所有 DB 和 collection 的 sharding 配置信息。
Route Process
这是一个前端路由,客户端由此接入,然后询问 Config Servers 需要到哪个 Shard 上查询或
保存记录,再连接相应的 Shard 进行操作,最后将结果返回给客户端。客户端只需要将原本
发给 mongod 的查询或更新请求原封不动地发给 Routing Process,而不必关心所操作的记录
存储在哪个 Shard 上。
下面我们在同一台物理机器上构建一个简单的 Sharding Cluster:
Shard Server 1: 20000
Shard Server 2: 20001
Config Server : 30000
Route Process: 40000
1 创建shared Server
mkdir
-
p /data/shard/s0
-
-
创建数据目录
mkdir
-
p /data/shard/s1
mkdir
-
p /data/shard/log
-
-
创建日志目录
/Apps/mongo/bin/mongod
-
-
shardsvr
-
-
port 20000
-
-
dbpath /data/shard/s0
-
-
fork
-
-
logpath
/data/shard/log/s0
.
log
-
-
directoryperdb
-
-
启动 Shard Server 实例 1 ---一个分片
/Apps/mongo/bin/mongod
-
-
shardsvr
-
-
port 20001
-
-
dbpath /data/shard/s1
-
-
fork
-
-
logpath
/data/shard/log/s1 . log - - directoryperdb - - 启动 Shard Server 实例 2 --一个分片
2 启动 Config Server
mkdir
-
p /data/shard/config
-
-
创建数据目录
/Apps/mongo/bin/mongod
-
-
configsvr
-
-
port 30000
-
-
dbpath /data/shard/config
-
-
fork
-
-
logpath
/data/shard/log/config . log - - directoryperdb - - 启动 Config Server 实例
3 启动 Route Process
点击( 此处 )折叠或打开
/Apps/mongo/bin/mongos
-
-
port 40000
-
-
configdb
localhost
:
30000
-
-
fork
-
-
logpath
/data/shard/log/route . log - - chunkSize 1 - - 启动 Route Server 实例,和下面一样 最好写i p
4配置 Sharding
接下来,我们使用 MongoDB Shell 登录到 mongos,添加 Shard 节点
[
root@localhost ~
]
#
/Apps/mongo/bin/mongo admin
-
-
port 40000
-
-
此操作需要连接 admin 库
MongoDB shell version
:
1
.
8
.
1
connecting to
:
127
.
.
.
1
:
40000/admin
>
db
.
runCommand
(
{
addshard
:
"localhost:20000"
}
)
-
-
添加 Shard Server,最好写ip
{
"shardAdded"
:
"shard0000"
,
"ok"
:
1 }
>
db
.
runCommand
(
{
addshard
:
"localhost:20001"
}
)
{
"shardAdded"
:
"shard0001"
,
"ok"
:
1 }
> db . runCommand ( { enablesharding : "test" } )
[这里只是标识这个数据库可以启用分片,但实际上新建的文档并没有进行分片]
{
"ok"
:
1 }
>
db
.
runCommand
(
{
shardcollection
:
"test.users"
,
key
:
{ _id
:
1 }}
)
-
-
设置分片的集合名称,且必
须指定 Shard Key,系统会自动创建索引
{
"collectionsharded"
:
"test.users"
,
"ok"
:
1 }
或者命令:
sh.shardCollection("welike_mongo.userRelationMongo",{_id:1})
5验证 Sharding 正常工作
我们已经对
test
.
users 表进行了分片的设置,下面我们们插入一些数据看一下结果
>
use
test
switched to db
test
>
for
(
var i
=
1
;
i
<
=
500000
;
i
+
+
)
db
.
users
.
insert
(
{
age
:
i
,
name
:
"wangwenlong"
,
addr
:
"Beijing"
,
country
:
"China"
}
)
>
db
.
users
.
stats
(
)
{
"sharded"
:
true
,
-
-
说明此表已被 shard
"ns"
:
"test.users"
,
"count"
:
500000
,
"size"
:
48000000
,
"avgObjSize"
:
96
,
"storageSize"
:
66655232
,
"nindexes"
:
1
,
"nchunks"
:
43
,
"shards"
:
{
"shard0000"
:
{
-
-
在此分片实例上约有 24
.
5M 数据
"ns"
:
"test.users"
,
"count"
:
254889
,
"size"
:
24469344
,
"avgObjSize"
:
96
,
79 / 91
"storageSize"
:
33327616
,
"numExtents"
:
8
,
"nindexes"
:
1
,
"lastExtentSize"
:
12079360
,
"paddingFactor"
:
1
,
"flags"
:
1
,
"totalIndexSize"
:
11468800
,
"indexSizes"
:
{
"_id_"
:
11468800
}
,
"ok"
:
1
}
,
"shard0001"
:
{
-
-
在此分片实例上约有 23
.
5M 数据
"ns"
:
"test.users"
,
"count"
:
245111
,
"size"
:
23530656
,
"avgObjSize"
:
96
,
"storageSize"
:
33327616
,
"numExtents"
:
8
,
"nindexes"
:
1
,
"lastExtentSize"
:
12079360
,
"paddingFactor"
:
1
,
"flags"
:
1
,
"totalIndexSize"
:
10649600
,
"indexSizes"
:
{
"_id_"
:
10649600
}
,
"ok"
:
1
}
}
,
"ok"
:
1
}
>
7列出所有的 Shard Server
>
db
.
runCommand
(
{
listshards
:
1 }
)
-
-
列出所有的 Shard Server
{
"shards"
:
[
{
"_id"
:
"shard0000"
,
"host"
:
"localhost:20000"
}
,
{
"_id"
:
"shard0001"
,
"host"
:
"localhost:20001"
}
]
,
"ok"
:
1
}
8 查看 Sharding 信息
>
printShardingStatus
(
)
等价 sh.status()
-
-
查看 所有表的Sharding 信息
-
-
-
Sharding Status
-
-
-
sharding version
:
{
"_id"
:
1
,
"version"
:
3 }
shards
:
{
"_id"
:
"shard0000"
,
"host"
:
"localhost:20000"
}
{
"_id"
:
"shard0001"
,
"host"
:
"localhost:20001"
}
databases
:
{
"_id"
:
"admin"
,
"partitioned"
:
false
,
"primary"
:
"config"
}
{
"_id"
:
"test"
,
"partitioned"
:
true
,
"primary"
:
"shard0000"
}
test
.
users chunks
:
shard0000 1 ----test.users在shard0000 上有一个chunks块
{
"_id"
:
{ $minKey
:
1 } }
-
-
>
>
{
"_id"
:
{ $maxKey
:
1 } } on
:
shard0000 {
"t"
:
1000
,
"i"
:
0 }
>
判断是否是 Sharding
点击( 此处 )折叠或打开
>
db
.
runCommand
(
{
isdbgrid
:
1 }
)
{ "isdbgrid" : 1 , "hostname" : "localhost" , "ok" : 1 }
对现有的表进行 Sharding
刚才我们是对表 test.users 进行分片了,下面我们将对库中现有的未分片的表 test.users_2 进
行分片处理
表最初状态如下,可以看出他没有被分片过:
>
db.users_2.stats() 或者 db.hotFeedMongo.stats().sharded
{
"ns" : "test.users_2",
"sharded" : false,
"primary" : "shard0000",
"ns" : "test.users_2",
"count" : 500000,
"size" : 48000016,
"avgObjSize" : 96.000032,
"storageSize" : 61875968,
"numExtents" : 11,
"nindexes" : 1,
"lastExtentSize" : 15001856,
"paddingFactor" : 1,
"flags" : 1,
"totalIndexSize" : 20807680,
"indexSizes" : {
"_id_" : 20807680
},
"ok" : 1
}
对其进行分片处理:
>
use admin
switched to db admin
>
db.runCommand({ shardcollection: "test.users_2", key: { _id:1 }})
{ "collectionsharded" : "test.users_2", "ok" : 1 }
再次查看分片后的表的状态,可以看到它已经被我们分片了
>
use test
switched to db test
>
db.users_2.stats()
{
"sharded" : true,
"ns" : "test.users_2",
"count" : 505462,
……
"shards" : {
"shard0000" : {
"ns" : "test.users_2",
……
"ok" : 1
},
"shard0001" : {
"ns" : "test.users_2",
……
"ok" : 1
}
},
"ok" : 1
}
>
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29096438/viewspace-1800199/ ,如需转载,请注明出处,否则将追究法律责任。