环境:
角色 IP
配置服务器 192.168.56.101
路由服务器 192.168.56.102
片服务器01 192.168.56.103
片服务器02 192.168.56.104
mongodb:2.4.6
1.创建目录
配置服务器: mkdir -p /db/config_server/
路由服务器: mkdir -p /db/route_server/
片服务器01: mkdir -p /db/shard/
片服务器02: mkdir -p /db/shard/
2.解压缩mongodb安装包
每台机器上执行
tar -zxvf mongodb-linux-x86_64-2.6.5.tgz
3.将mongodb目录转移到每个机器的相应目录
配置服务器: mv mongodb-linux-x86_64-2.6.5 /db/config_server/mongodb
路由服务器: mv mongodb-linux-x86_64-2.6.5 /db/route_server/mongodb
片服务器01: mv mongodb-linux-x86_64-2.6.5 /db/shard/mongodb
片服务器02: mv mongodb-linux-x86_64-2.6.5 /db/shard/mongodb
3.启动配置服务器:
配置参数文件为config_server.cnf,内容如下:
port = 27017
fork = true
dbpath = /db/config_server/data
logpath = /db/config_server/log/logs
logappend = true
configsvr = true
directoryperdb = true
启动
#./mongod -f /db/config_server/conf/config_server.cnf
启动路由服务器:
配置参数文件为route_server.cnf,内容如下:
port = 27017
configdb = 192.168.56.101:27017
logpath = /db/route_server/log/logs
fork = true
启动
mongos -f /db/route_server/conf/route_server.cnf
启动片服务器
每个片服务器的操作一样
配置参数文件为shard.cnf,内容如下:
port = 27017
fork = true
dbpath = /db/shard/data
logpath = /db/shard/log/logs
logappend = true
shardsvr = true
启动
./mongod -f /db/shard/conf/shard.cnf
4.在路由服务器上执行,添加分片服务器
./mongo
mongos>use admin
mongos>db.runCommand({"addshard":"192.168.56.103:27017",allowLocal:true})
mongos>db.runCommand({"addshard":"192.168.56.104:27017",allowLocal:true})
查看分片情况
mongos>use admin
mongos> db.runCommand({listshards:1});
{
"shards" : [
{
"_id" : "shard0000",
"host" : "192.168.56.103:27017"
},
{
"_id" : "shard0001",
"host" : "192.168.56.104:27017"
}
],
"ok" : 1
}
5.启用分片功能
在路由服务器上执行
是数据库hxl启用分片功能
mongos>db.runCommand({"enablesharding":"hxl"});
为集合person设置片键,这里使用id做hash分片
mongos>use admin
mongos>db.runCommand({"shardcollection":"hxl.person","key":{"_id":"hashed"}})
{ "collectionsharded" : "hxl.person", "ok" : 1 }
写入数据
mongos>use hxl
mongos>for (var i=0;i<100000;i++){db.person.insert({"name":"hxl"+i,"age":i})}
mongos> db.person.count();
100000
查看分片结果
mongos> db.printShardingStatus();
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 4,
"minCompatibleVersion" : 4,
"currentVersion" : 5,
"clusterId" : ObjectId("562843cbdc7320ef0c087f41")
}
shards:
{ "_id" : "shard0000", "host" : "192.168.56.103:27017" }
{ "_id" : "shard0001", "host" : "192.168.56.104:27017" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "hxl", "partitioned" : true, "primary" : "shard0000" }
hxl.person
shard key: { "_id" : "hashed" }
chunks:
shard0000 2
shard0001 2
{ "_id" : { "$minKey" : 1 } } -->> { "_id" : NumberLong("-4611686018427387902") } on : shard0000 Timestamp(2, 2)
{ "_id" : NumberLong("-4611686018427387902") } -->> { "_id" : NumberLong(0) } on : shard0000 Timestamp(2, 3)
{ "_id" : NumberLong(0) } -->> { "_id" : NumberLong("4611686018427387902") } on : shard0001 Timestamp(2, 4)
{ "_id" : NumberLong("4611686018427387902") } -->> { "_id" : { "$maxKey" : 1 } } on : shard0001 Timestamp(2, 5)
{ "_id" : "test", "partitioned" : false, "primary" : "shard0000" }
查看集合的分片记录情况
mongos> use hxl;
mongos> db.person.stats()
{
"sharded" : true,
"systemFlags" : 1,
"userFlags" : 1,
"ns" : "hxl.person",
"count" : 100000,
"numExtents" : 12,
"size" : 11200000,
"storageSize" : 22364160,
"totalIndexSize" : 7857136,
"indexSizes" : {
"_id_" : 3254048,
"_id_hashed" : 4603088
},
"avgObjSize" : 112,
"nindexes" : 2,
"nchunks" : 4,
"shards" : {
"shard0000" : {
"ns" : "hxl.person",
"count" : 50046,
"size" : 5605152,
"avgObjSize" : 112,
"storageSize" : 11182080,
"numExtents" : 6,
"nindexes" : 2,
"lastExtentSize" : 8388608,
"paddingFactor" : 1,
"systemFlags" : 1,
"userFlags" : 1,
"totalIndexSize" : 4047120,
"indexSizes" : {
"_id_" : 1627024,
"_id_hashed" : 2420096
},
"ok" : 1
},
"shard0001" : {
"ns" : "hxl.person",
"count" : 49954,
"size" : 5594848,
"avgObjSize" : 112,
"storageSize" : 11182080,
"numExtents" : 6,
"nindexes" : 2,
"lastExtentSize" : 8388608,
"paddingFactor" : 1,
"systemFlags" : 1,
"userFlags" : 1,
"totalIndexSize" : 3810016,
"indexSizes" : {
"_id_" : 1627024,
"_id_hashed" : 2182992
},
"ok" : 1
}
},
"ok" : 1
}
从以上输出可以看出,总共100000条记录,其中50046分配到分片shard0000中,49954分布到shard0001中.
-- The End --
阅读(3994) | 评论(1) | 转发(0) |