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

构建一个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节点,多个副本集用逗号分隔:

扫描二维码关注公众号,回复: 1959464 查看本文章
  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服务器。

猜你喜欢

转载自www.linuxidc.com/Linux/2017-03/142382.htm