<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC] ## 一 分片集概念 ### A 分片概述 分片(sharding)是指將數據庫拆分,將其分散在不同的機器上的過程。 分片集群(sharded cluster)是一種水平擴展數據庫系統性能的方法,能夠將數據集分布式存儲在不同的分片(shard)上,每個分片只保存數據集的一部分,MongoDB保證各個分片之間不會有重復的數據,所有分片保存的數據之和就是完整的數據集。 分片集群將數據集分布式存儲,能夠將負載分攤到多個分片上,每個分片只負責讀寫一部分數據,充分利用了各個shard的系統資源,提高數據庫系統的吞吐量 mongodb3.2版本后,分片技術必須結合復制集完成 **應用場景:** 1. 單臺機器的磁盤不夠用了,使用分片解決磁盤空間的問題 2. 單個mongod已經不能滿足寫數據的性能要求。通過分片讓寫壓力分散到各個分片上面,使用分片服務器自身的資源 3. 想把大量數據放到內存里提高性能。和上面一樣,通過分片使用分片服務器自身的資源 ### B 分片存儲原理 ![分片存儲原理](https://s2.ax1x.com/2019/12/12/Qy0c7Q.png) **存儲方式:** * 數據集被拆分成數據塊(chunk),每個數據塊包含多個doc,數據塊分布式存儲在分片集群中 **角色:** * Config server 1.存儲節點信息 2.存儲分片策略 3.記錄數據存儲的位置 * Shard server 將數據進行分片,拆分成數據塊(chunk),數據塊真正存放的單位 * Mongos server 1.接收用戶請求,返回用戶數據 2.讀取config server,獲取分片策略和節點信息 3.根據分片策略路由請求和獲取數據 4.自動均衡數據chunk **總結:** 應用請求mongos來操作mongodb的增刪改查,配置服務器存儲數據庫元信息,并且和mongos做同步,數據最終存入在shard(分片)上,為了防止數據丟失,同步在副本集中存儲了一份,仲裁節點在數據存儲到分片的時候決定存儲到哪個節點 ### C 分片語法和分片類型 **片鍵類型** 片鍵是文檔的一個屬性字段或是一個復合索引字段,一旦建立后則不可改變,片鍵是拆分數據的關鍵的依據 在數據極為龐大的場景下,片鍵決定了數據在分片的過程中數據的存儲位置,直接會影響集群的性能 創建片鍵時,需要有一個支撐片鍵運行的索引 **語法解析:** ~~~ mongos> use 庫名 mongos> db.集合名.ensureIndex({"鍵名":1}) ##創建索引 mongos> sh.enableSharding("庫名") ##開啟庫的分片 mongos> sh.shardCollection("庫名.集合名",{"鍵名":1}) ##開啟集合的分片并指定片鍵 ~~~ **片鍵分類:** 1. 遞增片鍵 使用時間戳,日期,自增的主鍵,ObjectId,\_id等,此類片鍵的寫入操作集中在一個分片服務器上,寫入不具有分散性,這會導致單臺服務器壓力較大,但分割比較容易,這臺服務器可能會成為性能瓶頸 ```sh > db.bar.ensureIndex({"timestamp":1}) > sh.enableSharding("foo") > sh.shardCollection("foo.bar",{""timestamp":1}) ``` 2. 哈希片鍵 也稱之為散列索引,使用一個哈希索引字段作為片鍵,優點是使數據在各節點分布比較均勻,數據寫入可隨機分發到每個分片服務器上,把寫入的壓力分散到了各個服務器上。但是讀也是隨機的,可能會命中更多的分片,但是缺點是無法實現范圍區分 ```sh > db.bar.ensureIndex({"files_id":"hashed"}) > sh.enableSharding("foo") > sh.shardCollection("foo.fs.chunks",{"files_id":"hashed"}) ``` 3. 組合片鍵 數據庫中沒有比較合適的鍵值供片鍵選擇,或者是打算使用的片鍵基數太小(即變化少如星期只有7天可變化),可以選另一個字段使用組合片鍵,甚至可以添加冗余字段來組合 ```sh > sh.enableSharding("foo") > sh.shardCollection("foo.fs.chunks",{"files_id":1,"n":1}) ``` 4. 標簽片鍵 數據存儲在指定的分片服務器上,可以為分片添加tag標簽,然后指定相應的tag 比如讓`0-1999`出現在shard0000上,`2000-9999`出現在shard0001或shard0002上,就可以使用tag讓均衡器指定分發 ```sh > sh.addShardTag("shard0000","T"); > sh.addShardTag("shard0001","Q"); > sh.addShardTag("shard0002","Q"); > sh.addTagRange("foo.ids",{"id":0},{"id":1999},"T"); > sh.addTagRange("foo.ids",{"id":2000},{"id":9999},"Q"); ``` ## 二 分片集群搭建 ### A 集群規劃和目錄創建 單機啟動10個mongodb實例,端口為38030-38039,其中 1. configserver: 3臺構成的復制集,1主兩從,不支持arbiter 復制集名字:configsvr 端口范圍:38031-38033 2. shard節點: sh1:38034-36(1主1從1個arbiter) sh2:38037-39(1主1從1個arbiter) 3. mongos節點 38030單節點節點 創建目錄 ``` mkdir -p /opt/mongodb/2803{0,1,2,3,4,5,6,7,8,9}/{conf,data,log} ``` ### B configserver復制集群準備 **準備configserver配置文件** ```sh [mongo@noah ~]$ vim /opt/mongodb/28031/conf/mongod.conf systemLog: destination: file path: /opt/mongodb/38031/log/mongodb.log logAppend: true storage: journal: enabled: true dbPath: /opt/mongodb/38031/data directoryPerDB: true wiredTiger: engineConfig: cacheSizeGB: 1 directoryForIndexes: true collectionConfig: blockCompressor: zlib indexConfig: prefixCompression: true processManagement: fork: true net: port: 38031 replication: oplogSizeMB: 2048 replSetName: configReplSet sharding: clusterRole: configsvr ``` **復制配置文件為多份** ```sh cp /opt/mongodb/2803{1,2}/conf/mongod.conf cp /opt/mongodb/2803{1,3}/conf/mongod.conf ``` **修改配置文件端口** ```sh sed -i 's#28031#28032#g' /opt/mongodb/28032/conf/mongod.conf sed -i 's#28031#28033#g' /opt/mongodb/28033/conf/mongod.conf ``` ### C shard復制集群準備 **準備shard配置文件** ```sh [mongo@noah ~]$ vim /opt/mongodb/28034/conf/mongod.conf systemLog: destination: file path: /opt/mongodb/38034/log/mongodb.log logAppend: true storage: journal: enabled: true dbPath: /opt/mongodb/38034/data directoryPerDB: true wiredTiger: engineConfig: cacheSizeGB: 1 directoryForIndexes: true collectionConfig: blockCompressor: zlib indexConfig: prefixCompression: true processManagement: fork: true net: port: 38034 replication: oplogSizeMB: 2048 replSetName: sh1 sharding: clusterRole: shardsvr ``` **復制配置文件為多份** ```sh cp /opt/mongodb/2803{4,5}/conf/mongod.conf cp /opt/mongodb/2803{4,6}/conf/mongod.conf cp /opt/mongodb/2803{4,7}/conf/mongod.conf cp /opt/mongodb/2803{4,8}/conf/mongod.conf cp /opt/mongodb/2803{4,9}/conf/mongod.conf ``` **修改sh1集群配置文件端口** ```sh sed -i 's#28034#28035#g' /opt/mongodb/28035/conf/mongod.conf sed -i 's#28034#28036#g' /opt/mongodb/28036/conf/mongod.conf ``` **修改sh2集群配置文件端口和集群名** ```sh sed -i -e 's#28034#28037#g' -e 's#sh1#sh2#g' /opt/mongodb/28037/conf/mongod.conf sed -i -e 's#28034#28038#g' -e 's#sh1#sh2#g' /opt/mongodb/28038/conf/mongod.conf sed -i -e 's#28034#28039#g' -e 's#sh1#sh2#g' /opt/mongodb/28039/conf/mongod.conf ``` ### D 啟動實例并啟動復制集 **啟動所有實例** ```sh mongod -f /opt/mongodb/38031/conf/mongodb.conf mongod -f /opt/mongodb/38032/conf/mongodb.conf mongod -f /opt/mongodb/38033/conf/mongodb.conf mongod -f /opt/mongodb/38034/conf/mongodb.conf mongod -f /opt/mongodb/38035/conf/mongodb.conf mongod -f /opt/mongodb/38036/conf/mongodb.conf mongod -f /opt/mongodb/38037/conf/mongodb.conf mongod -f /opt/mongodb/38038/conf/mongodb.conf ``` **configserver集群搭建** ```sh mongo --port 38030 admin config = {_id: 'configReplSet', members: [ {_id: 0, host: '10.0.0.11:38030'}, {_id: 1, host: '10.0.0.11:38031'}, {_id: 2, host: '10.0.0.11:38032'}] } rs.initiate(config) ``` **sh1復制集構建:** ```sh mongo --port 38033 admin config = {_id: 'sh1', members: [ {_id: 0, host: '10.0.0.11:38033'}, {_id: 1, host: '10.0.0.11:38034'}, {_id: 2, host: '10.0.0.11:38035',"arbiterOnly":true}] } rs.initiate(config) ``` **sh2復制集構建:** ```sh mongo --port 38036 admin config = {_id: 'sh1', members: [ {_id: 0, host: '10.0.0.11:38036'}, {_id: 1, host: '10.0.0.11:38037'}, {_id: 2, host: '10.0.0.11:38038',"arbiterOnly":true}] } rs.initiate(config) ``` ## 三 分片集群配置 ### A 配置并啟動mongos 節點配置 ```sh [mongo@noah ~]$ vim /opt/mongodb/38030/conf/mongos.conf systemLog: destination: file path: /opt/mongodb/38030/log/mongos.log logAppend: true net: port: 38030 sharding: configDB: configReplSet/10.0.0.11:38031,10.0.0.11:38032,10.0.0.11:38033 processManagement: fork: true ``` 啟動mongos ```sh mongod -f /opt/mongodb/38030/conf/mongodb.conf ``` ### B 配置分片集群 **連接到mongs的admin數據庫** ```sh mongo 10.0.0.11:38030/admin ``` **添加分片** ```sh db.runCommand( { addshard : "sh1/10.0.0.11:38034,10.0.0.11:38035,10.0.0.11:38036",name:"shard1"} ) db.runCommand( { addshard : "sh2/10.0.0.11:38037,10.0.0.11:38038,10.0.0.11:38039",name:"shard2"} ) ``` **列出分片** ```sh mongos> db.runCommand( { listshards : 1 } ) ``` **整體狀態查看** ```sh mongos> sh.status(); ``` ## 四 使用分片集群 ### A Hash分片配置及測試: 例子:對noah庫下的vast大表進行hash 1. 對于noah開啟分片功能 ```sh mongo --port 38030 admin use admin admin> db.runCommand( { enablesharding : "noah" } ) ``` 2. 對于noah庫下的vast表建立hash索引 ```sh use noah noah> db.vast.ensureIndex( { id: "hashed" } ) ``` 3. 開啟分片 ```sh use admin admin > sh.shardCollection( "noah.vast", { id: "hashed" } ) ``` 4. 錄入10w行數據測試 ```sh use noah for(i=1;i<100000;i++){ db.vast.insert({"id":i,"name":"luogang","age":70,"date":new Date()}); } ``` 5. hash分片結果測試 ```sh mongo --port 38034 use noah db.vast.count(); mongo --port 38037 use noah db.vast.count(); ``` ### B RANGE分片配置及測試 例子:test庫下的vast大表進行手工分片 1. 激活數據庫分片功能 ```sh mongo --port 38030 admin > db.runCommand( { enablesharding : "test" } ) ``` 2. 創建索引 ```sh use test > db.vast.ensureIndex( { id: 1 } ) ``` 3. 開啟分片 ```sh use admin > sh.shardcollection("test.vast", {id: 1} } ) ``` 4. 集合分片驗證 ```sh admin> use test test> for(i=1;i<500000;i++){ db.vast.insert({"id":i,"name":"shenzheng","age":70,"date":new Date()}); } test> db.vast.stats() ``` 5. 分片結果測試 ```sh shard1: mongo --port 38034 db.vast.count(); shard2: mongo --port 38037 db.vast.count(); ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看