MongoDB分片實戰(一):集羣搭建

隨筆- 71 文章- 9 評論- 143

MongoDB分片實戰(一):集羣搭建

環境準備

Linux環境

主機 OS 備註
192.168.32.13  CentOS6.3 64位  普通PC
192.168.71.43  CentOS6.2 64位  服務器,NUMA CPU架構

MongoDB版本:mongodb-linux-x86_64-2.4.1,下載地址:www.mongodb.org/downloads.html

MongoDB安裝:分別在兩臺機器上安裝好mongodb 2.4.1,安裝路徑都爲/url/local/mongodb-2.4.1/linux

 cd /usr/local/src/ wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.4.1.tgz tar -zxvf mongodb-linux-x86_64-2.4.1.tgz cp -r mongodb-linux-x86_64-2.4.1 /usr/local/mongodb-2.4.1 cd /usr/local/mongodb-2.4.1/bin/ ll 

能夠看到mongodb安裝成功有以下模塊:mongodb

mongodb啓動和關閉等在後面集羣搭建中有詳細說明,在此再也不贅述。數據庫

Sharding集羣搭建

Mongodb一共有三種集羣搭建的方式:Replica Set(副本集)、Sharding(切片)和Master-Slaver(主從)。下面要搭建的是Sharding,Sharding集羣也是三種集羣中最複雜的。服務器

配置服務器啓動(192.168.32.13:10000):架構

 1.    ./bin/mongod --fork --dbpath data/config/ --logpath log/config.log –port 10000 

路由服務器啓動(192.168.32.13:20000):ide

 1.    ./bin/mongos --port 20000 --configdb 192.168.32.13:10000 --logpath log/mongos.log  --fork 

注意1:配置--conigdb的時候ip地址不能填localhost或127.0.0.1不然添加分片時會返回以下錯誤信息:post

 1. { 2.            "ok" : 0, 3.            "errmsg" : "can't use localhost as a shard since all shards need to communicate. either use all shards and configdbs in localhost or all in actual IPs host: 192.168.71.43:27017 isLocalHost:0" 4.    } 

啓動分片1(192.168.32.13:27019):測試

 1.    ./bin/mongod --dbpath data/shard3/ --logpath log/shard3.log  --fork --port 27019 

啓動分片2(192.168.32.13:27020):url

 1.    ./bin/mongod --dbpath data/shard3/ --logpath log/shard3.log  --fork --port 27020 

啓動分片3(192.168.71.43:27017):

 1.    numactl --interleave=all ./bin/mongod --dbpath data/shard1/ --logpath log/shard1.log  --fork --port 27017 

啓動分片4(192.168.71.43:27018):

 1.    numactl --interleave=all ./bin/mongod --dbpath data/shard2/ --logpath log/shard2.log  --fork --port 27018 

說明:關於這裏爲何加numactl --interleave=all,後面有詳細說明。

Note:在生產環境能夠將啓動的配置信息寫入文件,而後啓動的時候指定配置文件,這樣便於管理:

複製代碼
 1.    vi conf/config.conf 2.    bpath=data/config/ 3.    logpath=log/config.log 4.    port=10000 5.    fork=true  6.    ./bin/mongod -f conf/config.conf 
複製代碼

添加分片1(192.168.32.13:27019):

 1.    ./bin/mongo --port 20000 2.    mongos> use admin 3. switched to db admin 4.    mongos> db.runCommand({addshard:"192.168.32.13:27019",allowLocal:true }) 

注意2:一樣這裏的192.168.32.13不能用localhost或127.0.0.1代替,而且當路由進程和分片在同一臺機器上要指定allowLocal爲true,由於MongoDB儘可能避免錯誤的配置,將集羣配置在本地,因此這個配置指明當前僅僅是用於開發。

添加分片3(192.168.71.43:27017):

 1.    mongos> db.runCommand({addshard:"192.168.71.43:27017" }) 

相似的添加分片2,4。

分片添加成功返回相似下面的信息(當前mongodb版本爲2.4.1):

 1.    { "shardAdded" : "shard0000", "ok" : 1 } 

刪除分片:若是要刪除分片的話能夠removeshard命令:

複製代碼
 1.    mongos> use admin 2. switched to db admin 3.    mongos> db.runCommand({"removeshard":"192.168.32.13:27020"}) 4. { 5.            "msg" : "draining started successfully", 6.            "state" : "started", 7.            "shard" : "shard0001", 8.            "note" : "you need to drop or movePrimary these databases", 9.            "dbsToMove" : [ 10.                    "test3" 11. ], 12.            "ok" : 1 13.    } 
