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

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

dc166a30dd685af75d234808f3f9cdf6.png

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

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

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

1543bdd4f7373a4801f4961af2d317dc.png 

2. mongdb的安裝

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

3.創建所需要的目錄

1
2
3
4
5
6
#創建所需要的目錄
[[email protected] mongodb] # mkdir -p mongos{1..3}/log
[[email protected] mongodb] # mkdir -p config{1..3}/log
[[email protected] mongodb] # mkdir -p config{1..3}/data
[[email protected] mongodb] # mkdir -p shard{1..3}_{1..3}/log
[[email protected] mongodb] # mkdir -p shard{1..3}_{1..3}/data


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

1
2
3
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服務器

1
2
3
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.啓動分片的副本集

 

1
2
3
4
5
6
7
8
9
   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.分別對每個分片配置副本集,任意登錄一個節點

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#設置第一個分片副本集
[[email protected] ~] # 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> 
  
#設置第二個分片副本集
[[email protected] ~] # 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> 
#設置第三個分片副本集
[[email protected] ~] # 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#串聯路由服務器與分片副本集1
[[email protected] ~] # 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
[[email protected] ~] # 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
[[email protected] ~] # 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.查看分片服務器的配置信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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:212'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;font-size:1em;background:none;border:0px;line-height:1.1em;vertical-align:baseline;color:#0000FF;">"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上,指定數據庫、指定集合讓分片生效

相關文章
相關標籤/搜索