MongoDB整理筆記のReplica Sets + Sharding

來源:互聯網
上載者:User

標籤:

    MongoDB Auto-Sharding 解決了海量儲存和動態擴容的問題,但離實際生產環境所需的高可靠、高可用還有些距離,所以有了"Replica Sets + Sharding"的解決方案。

    shard:

    使用Replica Sets,確保每個資料節點都具有備份,自動容錯轉移,自動回複能力。

    config:

    使用3個設定管理員,確保中繼資料的完整性。

    route:

    使用3個路由進程,實現負載平衡,提高用戶端接入效能。

     配置Replica Sets + Sharding 架構圖:

     

    

    配置Replica Sets + Sharding

   (1)配置shard1所用到的Replica Sets

    在server A上

[[email protected] bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard1 --port 27017--dbpath /data/shard1_1 --logpath /data/shard1_1/shard1_1.log --logappend --fork[[email protected] bin]# all output going to: /data/shard1_1/shard1_1.logforked process: 18923

    在server B上

[[email protected] bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard1 --port 27017--dbpath /data/shard1_2 --logpath /data/shard1_2/shard1_2.log --logappend --forkforked process: 18859[[email protected] bin]# all output going to: /data/shard1_2/shard1_2.log[[email protected] bin]#

    在Server C 上

[[email protected] bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard1 --port 27017--dbpath /data/shard1_3 --logpath /data/shard1_3/shard1_3.log --logappend --forkall output going to: /data/shard1_3/shard1_3.logforked process: 18768[[email protected] bin]#

    用mongo 串連其中一台機器的27017 連接埠的mongod,初始化Replica Sets“shard1”,執行:

[[email protected] bin]# ./mongo --port 27017MongoDB shell version: 1.8.1connecting to: 127.0.0.1:27017/test> config = {_id: ‘shard1‘, members: [... {_id: 0, host: ‘192.168.3.231:27017‘},... {_id: 1, host: ‘192.168.3.232:27017‘},... {_id: 2, host: ‘192.168.3.233:27017‘}]... }……> rs.initiate(config){"info" : "Config now saved locally. Should come online in about a minute.","ok" : 1}
View Code

   (2)配置shard2所用到的Replica Sets

     在server A上

[[email protected] bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard2 --port 27018--dbpath /data/shard2_1 --logpath /data/shard2_1/shard2_1.log --logappend --forkall output going to: /data/shard2_1/shard2_1.log[[email protected] bin]# forked process: 18993[[email protected] bin]#
View Code

     在server B上

[[email protected] bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard2 --port 27018--dbpath /data/shard2_2 --logpath /data/shard2_2/shard2_2.log --logappend --forkall output going to: /data/shard2_2/shard2_2.logforked process: 18923[[email protected] bin]#
View Code

     在Server C上

[[email protected] bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard2 --port 27018--dbpath /data/shard2_3 --logpath /data/shard2_3/shard2_3.log --logappend --fork[[email protected] bin]# all output going to: /data/shard2_3/shard2_3.logforked process: 18824[[email protected] bin]#
View Code

     用mongo 串連其中一台機器的27018 連接埠的mongod,初始化Replica Sets “shard2”,執行:

[[email protected] bin]# ./mongo --port 27018MongoDB shell version: 1.8.1connecting to: 127.0.0.1:27018/test> config = {_id: ‘shard2‘, members: [... {_id: 0, host: ‘192.168.3.231:27018‘},... {_id: 1, host: ‘192.168.3.232:27018‘},... {_id: 2, host: ‘192.168.3.233:27018‘}]... }……> rs.initiate(config){"info" : "Config now saved locally. Should come online in about a minute.","ok" : 1db.runCommand({ enablesharding:"test" })db.runCommand({ shardcollection: "test.users", key: { _id:1 }})}
View Code

  (3)配置3 台Config Server
    在Server A、B、C上執行:

/Apps/mongo/bin/mongod --configsvr --dbpath /data/config --port 20000 --logpath/data/config/config.log --logappend --fork

   (4)配置3台Route Process
    在Server A、B、C上執行: 

/Apps/mongo/bin/mongos --configdb192.168.3.231:20000,192.168.3.232:20000,192.168.3.233:20000 --port 30000 --chunkSize 1--logpath /data/mongos.log --logappend --fork

  (5)配置Shard Cluster
    串連到其中一台機器的連接埠30000 的mongos 進程,並切換到admin 資料庫做以下配置

[[email protected] bin]# ./mongo --port 30000MongoDB shell version: 1.8.1connecting to: 127.0.0.1:30000/test> use adminswitched to db admin>db.runCommand({addshard:"shard1/192.168.3.231:27017,192.168.3.232:27017,192.168.3.233:27017"});{ "shardAdded" : "shard1", "ok" : 1 }>db.runCommand({addshard:"shard2/192.168.3.231:27018,192.168.3.232:27018,192.168.3.233:27018"});{ "shardAdded" : "shard2", "ok" : 1 }>
View Code

    啟用資料庫及集合的分區 

db.runCommand({ enablesharding:"test" })db.runCommand({ shardcollection: "test.users", key: { _id:1 }})

   (6)驗證Sharding正常工作
    串連到其中一台機器的連接埠30000 的mongos 進程,並切換到test 資料庫,以便添加測試資料 

use testfor(var i=1;i<=200000;i++) db.users.insert({id:i,addr_1:"Beijing",addr_2:"Shanghai"});db.users.stats(){"sharded" : true,"ns" : "test.users","count" : 200000,"size" : 25600384,"avgObjSize" : 128,"storageSize" : 44509696,"nindexes" : 2,"nchunks" : 15,"shards" : {"shard0000" : {……},"shard0001" : {……}},"ok" : 1}
View Code

    可以看到Sharding搭建成功了,跟我們期望的結果一致,至此我們就將Replica Sets與Sharding結合的架構也學習完畢了!

MongoDB整理筆記のReplica Sets + Sharding

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.