根據下面的任務順序來部署一個分片叢集:
警告
分片和“localhost”位址
如果你使用“localhost”或者是127.0.0.1 作為任一節點辨別的主機名部分,例如addShard 方法的host 參數或者是運作時操作 --configdb的值,那麼你必須為叢集中的所有MongoDB的所有節點設定都使用“localhost”或者是127.0.0.1。如果你把本地位址和遠端位址混合使用的話,MongoDB将會出現錯誤。
啟動Config Server的資料庫執行個體
Config Server程序是用來存放叢集中繼資料的mongod執行個體。你可以通過 --configsvr 操作指定一個mongod 執行個體作為configure server。每個config server都儲存了一份叢集中繼資料的備份。
在生産環境中,你必須部署恰好三個config server執行個體,并且每個執行個體都要運作在不同的伺服器上,來保證正常運作時間和資料安全。在測試環境中,你可以讓三個執行個體在一台機器上運作。
重要
一個分片叢集中的所有成員必須能夠連接配接到這個分片叢集中的其他所有成員,包括所有的分片和config servers。要確定包括防火牆在内的安全系統和網絡允許這些連接配接。
- 為每個config server建立資料目錄。在預設情況下,一個config server将它的資料檔案存儲再/data/configdb 目錄下。你可以選擇一個其他的位置。要建立一個資料目錄,執行類似下面的指令:
mkdir /data/configdb
- 啟動三個config server 執行個體。啟動每個執行個體使用滿足下面文法的指令:
Config Servers 預設的端口号是27019。 你可以制定其它的端口号。下面的示例啟動了一個使用預設端口号和預設資料路徑的config server:mongod --configsvr --dbpath <path> --port <port>
mongod --configsvr --dbpath /data/configdb --port 27019
有關更多的指令選項, 檢視 mongod 或者 Configuration File Options.
提示
在你第一次初始化一個分片叢集時,所有的config servers 都必須處于運作狀态,并且要能被夠通路到。
啟動Mongos執行個體
Mongos 執行個體是輕量級的并且不需要資料目錄。你可以在運作着叢集其他元件的系統上運作一個mongos執行個體,例如一個應用伺服器(注:運作這用戶端程式的伺服器)或者一台運作着mongod程序的伺服器。預設情況下,mongos的端口号是 27017。
當你啟動 mongos 執行個體時,要指定三個config servers的hostnames,要麼通過配置檔案指定,要麼通過指令行參數指定。
貼士
為了避免停機時間,給每個config server設定一個邏輯上的DNS名稱(與伺服器的實體或者虛拟節點名稱無關)。如果沒有邏輯DNS名稱,移動或者重命名一個config server需要關閉叢集中所有 mongod 和 mongos 執行個體。
要啟動一個 mongos 執行個體, 執行使用下面文法的指令:
mongos --configdb <config server hostnames>
例如:要啟動一個連接配接在以下節點上運作在預設端口的config server執行個體的 mongos 執行個體:
- cfg0.example.net
- cfg1.example.net
- cfg2.example.net
你需要發出先面的指令:
mongos --configdb cfg0.example.net:27019,cfg1.example.net:27019,cfg2.example.net:27019
叢集中每一個 mongos 都必須使用相同的 configDB 字元串, 通過相同的順序列出的相同的主機名稱(with identical host names listed in identical order)。
如果你啟動一個 mongos 執行個體使用的字元串不能正确比對叢集中其他的 mongos 執行個體使用的字元串,這個 mongos 将會報 Config Database String Error 錯誤,并且拒絕啟動。
添加分片到叢集中
一個分片可以是一個單獨的mongod執行個體也可以是一個複制集。在生産環境中,所有的分片都應該是一個複制集。
- 通過 mongo shell, 連接配接上 mongos 執行個體。發出使用下面文法的指令:
例如:如果一個 mongos 可以在 mongos0.example.net 的 27017端口上被通路到, 發出下面的指令:mongo --host <hostname of machine running mongos> --port <port mongos listens on>
mongo --host mongos0.example.net --port 27017
-
添加每個分片到叢集都要使用 sh.addShard() 方法,向下面的例子展示的那樣。如果這個分片是一個複制集,那麼指定這個資料集的名稱和集合中的一個成員。在生産環境中,所有的分片都必須是複制集。
可選
你可以是喲你資料庫指令 addShard 來指定叢集的名稱和最大數目。如果你不指定這些,MongoDB會自動指定。要使用資料庫指令,檢視 addShard.
下面是通過 sh.addShard() 添加到叢集的示例:
-
要給一個名字是 rs1 并且運作在端口為27017 的mongodb0.example.net上的複制集添加一個分片,發出下面的指令:
版本2.0.3中的變更
對于早于2.0.3版本的MongoDB,你必須指定複制集中所有的成員。例如:
- 要給運作在 mongodb0.example.net 端口号為27017的單獨的mongod執行個體添加分片,發出如下指令:
提示
讓 chunks 遷移到新的分片上可能會花費些時間。
-
對資料庫啟用分片
你必須對資料庫的集合啟用分片才能分片一個集合。(Before you can shard a collection, you must enable sharding for the collection’s database)。啟用資料庫的分片不會對資料進行重新配置設定,而是讓資料庫中的集合能夠被分片。
一旦你為資料庫啟用分片,MongoDB會再分片之前為存放所有資料的資料庫指定一個 primary shard 。
- 通過 mongo shell連接配接到 mongos 執行個體。發出使用下面文法的指令:
mongo --host <hostname of machine running mongos> --port <port mongos listens on>
- 執行 sh.enableSharding() 方法,指定已啟用分片的資料庫的名稱。使用下面的文法:
可選項,你可以使用 enableSharding 指令為資料庫啟用分片:
對集合啟用分片
在每一個集合的基礎上啟用分片。
- 指定你要使用的 shard key。你對shard key的選擇會影響到分片的效率。
- 如果集合已經包含資料,你必須再shard key上使用ensureIndex()方法來建立索引。如果集合是空的,MongoDB會在sh.shardCollection() 的步驟中建立索引。
-
通過再mongo shell 中執行sh.shardCollection() 方法來為集合啟用分片。使用如下文法:
Replace the <database>.<collection> string with the full namespace of your database, which consists of the name of your database, a dot (e.g. .), and the full name of the collection. Theshard-key-pattern represents your shard key, which you specify in the same form as you would an index key pattern.
EXAMPLE
The following sequence of commands shards four collections:
以下操作按順序分片:sh.shardCollection("records.people", { "zipcode": 1, "name": 1 } ) sh.shardCollection("people.addresses", { "state": 1, "_id": 1 } ) sh.shardCollection("assets.chairs", { "type": 1, "_id": 1 } ) sh.shardCollection("events.alerts", { "_id": "hashed" } )
-
The people collection in the records database using the shard key { "zipcode": 1,"name": 1 }.
This shard key distributes documents by the value of the zipcode field. If a number of documents have the same value for this field, then that chunk will be splittable by the values of the name field.
-
The addresses collection in the people database using the shard key { "state": 1,"_id": 1 }.
This shard key distributes documents by the value of the state field. If a number of documents have the same value for this field, then that chunk will be splittable by the values of the _id field.
-
The chairs collection in the assets database using the shard key { "type": 1,"_id": 1 }.
This shard key distributes documents by the value of the type field. If a number of documents have the same value for this field, then that chunk will be splittable by the values of the _idfield.
-
The alerts collection in the events database using the shard key { "_id": "hashed"}.
New in version 2.4.
這個shard key通過_id字段的hash值來對文檔進行配置設定。MongoDB computes the hash of the _id field for the hashed index, which should provide an even distribution of documents across a cluster.
-