MongoDB Sharing

MongoDB Sharing

可以先看看官網介紹:Sharding Introduction
Shards 類似 Mysql 的Data nodes.
Config Servers 類似 Mysql 的Management node.
Routing Processes 類似 Mysql 的SQL node.

架構:

1
2
3
4
5
    主機         shars      shars      shars     config   Routing
192.168.1.52   myset52    myset59    myset60      yes
192.168.1.59   myset52    myset59    myset60      yes
192.168.1.60   myset52    myset59    myset60      yes
192.168.1.61   myset52備  myset59備  myset60備               yes

設定:

1
2
3
4
$ mkdir /var/lib/mongodb_s1
$ mkdir /var/lib/mongodb_s2
$ mkdir /var/lib/mongodb_s3
$ mkdir /var/lib/mongodb_c

設定各個設定檔.
/etc/mongodb_s1.conf

1
2
3
4
5
6
7
8
9
dbpath=/var/lib/mongodb_s1
logpath=/var/log/mongodb/mongodb_s1.log
logappend=true
bind_ip = 127.0.0.1
port = 29017
nohttpinterface = true
shardsvr = true
rest = true
replSet = myset52

/etc/mongodb_s2.conf

1
2
3
4
5
6
7
8
9
dbpath=/var/lib/mongodb_s2
logpath=/var/log/mongodb/mongodb_s2.log
logappend=true
bind_ip = 127.0.0.1
port = 29018
nohttpinterface = true
shardsvr = true
rest = true
replSet = myset59

/etc/mongodb_s3.conf

1
2
3
4
5
6
7
8
9
dbpath=/var/lib/mongodb_s3
logpath=/var/log/mongodb/mongodb_s3.log
logappend=true
bind_ip = 127.0.0.1
port = 29019
nohttpinterface = true
shardsvr = true
rest = true
replSet = myset60

/etc/mongodb_c.conf

1
2
3
4
5
6
dbpath=/var/lib/mongodb_c
logpath=/var/log/mongodb/mongodb_c.log
logappend=true
bind_ip = 127.0.0.1                                                          
port = 29020
nohttpinterface = true

個別啟動

1
2
3
4
$ /usr/bin/mongod --fork --config /etc/mongodb_s1.conf
$ /usr/bin/mongod --fork --config /etc/mongodb_s2.conf
$ /usr/bin/mongod --fork --config /etc/mongodb_s3.conf
$ /usr/bin/mongod --configsvr --fork --config /etc/mongodb_c.conf #192.168.1.61不使用Config

設定myset52:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ /usr/bin/mongo 192.168.1.52:29017/admin
MongoDB shell version: 1.8.2
connecting to: admin
> cfg = {
_id : "myset52",
members : [
{ _id : 0, host : "192.168.1.52:29017" },
{ _id : 1, host : "192.168.1.59:29017" },
{ _id : 2, host : "192.168.1.60:29017" },
{ _id : 3, host : "192.168.1.61:29017", arbiterOnly: true}
] }
 
> rs.initiate(cfg);
> rs.status();

設定myset59:

1
2
3
4
5
6
7
8
9
10
11
12
13
$ /usr/bin/mongo 192.168.1.59:29018/admin
MongoDB shell version: 1.8.2
connecting to: admin
> cfg = {
_id : "myset59",
members : [
{ _id : 0, host : "192.168.1.52:29018" },
{ _id : 1, host : "192.168.1.59:29018" },
{ _id : 2, host : "192.168.1.60:29018" },
{ _id : 3, host : "192.168.1.61:29018", arbiterOnly: true}
] }
> rs.initiate(cfg);
> rs.status();

設定myset60:

1
2
3
4
5
6
7
8
9
10
11
12
13
$ /usr/bin/mongo 192.168.1.59:29019/admin
MongoDB shell version: 1.8.2
connecting to: admin
> cfg = {
_id : "myset60",
members : [
{ _id : 0, host : "192.168.1.52:29019" },
{ _id : 1, host : "192.168.1.59:29019" },
{ _id : 2, host : "192.168.1.60:29019" },
{ _id : 3, host : "192.168.1.61:29019", arbiterOnly: true}
] }
> rs.initiate(cfg);
> rs.status();