複製代碼

移除分片須要一個過程,MongoDB會把移除的片上的數據(塊)挪到其餘片上,移動中會顯示進度:

複製代碼
 1.    mongos> db.runCommand({"removeshard":"192.168.32.13:27020"}) 2. { 3.            "msg" : "draining ongoing", 4.            "state" : "ongoing", 5.            "remaining" : { 6.                    "chunks" : NumberLong(0), 7.                    "dbs" : NumberLong(1) 8. }, 9.            "note" : "you need to drop or movePrimary these databases", 10.            "dbsToMove" : [ 11.                    "test3" 12. ], 13.            "ok" : 1 14.    } 
複製代碼

注意:若是刪除的片是數據庫的大本營(基片),必須手動移動或刪除數據庫,用moveprimary命令,上面的示例中就提示192.168.32.13:27020是test3庫的大本營(primary),這個信息能夠經過查看config.databases看到:

 1.    mongos> use config 2. switched to db config 3.    mongos> db.databases.find() 4.    { "_id" : "test3", "partitioned" : false, "primary" : "shard0001" } 

這裏shard0001就是192.168.32.13:27020,下面經過moveprimary命令移除test3:

 1.    mongos> use admin 2. switched to db admin 3.    mongos> db.runCommand({"moveprimary":"test3","to":"192.168.32.13:27019"}) 4.    { "primary " : "shard0000:192.168.32.13:27019", "ok" : 1 } 

這時再查看config.databases會發現test3的大本營變成了shard0000(192.168.32.13:27019)

這時再執行removeshard命令則能成功移除分片了:

複製代碼
 1.    mongos> db.runCommand({"removeshard":"192.168.32.13:27020"}) 2. { 3.            "msg" : "removeshard completed successfully", 4.            "state" : "completed", 5.            "shard" : "shard0001", 6.            "ok" : 1 7.    } 
複製代碼

管理分片

進入mongos進程config庫能夠看到目前分片的狀況:

複製代碼
 1.    ./bin/mongo –port 20000 2.  use config 3.    db.shards.find() 1.    mongos> use config 2. switched to db config 3.    mongos> db.shards.find() 4.    { "_id" : "shard0000", "host" : "192.168.32.13:27019" } 5.    { "_id" : "shard0001", "host" : "192.168.71.43:27017" } 6.    { "_id" : "shard0002", "host" : "192.168.71.43:27018" } 
複製代碼

注意3:若是配置過程當中發生過上面注意1中出現的狀況,即配置configdb的時候用了localhost或127.0.0.1,則運行db.shards.find()可能會出現以下錯誤:

 1.    mongos> db.shards.find() 2. error: { 3.            "$err" : "could not initialize sharding on connection 192.168.32.13:10000 :: caused by :: mongos specified a different config database string : stored : localhost:10000 vs given : 192.168.32.13:10000", 4.            "code" : 15907 5.    } 

解決方法是從新啓動config進程

查看分片後的數據庫:

複製代碼
 1.    ./bin/mongo –port 20000 2. use test 3. db.test.user.insert({「test」: 「test」}) 4. …… 5. use config 6.    db.databases.find() 7.    { "_id" : "admin", "partitioned" : false, "primary" : "config" } 8.    { "_id" : "test", "partitioned" : false, "primary" : "shard0000" } 9.    { "_id" : "test2", "partitioned" : false, "primary" : "shard0000" } 10.    { "_id" : "test3", "partitioned" : false, "primary" : "shard0001" } 
複製代碼

「_id」,字符串。表示數據庫名。

「partioned」,布爾型。若是爲true則表示開啓了分片功能。

「primary」,字符串,這個值與「_id」對應表示這個數據庫的「大本營「在哪裏,不論分片與否,數據庫老是會有個「大本營」,建立數據庫時會隨機選擇一個片,也就是說大本營是開始建立數據庫文件的位置。雖然分片的時候數據庫也會用到不少別的服務器,可是從這分片開始。

至此整個mongodb分片集羣基本搭建完成,可是想讓分片正常、高效、穩定的運行還有不少工做要作,下一節將在此基礎上作一些簡單的測試。

相關文章
相關標籤/搜索