【MongoDB】分片(sharding)+副本集(replSet)叢集搭建

神諭丶發表於2016-11-25
構建一個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伺服器。






參考文件:







來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29773961/viewspace-2129100/,如需轉載,請註明出處,否則將追究法律責任。

相關文章