再192.168.1.61啟動 Routing

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
$ /usr/local/mongodb/bin/mongos --fork --configdb "192.168.1.52:29020,192.168.1.59:29020,192.168.1.60:29020" --chunkSize 1 --logpath /var/log/mongos.log --port 2920
$ /usr/bin/mongo 192.168.1.59:2920/admin
MongoDB shell version: 1.8.2
connecting to: admin
> db.runCommand( { addshard : "myset52/192.168.1.52:29017,192.168.1.59:29017,192.168.1.60:29017" } )# 設定shar
> db.runCommand( { addshard : "myset59/192.168.1.59:29018,192.168.1.52:29018,192.168.1.60:29018" } )# 設定shar
> db.runCommand( { addshard : "myset60/192.168.1.60:29019,192.168.1.59:29019,192.168.1.52:29019" } )# 設定shar
>
> db.runCommand( { enablesharding : "test" } )# 開啟shar 的db
>
> db.runCommand( { shardcollection : "test.user", key : {_id : 1} } ) # 開啟shar 的collection
> db.runCommand( { shardcollection : "test.group", key : {_id : 1} } )
> db.runCommand( { shardcollection : "test.name", key : {_id : 1} } )
> db.runCommand( { shardcollection : "test.password", key : {_id : 1} } )
>
> db.printShardingStatus(); #查看使用狀況
--- Sharding Status --- 
  sharding version: { "_id" : 1, "version" : 3 }
  shards:
      {
	"_id" : "myset52",
	"host" : "myset52/192.168.1.52:29017,192.168.1.60:29017,192.168.1.59:29017"
}
      {
	"_id" : "myset59",
	"host" : "myset59/192.168.1.59:29018,192.168.1.60:29018,192.168.1.52:29018"
}
      {
	"_id" : "myset60",
	"host" : "myset60/192.168.1.60:29019,192.168.1.59:29019,192.168.1.52:29019"
}
  databases:
	{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
	{ "_id" : "test", "partitioned" : true, "primary" : "myset52" }
		test.user chunks:
				myset52	1
			{ "_id" : { $minKey : 1 } } -->> { "_id" : { $maxKey : 1 } } on : myset52 { "t" : 1000, "i" : 0 }
		test.group chunks:
				myset52	1
			{ "_id" : { $minKey : 1 } } -->> { "_id" : { $maxKey : 1 } } on : myset52 { "t" : 1000, "i" : 0 }
		test.name chunks:
				myset52	5
			{ "_id" : { $minKey : 1 } } -->> { "_id" : ObjectId("4e4cb7fa61c5b9654a000005") } on : myset52 { "t" : 1000, "i" : 1 }
			{ "_id" : ObjectId("4e4cb7fa61c5b9654a000005") } -->> { "_id" : ObjectId("4e4cba3461c5b96f4c000002") } on : myset52 { "t" : 1000, "i" : 5 }
			{ "_id" : ObjectId("4e4cba3461c5b96f4c000002") } -->> { "_id" : ObjectId("4e4cba487f8b9ac33500000b") } on : myset52 { "t" : 1000, "i" : 7 }
			{ "_id" : ObjectId("4e4cba487f8b9ac33500000b") } -->> { "_id" : ObjectId("4e4cbbf37f8b9aa435000005") } on : myset52 { "t" : 1000, "i" : 8 }
			{ "_id" : ObjectId("4e4cbbf37f8b9aa435000005") } -->> { "_id" : { $maxKey : 1 } } on : myset52 { "t" : 1000, "i" : 4 }
		test.password chunks:
				myset52	1
			{ "_id" : { $minKey : 1 } } -->> { "_id" : { $maxKey : 1 } } on : myset52 { "t" : 1000, "i" : 0 }
	{ "_id" : "test", "partitioned" : false, "primary" : "myset52" }

設定完成!!!!
所有程式只需要連接到 192.168.1.61:2920的 Routing
其他動作都不必要.

2 thoughts on “MongoDB Sharing

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *