高可用mongodb集羣的學習記錄(四mongodb分片集羣搭建)

無論oracle還是mysql數據庫都有分區的概念,即同一張表物理上不在同一臺機器上,有效緩解了表都集中存在一臺機器的壓力。當然,mongodb也有類似的機制,即是分片。具體理論知識大家可以參考網上文檔,我這裏只記錄下具體操作步驟

dc166a30dd685af75d234808f3f9cdf6.png

參考網絡上一個圖。我選用的是2個副本集+1個仲裁。實際上我這裏分片集羣需要3mongos3config server,數據分片3shard server,對應着還有3個副本,3個仲裁節點,總共需要15個實例。因爲我資源確實緊張,又不想影響實驗效果。冥思苦想了一陣,索性在一臺機器上實現得了,分給不同的實例以不同的端口就好了。

閒話少說,開始搞起!!!

1. 資源劃分(感覺這是最糾結的一步)

1543bdd4f7373a4801f4961af2d317dc.png 

2. mongdb的安裝

這個可以參考博主的前一篇章進行安裝高可用mongodb集羣的學習記錄(一安裝配置MongoDB)

3.創建所需要的目錄

#創建所需要的目錄
[root@DB mongodb]# mkdir -p mongos{1..3}/log
[root@DB mongodb]# mkdir -p config{1..3}/log
[root@DB mongodb]# mkdir -p config{1..3}/data
[root@DB mongodb]# mkdir -p shard{1..3}_{1..3}/log
[root@DB mongodb]# mkdir -p shard{1..3}_{1..3}/data


4. 啓動每一個配置服務器

#啓動配置服務器
mongod --configsvr --dbpath config1/data --port 21001 --logpath config1/log/config.log --fork
mongod --configsvr --dbpath config2/data --port 21002 --logpath config2/log/config.log --fork
mongod --configsvr --dbpath config3/data --port 21003 --logpath config3/log/config.log --fork


5.啓動mongos服務器

mongos --configdb 192.168.221.160:21001,192.168.221.160:21002,192.168.221.160:21003 --port 20001 --logpath mongos1/log/mongos.log --fork
mongos --configdb 192.168.221.160:21001,192.168.221.160:21002,192.168.221.160:21003 --port 20002 --logpath mongos2/log/mongos.log --fork
mongos --configdb 192.168.221.160:21001,192.168.221.160:21002,192.168.221.160:21003 --port 20003 --logpath mongos3/log/mongos.log --fork


6.啓動分片的副本集

 

  mongod --shardsvr --replSet shard1 --port 21101 --dbpath shard1_1/data --logpath shard1_1/log/shard.log --fork --nojournal --oplogSize 10 
  mongod --shardsvr --replSet shard1 --port 21201 --dbpath shard1_2/data --logpath shard1_2/log/shard.log --fork --nojournal --oplogSize 10 
  mongod --shardsvr --replSet shard1 --port 21301 --dbpath shard1_3/data --logpath shard1_3/log/shard.log --fork --nojournal --oplogSize 10 
  mongod --shardsvr --replSet shard2 --port 21102 --dbpath shard2_1/data --logpath shard2_1/log/shard.log --fork --nojournal --oplogSize 10 
  mongod --shardsvr --replSet shard2 --port 21202 --dbpath shard2_2/data --logpath shard2_2/log/shard.log --fork --nojournal --oplogSize 10 
  mongod --shardsvr --replSet shard2 --port 21302 --dbpath shard2_3/data --logpath shard2_3/log/shard.log --fork --nojournal --oplogSize 10 
  mongod --shardsvr --replSet shard3 --port 21103 --dbpath shard3_1/data --logpath shard3_1/log/shard.log --fork --nojournal --oplogSize 10 
  mongod --shardsvr --replSet shard3 --port 21203 --dbpath shard3_2/data --logpath shard3_2/log/shard.log --fork --nojournal --oplogSize 10 
  mongod --shardsvr --replSet shard3 --port 21303 --dbpath shard3_3/data --logpath shard3_3/log/shard.log --fork --nojournal --oplogSize 10


