1. 程式人生 > >Mongodb同機多例項部署

Mongodb同機多例項部署

所謂同機多例項是指在同一臺伺服器上部署多個mongod例項,本部署教程在2.4、2.6和2.8版本下均可使用,本博主均已測試通過。需要額外注意的是2點:

1、需要同步伺服器時間

2、使用numactl啟動mongod、mongos。

另外Config檔案的設定請參考本部落格的“Mongodb設定”。

1       部署過程

1.1 同步伺服器時間

部署前必須同步伺服器時間,否則容易出現奇怪問題,如”wait**s for distributed lock”等。同步伺服器時間需要先設定同步點,即找臺伺服器啟動時間同步服務。然後再在各個伺服器上同步該同步點:

         ntpdate 10.1.1.2

這裡10.1.1.2是同步點伺服器。如果不同步時間出現一例錯誤如下:升級mongos2.8會一直等待鎖:

1.2 啟動mongod

需要注意的是,因為本部署為多mongod同時執行在一臺伺服器上,所以需要使用numactl啟動mongod,numactl的作用是讓作業系統識別不同的mongod以便進行管理,避免造成衝突。

在10.1.101.8上執行:

numactl --interleave=all bin/mongod –f conf/sd1.conf

numactl --interleave=all bin/mongod –f conf/sd2.conf

numactl --interleave=all bin/mongod –f conf/sd3.conf

在另外兩臺機器上也執行上述命令

1.3 設定副本集

在10.1.101.8上執行

/usr/local/mongodb/bin/mongo localhost:27001

use admin

config={_id:"set1",members:[{_id:0,host:"10.1.101.8:27001"},{_id:1,host:"10.1.101.9:27001"},{_id:2,host:"10.1.101.10:27001","arbiterOnly":true}]};

rs.initiate(config)

在10.1.101.9上執行

/usr/local/mongodb/bin/mongo localhost:27002

use admin

config={_id:"set2",members:[{_id:0,host:"10.1.101.9:27002"},{_id:1,host:"10.1.101.10:27002"},{_id:2,host:"10.1.101.8:27002","arbiterOnly":true}]};

rs.initiate(config)

在10.1.101.10上執行

/usr/local/mongodb/bin/mongo localhost:27003

use admin

config={_id:"set3",members:[{_id:0,host:"10.1.101.10:27003"},{_id:1,host:"10.1.101.8:27003"},{_id:2,host:"10.1.101.9:27003","arbiterOnly":true}]};

rs.initiate(config)

執行完上述命令後,可執行rs.config()檢視副本集。

1.4 啟動config server

10.1.101.8,9,10啟動config

numactl --interleave=all bin/mongod –f conf/configsvr.conf

其餘兩臺機器也執行上述命令。

可使用netstat –lntp | grep mongo檢視已啟動程序。

1.5 啟動mongos

10.1.101.8啟動mongos

numactl --interleave=all bin/mongos –f conf/mongos.conf

1.6 配置shard

/usr/local/mongodb/bin/mongolocalhost:6080

useadmin

db.runCommand({addshard:"set1/10.1.101.8:27001,10.1.101.9:27001,10.1.101.10:27001"})

db.runCommand({addshard:"set2/10.1.101.8:27002,10.1.101.9:27002,10.1.101.10:27002"})

db.runCommand({addshard:"set3/10.1.101.8:27003,10.1.101.9:27003,10.1.101.10:27003"})

db.runCommand({enablesharding:"docinfo"})

db.runCommand({shardcollection:"docinfo.docinfo",key:{_id:1}})

db.runCommand({enablesharding:"doctext"})

db.runCommand({shardcollection:"doctext.doctext",key:{_id:1}})

1.7 停掉balancer

use config

sh.stopBalancer()

或使用以下命令

db.settings.update({_id:"balancer"},{$set:{stopped:true}},true);

2       注意事項

大部分注意事項在上文中已經提及。上述配置過程基本和本部落格文章:“mongodb的配置”一致,請參考。