Mongodb的分佈式配置

         Mongodb存在三種級別的分佈式配置:主從配置、集羣配置以及集羣分片配置,建議咱們採用集羣配置。mongodb

主從配置

  1. 啓動主服務器: mongod.exe  --dbpath ..\db  --port 27017 –master
  2. 啓動若干個從服務器:mongod.exe --slave --source 127.0.0.1:27017  --dbpath ..\db2 --port 27018,source後面是主服務器的地址和端口號。

這種方式簡單靈活,可用於備份、故障恢復,讀擴展。爲了平衡負載,通常經過讀寫分離模式,即主庫寫、從庫讀。缺點是若是主庫down了,就不能寫了,不會自動恢復。數據庫

集羣配置

  1. 啓動三個實例:mongod.exe –replSet  rs  --dbpath ..\db --port 27017,mongod.exe –replSet  rs  --dbpath ..\db --port 27018,mongod.exe –replSet  rs  --dbpath ..\db --port 27019。接下來要初始化Replica Sets環境
  2. 登陸任何一個實例:mongo.exe --port 27018 –host localhost
  3. config_rs={_id:'rs',members:[{_id:0,host:'127.0.0.1:27017'},{_id:1,host:'127.0.0.1:27018'},{_id:2,host:'127.0.0.1:27019'}]}

 

 

  4. 執行rs.initiate(config_rs),成功後執行rs.status()便可看到配置的集羣。等全部節點的「stateStr」爲「Primary」或者「secondary」則集羣同步好了。服務器

 

 

  5.  以後若是要添加或者刪除節點,使用rs.add(hostportstr), rs.remove(hostportstr)分佈式

在集羣分兩類節點一個Primary和多個secondary(使用rs.isMaster()看節點的類型),Primary可讀寫,secondary只能讀。當Primary 出錯時,剩下的secondary會推舉一個成爲Primary,當出錯的Primary恢復正常時會自動添加到集羣併成爲secondary。性能

  6.  MongoDB.Driver 的連接配置:blog

mongodb://localhost:3000,localhost:3001,localhost:3002/?replicaSet=rs&readPreference=primaryPreferred,若是須要更好的性能能夠使用 readPreference= secondaryPreferred實現讀寫分離。rem

集羣分片配置

  1. 建立集羣shard-a (同集羣配置中的過程)

mongod.exe --shardsvr --replSet shard-a --dbpath "..\data\rs-a-1" --port 3000get

mongod.exe --shardsvr --replSet shard-a --dbpath "..\data\rs-a-2" --port 3001同步

mongod.exe --shardsvr --replSet shard-a --dbpath "..\data\rs-a-3" --port 3002it

config_rsa={_id:'shard-a',members:[{_id:0,host:'127.0.0.1:3000'},{_id:1,host:'127.0.0.1:3001'},{_id:2,host:'127.0.0.1:3002'}]}

rs.initiate(config_rsa)

  2. 建立集羣shard-b

mongod.exe --shardsvr --replSet shard-b --dbpath "..\data\rs-b-1" --port 30100

mongod.exe --shardsvr --replSet shard-b --dbpath "..\data\rs-b-2" --port 30101

mongod.exe --shardsvr --replSet shard-b --dbpath "..\data\rs-b-3" --port 30102

config_rsb={_id:'shard-b',members:[{_id:0,host:'127.0.0.1:30100'},{_id:1,host:'127.0.0.1:30101'},{_id:2,host:'127.0.0.1:30102'}]}

rs.initiate(config_rsb)

  3. 啓動配置服務器

mongod.exe --configsvr --dbpath "..\data\config-1" --port 27019

mongod.exe --configsvr --dbpath "..\data\config-2" --port 27020

mongod.exe --configsvr --dbpath "..\data\config-3" --port 27021

  4. 啓動Mongos(也能夠多個)

Mongos.exe --configdb localhost:27019,localhost:27020,localhost:27021 --port 40000

把分片shard-a, shard-b添加到分片集羣中:(使用mongo.exe登陸Mongos)

sh.addShard("shard-a/127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002")

sh.addShard("shard-b/127.0.0.1:30100,127.0.0.1:30101,127.0.0.1:30102")

  5. 開啓分片集合(登陸到Mongos)

sh.enableSharding("UserSubscribe_DB")

sh.shardCollection("UserSubscribe_DB.Sector",{_id:1})

  6. 查看Mongos狀態

sh.status

db.getSiblingDB("config").databases.find():查看分片數據庫的設置

db.getSiblingDB("config").collections.find():查看分片集合的設置

  7. MongoDB.Driver 的連接配置

mongodb://localhost:40000/

  8. 對於已有非分片數據,再使用分片集合的方案

首先把現有數據導出,而後在根據以上過程配置分片集羣和爲庫開啓分片(選擇合適的鍵進行分片),最後經過Mongos導入數據。這樣經過Mongos就能訪問了。

相關文章
相關標籤/搜索