MongoDB学习札记 第十篇 分片集群搭建
MongoDB学习札记 第十篇 分片 测试架构实战
##实验环境准备:
configSerer
192.168.236.131:27000
mongos
192.168.236.131:28000
shards
192.168.236.131:29001
192.168.236.131:29002
192.168.236.131:29003
第一步: 创建分片实验需要的目录
root@ubuntu:~# mkdir -p ~/mongoData/shard/s1
root@ubuntu:~# mkdir -p ~/mongoData/shard/s2
root@ubuntu:~# mkdir -p ~/mongoData/shard/s3
root@ubuntu:~# mkdir -p ~/mongoData/shard/log
root@ubuntu:~# mkdir -p ~/mongoData/shard/config
第二步: 启动configServer
root@ubuntu:~# mongod --configsvr --dbpath ~/mongoData/shard/config/ --fork --logpath ~/mongoData/shard/log/configsvr.log --logappend --port 27000
第三步: 启动mongos
root@ubuntu:~# mongos --configdb 192.168.236.131:27000 --port 28000 --fork --logpath ~/mongoData/shard/log/mongs.log
第四步: 启动所有的shard分片
root@ubuntu:~/mongoData# mongod --dbpath ~/mongoData/shard/s1/ --port 29001 --fork --logpath ~/mongoData/shard/log/s1.log --shardsvr --logappend
root@ubuntu:~/mongoData# mongod --dbpath ~/mongoData/shard/s2/ --port 29002 --fork --logpath ~/mongoData/shard/log/s2.log --shardsvr --logappend
root@ubuntu:~/mongoData# mongod --dbpath ~/mongoData/shard/s3/ --port 29003 --fork --logpath ~/mongoData/shard/log/s3.log --shardsvr --logappend
第五步: 将shard添加到mongos中 并配置
root@ubuntu:~/mongoData# mongo --port 28000
MongoDB shell version: 3.0.3
connecting to: 127.0.0.1:28000/test
... ...
mongos> use admin
switched to db admin
mongos> sh.addShard("192.168.236.131:29001")
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> sh.addShard("192.168.236.131:29002")
{ "shardAdded" : "shard0001", "ok" : 1 }
mongos> sh.addShard("192.168.236.131:29003")
{ "shardAdded" : "shard0002", "ok" : 1 }
mongos>
使用下面两个命令来配置需要分片的数据库及集合以及对应的片键
sh.enableSharding(“
sh.shardCollection(“
mongos> sh.enableSharding("test")
{ "ok" : 1 }
mongos> sh.shardCollection("test.users",{"username":1,"_id":1})
{ "collectionsharded" : "test.users", "ok" : 1 }
mongos>
如果是添加副本集作为分片怎么处理?
addShard
The hostname and port of the mongod instance to be added as a shard. To add a replica set as a shard, specify the name of the replica set and the hostname and port of a member of the replica set.
上面这段话引用自官网,也就是说,我们只需要指定副本集的名称然后再指定其中一台机器即可。
比如:
sh.addShard(“replicaSet0/<ont host of the replica set>:<port>”);
验证分片集群部署情况
先往mongos插入100条数据,然后通过 db.users.stats() 查看集合的状态,发现集合被切分到三个分片中了,虽然第一个分片数据量比较多,其他两个分片数据量相对较少 (这个和片键 sharding key 的设置有关,我没有详细看官网关于shard key设置的文章,所以这里的片键设置比较简单,随意。)
mongos> for(var i=0; i<100; i++) {
... db.users.insert({"username":"" + i,age:i*2 , addr:"ardr"+i})
... }
WriteResult({ "nInserted" : 1 })
mongos> db.users.stats()
{
"sharded" : true,
"paddingFactorNote" : "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only.",
"userFlags" : 1,
"capped" : false,
"ns" : "test.users",
"count" : 100,
"numExtents" : 4,
"size" : 11200,
"storageSize" : 57344,
"totalIndexSize" : 49056,
"indexSizes" : {
"_id_" : 24528,
"username_1__id_1" : 24528
},
"avgObjSize" : 112,
"nindexes" : 2,
"nchunks" : 3,
"shards" : {
"shard0000" : {
"ns" : "test.users",
"count" : 88,
"size" : 9856,
"avgObjSize" : 112,
"numExtents" : 2,
"storageSize" : 40960,
"lastExtentSize" : 32768,
"paddingFactor" : 1,
"paddingFactorNote" : "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only.",
"userFlags" : 1,
"capped" : false,
"nindexes" : 2,
"totalIndexSize" : 16352,
"indexSizes" : {
"_id_" : 8176,
"username_1__id_1" : 8176
},
"ok" : 1
},
"shard0001" : {
"ns" : "test.users",
"count" : 11,
"size" : 1232,
"avgObjSize" : 112,
"numExtents" : 1,
"storageSize" : 8192,
"lastExtentSize" : 8192,
"paddingFactor" : 1,
"paddingFactorNote" : "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only.",
"userFlags" : 1,
"capped" : false,
"nindexes" : 2,
"totalIndexSize" : 16352,
"indexSizes" : {
"_id_" : 8176,
"username_1__id_1" : 8176
},
"ok" : 1
},
"shard0002" : {
"ns" : "test.users",
"count" : 1,
"size" : 112,
"avgObjSize" : 112,
"numExtents" : 1,
"storageSize" : 8192,
"lastExtentSize" : 8192,
"paddingFactor" : 1,
"paddingFactorNote" : "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only.",
"userFlags" : 1,
"capped" : false,
"nindexes" : 2,
"totalIndexSize" : 16352,
"indexSizes" : {
"_id_" : 8176,
"username_1__id_1" : 8176
},
"ok" : 1
}
},
"ok" : 1
}
这时候如果用mongo客户端去连接 29001 , 29002, 29003端口,会发现只有集合的部分数据是可见的,这也证明了我们实验成功了
root@ubuntu:~/mongoData# mongo --port 29001
MongoDB shell version: 3.0.3
connecting to: 127.0.0.1:29001/test
... ...
> db.users.find().count()
88
> exit
bye
root@ubuntu:~/mongoData# mongo --port 29002
MongoDB shell version: 3.0.3
connecting to: 127.0.0.1:29002/test
... ...
> db.users.find().count()
11
> exit
bye
root@ubuntu:~/mongoData# mongo --port 29003
MongoDB shell version: 3.0.3
connecting to: 127.0.0.1:29003/test
... ...
> db.users.find().count()
1
> exit
bye
root@ubuntu:~/mongoData#
整个分片的实验基本上已经验证成功了。
如果某个集合没有进行分片,数据会存放在primary shard里面。
mongos> db.sites.insert({"site":"webinglin.github.io","author":"linwenbin"})
WriteResult({ "nInserted" : 1 })
... ...
mongos> db.sites.insert({"site":"webinglin.github.io","author":"linwenbin"})
WriteResult({ "nInserted" : 1 })
mongos> db.sites.stats()
{
"sharded" : false,
"primary" : "shard0000",
"ns" : "test.sites",
"count" : 7,
"size" : 784,
"avgObjSize" : 112,
"numExtents" : 1,
"storageSize" : 8192,
"lastExtentSize" : 8192,
"paddingFactor" : 1,
"paddingFactorNote" : "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only.",
"userFlags" : 1,
"capped" : false,
"nindexes" : 1,
"totalIndexSize" : 8176,
"indexSizes" : {
"_id_" : 8176
},
"ok" : 1
}
mongos>
这篇文章介绍了简单的搭建分片集群的步骤。更多关于怎么选择片键(Shard Key),参考这里。
转载请注明出处! 原文地址: http://webinglin.github.io
Reference:
http://docs.mongodb.org/manual/core/sharded-cluster-architectures-test/