MongoDB學習筆記三:MongoDB集羣環境搭建

MongoDB學習筆記三:MongoDB集羣環境搭建

標籤(空格分隔): MongoDB


一、集羣搭建簡介

    爲什麼使用集羣架構?
    主從:故障轉移:無法實現,如果主機宕機,需要關閉slave並且按照master模式啓動。無法解決單點故障 無法autofailover  不可以自動主從的切換
    爲了解決主從的問題,MongoDB3.0之後出現副本集,副本集解決了故障轉移的問題,但是一個副本集中的數據是相同的,無法做到海量數據的存儲。所以就需要一個架構去解決這個問題。也就是分片式集羣。
    一個健壯的簡單的MongoDB集羣的搭建需要十個服務進程(分開搭建需要十臺服務器),這裏在一臺虛擬機上進行搭建。如下圖

這裏寫圖片描述

    Router:路由。作用就是轉發客戶端的請求。
    Config Server:配置服務器。用於記錄Shard和分片的詳細情況。
    Shard:分片服務器。真正存儲數據的地方。搭建副本集。

分片的兩種模式:Range Based Sharding 和 Hash Based Sharding

兩者的特點和不同點,參考官方文檔。
  1. Range Based Sharding

這裏寫圖片描述

  1. Hash Based Sharding

這裏寫圖片描述

二、搭建過程

  1. 搭建副本集:Shard1【名稱:motui】
    啓動服務

    ./bin/mongod --dbpath /root/mongodb/shard1/rep1/ --port 27017  --journal    --replSet  motui
    ./bin/mongod --dbpath /root/mongodb/shard1/rep2/ --port 27018  --journal    --replSet  motui
    ./bin/mongod --dbpath /root/mongodb/shard1/rep3/ --port 27019  --journal    --replSet  motui

    鏈接任意一臺mongo :

    ./bin/mongo --port 27018
    
    use admin  
    
    var config = { 
     _id:"motui", 
     members:[
       {_id:0,host:"192.168.0.167:27019"},
       {_id:1,host:"192.168.0.167:27018"},
       {_id:2,host:"192.168.0.167:27017"}]
     }
    
    rs.initiate(config);//初始化副本集
    
    rs.slaveOk() //訪問從機需要先執行
    
    查看主機:在master執行rs.isMaster();
    查看副本集狀態:rs.status();
    
    添加數據創建庫
  2. 搭建副本集:Shard2【名稱:motui1】

    啓動服務

    ./bin/mongod --dbpath /root/mongodb/shard2/rep1/ --port 27021  --journal    --replSet  motui1
    ./bin/mongod --dbpath /root/mongodb/shard2/rep2/ --port 27022  --journal    --replSet  motui1
    ./bin/mongod --dbpath /root/mongodb/shard2/rep3/ --port 27023  --journal    --replSet  motui1

    鏈接任意一臺mongo :

    ./bin/mongo --port 27021
    
    use admin  
    
    var config = { 
     _id:"motui1", 
     members:[
       {_id:0,host:"192.168.0.167:27023"},
       {_id:1,host:"192.168.0.167:27022"},
       {_id:2,host:"192.168.0.167:27021"}]
     }
    
    rs.initiate(config);//初始化副本集
    
    rs.slaveOk(); //訪問從機需要先執行
    
    查看主機:在master執行rs.isMaster();
    查看副本集狀態:rs.status();
    
    添加數據創建庫
  3. 啓動副本集motui 和 motui1

    ./bin/mongod --dbpath /root/mongodb/shard1/rep1/ --port 27017  --journal --replSet  motui --shardsvr
    ./bin/mongod --dbpath /root/mongodb/shard1/rep2/ --port 27018  --journal    --replSet  motui --shardsvr
    ./bin/mongod --dbpath /root/mongodb/shard1/rep3/ --port 27019  --journal    --replSet  motui --shardsvr
    
    
    ./bin/mongod --dbpath /root/mongodb/shard2/rep1/ --port 27021  --journal    --replSet  motui1 --shardsvr
    ./bin/mongod --dbpath /root/mongodb/shard2/rep2/ --port 27022  --journal    --replSet  motui1 --shardsvr
    ./bin/mongod --dbpath /root/mongodb/shard2/rep3/ --port 27023  --journal    --replSet  motui1 --shardsvr
  4. 啓動配置服務器

    ./bin/mongod --dbpath /root/mongodb/conf/ --port 27025  --journal   --configsvr
    
    ./bin/mongos --port 8000 --configdb  192.168.0.167:27025 --chunkSize 1
    
    連接多臺配置服務器命令:
    ./bin/mongos --port 8000 --configdb  192.168.111.129:27025,192.168.111.129:27026,192.168.111.129:27027 --chunkSize 1    
  5. 連接路由器服務器

    ./bin/mongo --port 8000
    
    use admin
    
    db.runCommand({addshard:"motui/192.168.0.167:27017,192.168.0.167:27018,192.168.0.167:27019",name:"Basic Shard Server1"})
    
    db.runCommand({addshard:"motui1/192.168.0.167:27021,192.168.0.167:27022,192.168.0.167:27023",name:"Basic Shard Server2"})
    
  6. 對testUser數據庫分片

    sh.enableSharding("testUser");
  7. 對ID進行分片
    使用hash based方式

    sh.shardCollection("testUser.t_user", {"_id": "hashed" });
    sh.shardCollection("testUser.t_order", {"_id": "hashed" });
  8. 數據測試

    use testUser
    for(var i=0; i<10000; i++){
        db.t_user.insert({name:"user"+i, age:i, email:"[email protected]" })
    }
    for(var i=0; i<10000; i++){
        db.t_order.insert({name:"user"+i, age:i, email:"[email protected]" })
    }
    查看狀態:db.t_user.stats();    db.t_order.stats();

附錄:主從複製
啓動服務

./bin/mongod --dbpath /root/mongodb/shard1/master --port 27017 --master --journal
./bin/mongod --dbpath /root/mongodb/shard1/slave1/ --port 27018 --slave --journal --source 192.168.0.167:27017
./bin/mongod --dbpath /root/mongodb/shard1/slave2/ --port 27018 --slave --journal --source 192.168.0.167:27017 --slavedelay 120

連接客戶端查看狀態

./bin/mongo --port 27017 --host 192.168.0.167
rs.slaveOk();   查看從機 
rs.help();  查看集羣狀態

命令:

    rs.status()                                { replSetGetStatus : 1 } checks repl set status
    rs.initiate()                              { replSetInitiate : null } initiates set with default settings
    rs.initiate(cfg)                           { replSetInitiate : cfg } initiates set with configuration cfg
    rs.conf()                                  get the current configuration object from local.system.replset
    rs.reconfig(cfg)                           updates the configuration of a running replica set with cfg (disconnects)
    rs.add(hostportstr)                        add a new member to the set with default attributes (disconnects)
    rs.add(membercfgobj)                       add a new member to the set with extra attributes (disconnects)
    rs.addArb(hostportstr)                     add a new member which is arbiterOnly:true (disconnects)
    rs.stepDown([stepdownSecs, catchUpSecs])   step down as primary (disconnects)
    rs.syncFrom(hostportstr)                   make a secondary sync from the given member
    rs.freeze(secs)                            make a node ineligible to become primary for the time specified
    rs.remove(hostportstr)                     remove a host from the replica set (disconnects)
    rs.slaveOk()                               allow queries on secondary nodes

    rs.printReplicationInfo()                  check oplog size and time range
    rs.printSlaveReplicationInfo()             check replica set members and replication lag
    db.isMaster()                              check who is primary
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章