7.分別對每個分片配置副本集,任意登錄一個節點

#設置第一個分片副本集
[root@DB ~]# mongo 192.168.221.160:21101
MongoDB shell version: 3.0.6
connecting to: 192.168.221.160:21101/test
> use admin
switched to db admin
#定義副本集配置信息
> config = {_id:"shard1",members:[{_id:0,host:"192.168.221.160:21101"},{_id:1,host:"192.168.221.160:21201"},{_id:2,host:"192.168.221.160:21301",arbiterOnly:true}]}
{
"_id" : "shard1",
"members" : [
{
"_id" : 0,
"host" : "192.168.221.160:21101"
},
{
"_id" : 1,
"host" : "192.168.221.160:21201"
},
{
"_id" : 2,
"host" : "192.168.221.160:21301",
"arbiterOnly" : true
}
]
}
#初始化副本集信息
> rs.initiate(config)
{ "ok" : 1 }
shard1:OTHER> 
 
#設置第二個分片副本集
[root@DB ~]# mongo 192.168.221.160:21102
MongoDB shell version: 3.0.6
connecting to: 192.168.221.160:21102/test
> use admin
switched to db admin
> config = {_id:"shard2",members:[{_id:0,host:"192.168.221.160:21102"},{_id:1,host:"192.168.221.160:21202"},{_id:2,host:"192.168.221.160:21302",arbiterOnly:true}]}
{
"_id" : "shard2",
"members" : [
{
"_id" : 0,
"host" : "192.168.221.160:21102"
},
{
"_id" : 1,
"host" : "192.168.221.160:21202"
},
{
"_id" : 2,
"host" : "192.168.221.160:21302",
"arbiterOnly" : true
}
]
}
> rs.initiate(config)
{ "ok" : 1 }
shard2:OTHER> 
#設置第三個分片副本集
[root@DB ~]# mongo 192.168.221.160:21103
MongoDB shell version: 3.0.6
connecting to: 192.168.221.160:21103/test
> use admin
switched to db admin
> config = {_id:"shard3",members:[{_id:0,host:"192.168.221.160:21103"},{_id:1,host:"192.168.221.160:21203"},{_id:2,host:"192.168.221.160:21303",arbiterOnly:true}]}
{
"_id" : "shard3",
"members" : [
{
"_id" : 0,
"host" : "192.168.221.160:21103"
},
{
"_id" : 1,
"host" : "192.168.221.160:21203"
},
{
"_id" : 2,
"host" : "192.168.221.160:21303",
"arbiterOnly" : true
}
]
}
> rs.initiate(config)
{ "ok" : 1 }
shard3:OTHER>


8.登錄到每一個mongos,設置分片配置,讓分片生效

#串聯路由服務器與分片副本集1
[root@DB ~]# mongo 192.168.221.160:20001
MongoDB shell version: 3.0.6
connecting to: 192.168.221.160:20001/test
Server has startup warnings: 
2017-11-15T13:51:20.732+0800 I CONTROL  ** WARNING: You are running this process as the root user, which is not recommended.
2017-11-15T13:51:20.733+0800 I CONTROL  
mongos> use admin
switched to db admin
mongos> db.runCommand({addshard:"shard1/192.168.221.160:21101,192.168.221.160:21201,192.168.221.160:21301"})
{ "shardAdded" : "shard1", "ok" : 1 }
mongos> 
#串聯路由服務器與分片副本集2
[root@DB ~]# mongo 192.168.221.160:20002
MongoDB shell version: 3.0.6
connecting to: 192.168.221.160:20002/test
Server has startup warnings: 
2017-11-15T13:51:43.660+0800 I CONTROL  ** WARNING: You are running this process as the root user, which is not recommended.
2017-11-15T13:51:43.661+0800 I CONTROL  
mongos> use admin
switched to db admin
mongos> db.runCommand({addshard:"shard2/192.168.221.160:21102,192.168.221.160:21202,192.168.221.160:21302"})
{ "shardAdded" : "shard2", "ok" : 1 }
mongos> 
 
