其中會以兩台主機建成 Shard 的 replica set、以三台主機建成 Config 的 replica set,並以一台主機作為 Mongos
系統環境與前置工作
- 作業系統環境為 Ubuntu 14.04。
- 共有 6 台主機,分別是 1 台 Mongos、3 台 Config Server、2 台 Shard Server。對應的 IP 位址如下表:
Host Name IP Address Mongos 192.168.200.15 Mongo-config-1 192.168.200.16 Mongo-config-2 192.168.200.17 Mongo-config-3 192.168.200.18 Mongo-shard-1 192.168.200.32 Mongo-shard-2 192.168.200.33 123456192.168.200.15 mongos
192.168.200.16 mongo-config-1
192.168.200.17 mongo-config-2
192.168.200.18 mongo-config-3
192.168.200.32 mongo-shard-1
192.168.200.33 mongo-shard-2
- 在每台主機上,依照 [1] 的描述,分別執行以下的指令安裝 MongoDB 3.2 版。
123apt-key adv --keyserver hkp:
//keyserver
.ubuntu.com:80 --recv EA312927
echo
"deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse"
|
sudo
tee
/etc/apt/sources
.list.d
/mongodb-org-3
.2.list
apt-get update && apt-get
install
-y mongodb-org=3.2.0
- 假設每台 MongoDB Shard 都有個 /mnt/mongo-storage 用來存放資料。如果需要檔案系統的選擇建議,可以參考官方的產品化資訊 [2]。
設定 Shard Server 並組成一個 Replica Set
相關的官方文件可參考 [3-4]。- 首先因為 Shard Server 是用來存放資料庫內的資料的節點,因此要提供額外的硬碟空間給 Shard Server。這裡假設提供出來的硬碟空間是掛載在 /mnt/mongo-storage 這個路徑。因此在執行後續動作前,先變更 /mnt/mongo-storage 的 owner,確保 MongoDB 有足夠的權限存取該資料夾。
1chown
-R mongodb:mongodb
/mnt/mongo-storage
- 前置動作的 mongodb-org 套件裝完以後,會自動產生一個 /etc/mongod.conf,用來設定這台主機上的 MongoDB instance。將以下的內容寫入並覆蓋兩台 Shard Server 的 /etc/mongod.conf 檔。
- 將儲存的資料設定為存放在 /mnt/mongo-storage。
- 設定使用 WiredTiger 儲存引擎。
- 設定以 daemon 形式啟動 MongoDB instance。
- 設定存放 operation log 最多使用 1024MB 的磁碟空間。
- 設定 Replica Set 的名稱為 replicaset1。
- 設定這個 MongoDB instance 的角色是 Shard Server。
12345678910111213141516171819202122232425storage:
dbPath:
/mnt/mongo-storage
journal:
enabled:
true
engine: wiredTiger
systemLog:
destination:
file
logAppend:
true
path:
/var/log/mongodb/mongod
.log
net:
port: 27017
bindIp: 0.0.0.0
processManagement:
fork:
true
replication:
oplogSizeMB: 1024
replSetName: replicaset1
sharding:
clusterRole: shardsvr
#snmp:
- 啟動 MongoDB instance。
1/usr/bin/mongod
--config
/etc/mongod
.conf
- 在每台 Shard Server 上,使用 mongo 指令進入本機的 MongoDB instance 的 shell 環境。
1mongo
- 在 Mongo shell 中,輸入以下的指令,建立一個 replica set,其中這個 replica set 的成員包含 mongo-shard-1 和 mongo-shard-2 兩台主機。
1rs.initiate({_id:
"replicaset1"
, members: [{
"_id"
:0,
"host"
:
"mongo-shard-1:27017"
}, {
"_id"
:1,
"host"
:
"mongo-shard-2:27017"
}]})
設定完成後,在第一台 Shard Server 上應該會收到以下的成功訊息:
1{
"ok"
: 1 }
同樣的指令在其他 Config Server 上執行,則會出現已經初始化過了的訊息~。 - 可以透過 conf() 指令,確認現在的 replica set 的狀態:
1rs.conf()
收到的回覆如下:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546{
"_id"
:
"replicaset1"
,
"version"
: 1,
"protocolVersion"
: NumberLong(1),
"members"
: [
{
"_id"
: 0,
"host"
:
"mongo-shard-1:27017"
,
"arbiterOnly"
:
false
,
"buildIndexes"
:
true
,
"hidden"
:
false
,
"priority"
: 1,
"tags"
: {
},
"slaveDelay"
: NumberLong(0),
"votes"
: 1
},
{
"_id"
: 1,
"host"
:
"mongo-shard-2:27017"
,
"arbiterOnly"
:
false
,
"buildIndexes"
:
true
,
"hidden"
:
false
,
"priority"
: 1,
"tags"
: {
},
"slaveDelay"
: NumberLong(0),
"votes"
: 1
}
],
"settings"
: {
"chainingAllowed"
:
true
,
"heartbeatIntervalMillis"
: 2000,
"heartbeatTimeoutSecs"
: 10,
"electionTimeoutMillis"
: 10000,
"getLastErrorModes"
: {
},
"getLastErrorDefaults"
: {
"w"
: 1,
"wtimeout"
: 0
}
}
}
設定 Config Server 並組成一個 Replica Set
相關的官方文件可參考 [3][5]。- 在每一台 Config Server 上編輯 /etc/mongod.conf 檔,以設定 Config Server:
- 設定儲存引擎使用 WiredTiger。
- 設定綁定的 IP 為 0.0.0.0,亦即允許任意來源連接這個 MongoDB instance。
- 設定以 daemon 背景模式執行。
- 設定 Config Server 的 replica set 名稱為 "configReplSet1"。
- 設定這個 MongoDB instance 角色為 Config Server。
1234567891011121314151617181920sharding:
clusterRole: configsvr
replication:
replSetName: configReplSet1
net:
port: 27017
bindIp: 0.0.0.0
storage:
dbPath:
/var/lib/mongodb
systemLog:
destination:
file
logAppend:
true
path:
/var/log/mongodb/mongod
.log
processManagement:
fork:
true
- 啟動 MongoDB instance。
1/usr/bin/mongod
--config
/etc/mongod
.conf
- 在每台 Config Server 上,使用 mongo 指令進入本機的 MongoDB instance 的 shell 環境。
1mongo
- 在 Mongo shell 中,輸入以下的指令,建立一個 replica set,其中這個 replica set 的成員包含 mongo-config-1、mongo-config-2 和 mongo-shard-3 三台主機。
1rs.initiate({_id:
"configReplSet1"
, configsvr:
true
,members: [{_id: 0, host:
"mongo-config-1:27017"
}, {_id: 1, host:
"mongo-config-2:27017"
}, {_id: 2, host:
"mongo-config-3:27017"
}]})
設定完成後,在第一台 Config Server 上應該會收到以下的成功訊息:
1{
"ok"
: 1 }
同樣的指令在其他 Config Server 上執行,則會出現已經初始化過了的訊息~。 - 跟 Shard Server 一樣,設定完以後可以利用 rs.conf() 指令確認設定完成後的 Config replica set 的狀態:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960{
"_id"
:
"configReplSet1"
,
"version"
: 1,
"configsvr"
:
true
,
"protocolVersion"
: NumberLong(1),
"members"
: [
{
"_id"
: 0,
"host"
:
"mongo-config-1:27017"
,
"arbiterOnly"
:
false
,
"buildIndexes"
:
true
,
"hidden"
:
false
,
"priority"
: 1,
"tags"
: {
},
"slaveDelay"
: NumberLong(0),
"votes"
: 1
},
{
"_id"
: 1,
"host"
:
"mongo-config-2:27017"
,
"arbiterOnly"
:
false
,
"buildIndexes"
:
true
,
"hidden"
:
false
,
"priority"
: 1,
"tags"
: {
},
"slaveDelay"
: NumberLong(0),
"votes"
: 1
},
{
"_id"
: 2,
"host"
:
"mongo-config-3:27017"
,
"arbiterOnly"
:
false
,
"buildIndexes"
:
true
,
"hidden"
:
false
,
"priority"
: 1,
"tags"
: {
},
"slaveDelay"
: NumberLong(0),
"votes"
: 1
}
],
"settings"
: {
"chainingAllowed"
:
true
,
"heartbeatIntervalMillis"
: 2000,
"heartbeatTimeoutSecs"
: 10,
"electionTimeoutMillis"
: 10000,
"getLastErrorModes"
: {
},
"getLastErrorDefaults"
: {
"w"
: 1,
"wtimeout"
: 0
}
}
}
啟動 Mongos 服務
Shard Server 和 Config Server 都設定完成後,最後就剩下啟動作為入口介面的 Mongos 了。相關的官方文件可以參考 [5] 的 Start the mongos Instances 小節。- 設定以 30000 port 啟動 Mongos 服務。
1
mongos --configdb configReplSet1
/mongo-config-1
:27017,mongo-config-2:27017,mongo-config-3:27017 -port 30000 -chunkSize 5 -logpath
/var/log/mongodb/mongod
.log -logappend -fork
- 啟動完 Mongos 之後,已經讓 Mongos 跟 Config Server 連接起來,但還沒有讓 Config Server 跟 Shard Server 連接,因此必須進入 Mongos 去設定 Shard Server 的連接資訊。首先先使用 Mongo Client 進入 Mongo Shell,其中因為上一步是用 30000 port 開啟 Mongos,因此這裡要指定 port 30000。
1mongo --port 30000
- 以 addShard() 指令將前面設定的 Shard Server 的 replica set 加入。需要注意的是,這裡不僅需要指定 replica set 的名字,同時也必須指定所有 replica set 中的 Shard Server 的位址。
1sh.addShard(
"replicaset1/mongo-shard-1:27017,mongo-shard-2:27017"
)
如果一切正常的話,應該會出現以下的回覆:
1{
"shardAdded"
:
"replicaset1"
,
"ok"
: 1 }
沒有留言:
張貼留言