阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

MongoDB 分片(sharding)+副本集(replSet)集群搭建

138次阅读
没有评论

共计 4979 个字符,预计需要花费 13 分钟才能阅读完成。

构建一个 sharded cluster 需要至少三个组件:

〇 shard: 每一个 shard 包括了切片数据的子集,也可以被部署为“副本集”
〇 mongos: 作为一个“查询路由”,在客户端和 shard cluster 之间作为一个中间接口,类似于 MySQL 的一些中间 proxy。
可以通过 mongo shell 或者 mongodb driver 直连 mongos
〇 config server: 一个存储了 sharded cluster 的元数据和配置信息的 server,同样也可以被部署为“副本集”(mongodb 3.2)


此处用了四台服务器实践,资源分配如下:


config server(186):

  1. mkdir p /data/config/data
  2. mkdir p /data/config/log
  3. mongod dbpath=/data/config/data logpath=/data/config/log/config.log port=30000 fork configsvr

mongos(185):

  1. mkdir p /data/mongos/log
  2. mongos logpath=/data/mongos/log/mongos.log port=27017 fork configdb=192.168.1.186:30000

此处先给两个 shard 节点做副本集,此处将通过端口区分不同实例:

shard1(187):

  1. mkdir p /data/shard1/data/set1
  2. mkdir p /data/shard1/data/set2
  3. mkdir p /data/shard1/data/set3
  4. mkdir p /data/shard1/logs
  5. mongod dbpath=/data/shard1/data/set1 logpath=/data/shard1/logs/set1.log port=27017 fork shardsvr replSet=shard1
  6. mongod dbpath=/data/shard1/data/set2 logpath=/data/shard1/logs/set2.log port=27018 fork shardsvr replSet=shard1
  7. mongod dbpath=/data/shard1/data/set3 logpath=/data/shard1/logs/set3.log port=27019 fork shardsvr replSet=shard1

通过 mongo shell 进入任意一个实例,此处选择的是 27017 实例,配置副本集并初始化

  1. # mongo 127.0.0.1:27017/admin

以下为 mongo shell 操作:

  1. > cnf = {_id:“shard1”, members:[
  2.         {_id:1, host:“192.168.1.187:27017”},
  3.         {_id:2, host:“192.168.1.187:27018”},
  4.         {_id:3, host:“192.168.1.187:27019”},
  5.         ]
  6.     }
  7. > rs.initiate(cnf);
  8. {“ok” : 1 }

可以检查一下副本集状态

  1. > rs.status()

再给 shard2 做三个副本集
shard2(188):

  1. mkdir p /data/shard2/data/set1
  2. mkdir p /data/shard2/data/set2
  3. mkdir p /data/shard2/data/set3
  4. mkdir p /data/shard2/logs
  5. mongod dbpath=/data/shard2/data/set1 logpath=/data/shard2/logs/set1.log port=27017 fork shardsvr replSet=shard2
  6. mongod dbpath=/data/shard2/data/set2 logpath=/data/shard2/logs/set2.log port=27018 fork shardsvr replSet=shard2
  7. mongod dbpath=/data/shard2/data/set3 logpath=/data/shard2/logs/set3.log port=27019 fork shardsvr replSet=shard2

和 shard1 一样,通过 mongo shell 进入任意一个实例,此处选择的是 27017 实例,配置副本集并初始化

  1. > cnf = {_id:“shard2”, members:[
  2.         {_id:1, host:“192.168.1.188:27017”},
  3.         {_id:2, host:“192.168.1.188:27018”},
  4.         {_id:3, host:“192.168.1.188:27019”},
  5.         ]
  6.     }
  7. > rs.initiate(cnf);
  8. {“ok” : 1 }

最后在 mongos(185) 的机器上添加 shard 节点

  1. # mongo 127.0.0.1:27017/admin

进入 mongo shell,通过 addshard 来加入 shard 节点,多个副本集用逗号分隔:

  1. mongos> db.runCommand( {addshard : “shard1/192.168.1.187:27017,192.168.1.187:27018,192.168.1.187:27019”});
  2. {“shardAdded” : “shard1”, “ok” : 1 }
  3. mongos> db.runCommand( {addshard : “shard2/192.168.1.188:27017,192.168.1.188:27018,192.168.1.188:27019”});
  4. {“shardAdded” : “shard2”, “ok” : 1 }
  5. mongos> db.runCommand( {listshards : 1 } );
  6. {
  7.         “shards” : [
  8.                 {
  9.                         “_id” : “shard1”,
  10.                         “host” : “shard1/192.168.1.187:27017,192.168.1.187:27018,192.168.1.187:27019”
  11.                 },
  12.                 {
  13.                         “_id” : “shard2”,
  14.                         “host” : “shard2/192.168.1.188:27017,192.168.1.188:27018,192.168.1.188:27019”
  15.                 }
  16.         ],
  17.         “ok” : 1
  18. }

此处对 sano1y 库的 testtb 使用 hash 策略:

  1. mongos> use admin
  2. switched to db admin
  3. mongos> db.runCommand({“enablesharding”:“sano1y”})
  4. {“ok” : 1 }
  5. mongos> db.runCommand({“shardcollection”:“sano1y.testtb”,“key”:{“_id”:“hashed”}})
  6. {“collectionsharded” : “sano1y.testtb”, “ok” : 1 }

目前为止,已经对 sano1y 库的 testtb 集合进行了 shard 配置。

测试:

  1. mongos> use sano1y
  2. switched to db sano1y
  3. mongos> for(i=0;i<100000;i++) {db.testtb.insert({“id”:i,“name”:“test_hash”});}

稍等片刻,等待插入完毕:

  1. WriteResult({ “nInserted” : 1 })

进入 shard1(187)的 PRIMARY 实例检查

  1. shard1:PRIMARY> use sano1y
  2. switched to db sano1y
  3. shard1:PRIMARY> db.testtb.find().count()
  4. 49983
  5. shard1:PRIMARY> db.testtb.find()
  6. {“_id” : ObjectId(“5837ef1dea1fd54fb38d845c”), “id” : 0, “name” : “test_hash” }
  7. {“_id” : ObjectId(“5837ef1dea1fd54fb38d845d”), “id” : 1, “name” : “test_hash” }
  8. {“_id” : ObjectId(“5837ef1dea1fd54fb38d845e”), “id” : 2, “name” : “test_hash” }
  9. {“_id” : ObjectId(“5837ef1dea1fd54fb38d8460”), “id” : 4, “name” : “test_hash” }
  10. {“_id” : ObjectId(“5837ef1dea1fd54fb38d8461”), “id” : 5, “name” : “test_hash” }
  11. {“_id” : ObjectId(“5837ef1dea1fd54fb38d8465”), “id” : 9, “name” : “test_hash” }
  12. {“_id” : ObjectId(“5837ef1dea1fd54fb38d8468”), “id” : 12, “name” : “test_hash” }
  13. {“_id” : ObjectId(“5837ef1dea1fd54fb38d846f”), “id” : 19, “name” : “test_hash” }
  14. {“_id” : ObjectId(“5837ef1dea1fd54fb38d8471”), “id” : 21, “name” : “test_hash” }
  15. {“_id” : ObjectId(“5837ef1dea1fd54fb38d8475”), “id” : 25, “name” : “test_hash” }
  16. {“_id” : ObjectId(“5837ef1dea1fd54fb38d8476”), “id” : 26, “name” : “test_hash” }
  17. {“_id” : ObjectId(“5837ef1dea1fd54fb38d8479”), “id” : 29, “name” : “test_hash” }
  18. {“_id” : ObjectId(“5837ef1dea1fd54fb38d847d”), “id” : 33, “name” : “test_hash” }
  19. {“_id” : ObjectId(“5837ef1dea1fd54fb38d847e”), “id” : 34, “name” : “test_hash” }
  20. {“_id” : ObjectId(“5837ef1dea1fd54fb38d8480”), “id” : 36, “name” : “test_hash” }
  21. {“_id” : ObjectId(“5837ef1dea1fd54fb38d8481”), “id” : 37, “name” : “test_hash” }
  22. {“_id” : ObjectId(“5837ef1dea1fd54fb38d8483”), “id” : 39, “name” : “test_hash” }
  23. {“_id” : ObjectId(“5837ef1dea1fd54fb38d8486”), “id” : 42, “name” : “test_hash” }
  24. {“_id” : ObjectId(“5837ef1dea1fd54fb38d848b”), “id” : 47, “name” : “test_hash” }
  25. {“_id” : ObjectId(“5837ef1dea1fd54fb38d848d”), “id” : 49, “name” : “test_hash” }

另外如果到 shard2 可以看到 shard1 这些不连续的 id。
可发现 shard1 和 2 中的 document 数量,还是比较均匀的。

shard1: 49983
shard2: 50017

至此,分片集群搭建基本完成。
当然,config server 现在存在着单点的问题,同样也可以将 config server 配置成一组 replSet,其本质是 mongod 服务器。

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-03/142382.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计4979字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中