#串聯路由服務器與分片副本集3
[root@DB ~]# mongo 192.168.221.160:20003
MongoDB shell version: 3.0.6
connecting to: 192.168.221.160:20003/test
Server has startup warnings: 
2017-11-15T13:51:59.589+0800 I CONTROL  ** WARNING: You are running this process as the root user, which is not recommended.
2017-11-15T13:51:59.589+0800 I CONTROL  
mongos> use admin
switched to db admin
mongos> db.runCommand({addshard:"shard3/192.168.221.160:21103,192.168.221.160:21203,192.168.221.160:21303"})
{ "shardAdded" : "shard3", "ok" : 1 }
mongos>


9.查看分片服務器的配置信息:

mongos> db.runCommand({listshards:1})
{
"shards" : [
{
"_id" : "shard1",
"host" : "shard1/192.168.221.160:21101,192.168.221.160:21201"
},
{
"_id" : "shard2",
"host" : "shard2/192.168.221.160:21102,192.168.221.160:21202"
},
{
"_id" : "shard3",
"host" : "shard3/192.168.221.160:21103,192.168.221.160:21203"
}
],
"ok" : 1
}
mongos>


    仲裁節點不儲存數據,沒有在這裏顯示出來

10.連接mongos上,指定數據庫、指定集合讓分片生效

[root@DB ~]# mongo 192.168.221.160:20001
MongoDB shell version: 3.0.6
connecting to: 192.168.221.160:20001/test
mongos> db.runCommand({enablesharding:"testdb"})
{
"ok" : 0,
"errmsg" : "enableSharding may only be run against the admin database.",
"code" : 13
}
mongos> use admin
switched to db admin
mongos> db.runCommand({enablesharding:"testdb"})
{ "ok" : 1 }
mongos> db.runCommand({shardcollection:"testdb.table1",key:{id:1}})
{ "collectionsharded" : "testdb.table1", "ok" : 1 }
mongos>


設置testdbtable1 表需要分片,根據 id 自動分片到 shard1 shard2shard3 上面去。要這樣設置是因爲不是所有mongodb 的數據庫和表都需要分片

 

11.測試分片片配置效果

[root@DB ~]# mongo 192.168.221.160:20001
MongoDB shell version: 3.0.6
connecting to: 192.168.221.160:20001/test
Server has startup warnings: 
2017-11-15T13:51:20.732+0800 I CONTROL  ** WARNING: You are running this process as the root user, which is not recommended.
2017-11-15T13:51:20.733+0800 I CONTROL  
mongos> use testdb;
switched to db testdb
mongos> for (var i = 1;i<=10000;i++) db.table1.save({id:i,"test1":"item1"})
WriteResult({ "nInserted" : 1 })
mongos> db.table1.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" : "testdb.table1",
"count" : 10000,
"numExtents" : 7,
"size" : 1120000,
"storageSize" : 2809856,
"totalIndexSize" : 654080,
"indexSizes" : {
"_id_" : 351568,
"id_1" : 302512
},
"avgObjSize" : 112,
"nindexes" : 2,
"nchunks" : 3,
"shards" : {
"shard1" : {
"ns" : "testdb.table1",
"count" : 9991,
"size" : 1118992,
...
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(0, 0),
"electionId" : ObjectId("5a0bd9cf86a6b76e11b5d820")
}
},
"shard2" : {
"ns" : "testdb.table1",
"count" : 1,
"size" : 112,
...
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(0, 0),
"electionId" : ObjectId("5a0bdadd74c5945a33a4ae46")
}
},
"shard3" : {
"ns" : "testdb.table1",
"count" : 8,
"size" : 896,
...
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(0, 0),
"electionId" : ObjectId("5a0bdb6f1c75115f41ff92f9")
}
}
},
"ok" : 1
}
mongos>


可以看到testdb.table1數據已經被分到三個分片,各自分片數量爲 shard1 “count” : 9991shard2 “count” : 1shard3 “count” : 8我也不知道爲啥分配的不均勻。納悶ing

好了,實驗到此結束,關於mongodb的學習會繼續進行...

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章