mongodb 3.4分區複製集配置

來源:互聯網
上載者:User

標籤:分區   慢日誌   soc   預設   min   eal   命名空間   realloc   日誌資訊   

1:啟動三個執行個體
mongod -f /home/mongodb/db27017/mongodb27017.confmongod -f /home/mongodb/db27018/mongodb27018.confmongod -f /home/mongodb/db27019/mongodb27019.conf

設定檔如下:

verbose = true #日誌資訊vvvv = true #日誌的層級logpath=/home/mongodb/db27019/log/mongodb.log #記錄檔logappend=true #設定檔案記錄格式TRUE為追加,false為覆蓋pport = 27019 #指定連接埠號碼maxConns = 2000 #最大連結數,預設情況是根據系統限制最大不超過2Wpidfilepath =/home/mongodb/db27019/tmp/mongo.pid #進程ID,沒有指定則啟動時候就沒有PID檔案nounixsocket = false #當設定為true時,不會產生socket檔案unixSocketPrefix =/home/mongodb/db27019/tmp #通訊端檔案路徑,預設/tmpfork = true #後台啟動並執行守護進程模式dbpath=/home/mongodb/db27019/data #資料存放目錄noprealloc = false #預分配方式來保證寫入效能,true會帶來效能下降nssize = 16 #命名空間預設大小16M,最大2G#慢日誌profile = 1 #0 關。無分析1僅包括慢操作2包括所有操作。slowms = 200 #大於200ms的日誌將會被記錄下來replSet=sharingmxqconfigsvr=true
2:進入一台機器進行初始化:
mongouse adminrs.initiate({_id: "sharingmxq",configsvr: true,members: [{ _id : 0, host : "localhost:27017" },{ _id : 1, host : "localhost:27018" },{ _id : 2, host : "localhost:27019" }]})

3:建立分區的複製集

[[email protected] mongodb]# mkdir -p /home/mongodb/db37017/data[[email protected] mongodb]# mkdir -p /home/mongodb/db37017/tmp[[email protected] mongodb]# mkdir -p /home/mongodb/db37017/log[[email protected] mongodb]# mkdir -p /home/mongodb/db37017/key[[email protected] mongodb]# mkdir -p /home/mongodb/db37018/data[[email protected] mongodb]# mkdir -p /home/mongodb/db37018/tmp[[email protected] mongodb]# mkdir -p /home/mongodb/db37018/log[[email protected] mongodb]# mkdir -p /home/mongodb/db37018/key[[email protected] mongodb]# mkdir -p /home/mongodb/db37019/key[[email protected] mongodb]# mkdir -p /home/mongodb/db37019/log[[email protected] mongodb]# mkdir -p /home/mongodb/db37019/tmp[[email protected] mongodb]# mkdir -p /home/mongodb/db37019/data

啟動:

mongod -f /home/mongodb/db37017/mongodb37017.confmongod -f /home/mongodb/db37018/mongodb37018.confmongod -f /home/mongodb/db37019/mongodb37019.conf

要注意一點,如果是添加分區複製集的話。每一個分區要指定不同的replSet,然後進行初始化。把一個複製集當成是每一個單機節點就好了,但是添加分區的時候必須指定的是master

4:啟動一個mongos執行個體連結配置mongodb資料庫
mongos --configdb "sharingmxq"/localhost:27017,localhost:27018,localhost:27019 --port=30000

然後添加分區節點:

use adminmongos> sh.addShard(‘localhost:37017‘){ "shardAdded" : "shard0000", "ok" : 1 }mongos> sh.addShard(‘localhost:37018‘){ "shardAdded" : "shard0001", "ok" : 1 }mongos> sh.addShard(‘localhost:37019‘){ "shardAdded" : "shard0002", "ok" : 1 }

如果三個分區節點使用的是複製集的話就要用以下的方式添加:

mongos> sh.addShard( "sharingmxq/localhost:37017"){ "shardAdded" : "sharingmxq", "ok" : 1 }mongos> sh.addShard( "sharingmxq/localhost:37018"){ "shardAdded" : "sharingmxq", "ok" : 1 }mongos> sh.addShard( "sharingmxq/localhost:37019"){ "shardAdded" : "sharingmxq", "ok" : 1 }

5:分區的配置

mongos --port=30000為某個資料庫啟動分區sh.enableSharding("maxiangqian")為某張表添加分區:
sh.shardCollection("maxiangqian.maxiangqian", { ‘id‘:1} )sh.shardCollection(‘aedata.ac01_test‘,{‘id‘:1,‘idcard‘:1})

6:驗證分區是否完成

mongos --port=30000use maxiangqianfor (var i = 1; i <= 100000; i++) db.maxiangqian.save({id:i,"test1":"testval1"});

登入各個分區伺服器進行驗證:

99981118已經完成了分區。但是為什麼分布會不均勻呢,因為我們使用的是hash分區,再插入他個九十萬看一下。(要區別hash分區和range分區,hash分區具有易擴充性,但是分布的伺服器卻不好控制)for (var i = 100000; i <= 1000000; i++) db.maxiangqian.save({id:i,"test1":"testval1"});再去驗證一下:328487 --port=37019335552 --port=37018335962 --port=37017

mongodb 3.4分區複製集配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.