MongoDB 自動分區 auto sharding

來源:互聯網
上載者:User

MongoDB部署實驗系列文章,MongoDB做為NoSQL資料庫,最近幾年持續升溫,越來越多的企業都開始嘗試用MongoDB代替原有Database做一些事情。MongoDB也在叢集,分區,複製上也有相當不錯的的表現。我通過將做各種MongoDB的部署實驗進行介紹。

第三篇 MongoDB 自動分區 auto sharding,分為6個部分
 1.初始設定檔案目錄
 2.啟動shard節點
 3.配置shard節點
 4.插入資料分區實驗
 5.刪除主分區
 6.重設主分區
 
系統內容介紹:
 
Ubuntu 12.04. LTS 64bit Server
 
1. 初始設定檔案目錄
 
建立目錄
 config1,config2,config3是配置節點
 shard1,shard2,shard3是分區節點
 

~ pwd
 /home/conan/dbs
 ~ mkdir config1 config2 config3 shard1 shard2 shard3
 conan@u1:~/dbs$ ls -l
 drwxrwxr-x 3 conan conan 4096 May 31 11:27 config1
 drwxrwxr-x 3 conan conan 4096 May 31 11:27 config2
 drwxrwxr-x 3 conan conan 4096 May 31 11:27 config3
 drwxrwxr-x 3 conan conan 4096 May 31 11:28 shard1
 drwxrwxr-x 3 conan conan 4096 May 31 11:29 shard2
 drwxrwxr-x 3 conan conan 4096 May 31 11:29 shard3
 

2. 啟動shard節點
 


啟動config節點
 
~ mongod --dbpath /home/conan/dbs/config1 --port 20001 --nojournal --fork --logpath /home/conan/dbs/config1.log
 ~ mongod --dbpath /home/conan/dbs/config2 --port 20002 --nojournal --fork --logpath /home/conan/dbs/config2.log
 ~ mongod --dbpath /home/conan/dbs/config3 --port 20003 --nojournal --fork --logpath /home/conan/dbs/config3.log
 
啟動mongos節點
 
~ mongos --configdb localhost:20001,localhost:20002,localhost:20003 --port 30001 --fork --logpath /home/conan/dbs/mongos1.log
 ~ mongos --configdb localhost:20001,localhost:20002,localhost:20003 --port 30002 --fork --logpath /home/conan/dbs/mongos2.log
 
啟動shard節點
 
~ mongod --dbpath /home/conan/dbs/shard1 --port 10001 --nojournal --fork --logpath /home/conan/dbs/shard1.log
 ~ mongod --dbpath /home/conan/dbs/shard2 --port 10002 --nojournal --fork --logpath /home/conan/dbs/shard2.log
 ~ mongod --dbpath /home/conan/dbs/shard3 --port 10003 --nojournal --fork --logpath /home/conan/dbs/shard3.log
 
查看連接埠
 
~ netstat -nlt
 Active Internet connections (only servers)
 Proto Recv-Q Send-Q Local Address Foreign Address State
 tcp 0 0 0.0.0.0:21003 0.0.0.0:* LISTEN
 tcp 0 0 0.0.0.0:10001 0.0.0.0:* LISTEN
 tcp 0 0 0.0.0.0:30001 0.0.0.0:* LISTEN
 tcp 0 0 0.0.0.0:10002 0.0.0.0:* LISTEN
 tcp 0 0 0.0.0.0:30002 0.0.0.0:* LISTEN
 tcp 0 0 0.0.0.0:10003 0.0.0.0:* LISTEN
 tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
 tcp 0 0 0.0.0.0:11001 0.0.0.0:* LISTEN
 tcp 0 0 0.0.0.0:31001 0.0.0.0:* LISTEN
 tcp 0 0 0.0.0.0:11002 0.0.0.0:* LISTEN
 tcp 0 0 0.0.0.0:31002 0.0.0.0:* LISTEN
 tcp 0 0 0.0.0.0:11003 0.0.0.0:* LISTEN
 tcp 0 0 0.0.0.0:20001 0.0.0.0:* LISTEN
 tcp 0 0 0.0.0.0:20002 0.0.0.0:* LISTEN
 tcp 0 0 0.0.0.0:20003 0.0.0.0:* LISTEN
 tcp 0 0 0.0.0.0:21001 0.0.0.0:* LISTEN
 tcp 0 0 0.0.0.0:21002 0.0.0.0:* LISTEN
 tcp6 0 0 :::22 :::* LISTEN
 

3. 配置shard節點
 
串連mongos1,在mongos中添加分區:
 
~ mongo localhost:30001/admin
 MongoDB shell version: 2.4.3
 connecting to: localhost:30001/admin
 
mongos> db.runCommand({addshard : "localhost:10001", allowLocal : true})
 { "shardAdded" : "shard0000", "ok" : 1 }
 mongos> db.runCommand({addshard : "localhost:10002", allowLocal : true})
 { "shardAdded" : "shard0001", "ok" : 1 }
 mongos> db.runCommand({addshard : "localhost:10003", allowLocal : true})
 { "shardAdded" : "shard0002", "ok" : 1 }
 


錯誤的文法:(MongoDB2.0.4已經不支援此文法)
 
mongos> db.runCommand({addshard : "shard1/localhost:10001,localhost:10002",name:"s1", allowLocal : true})
 {
 "ok" : 0,
 "errmsg" : "couldn't connect to new shard socket exception [CONNECT_ERROR] for shard1/localhost:10001,localhost:10002"
 }
 

查看分區資訊
 
mongos> db.runCommand({listshards:1})
 {
 "shards" : [
 {
 "_id" : "shard0000",
 "host" : "localhost:10001"
 },
 {
 "_id" : "shard0001",
 "host" : "localhost:10002"
 },
 {
 "_id" : "shard0002",
 "host" : "localhost:10003"
 }
 ],
 "ok" : 1
 }
 
啟用資料庫分區:fensme
 
mongos> db.runCommand({"enablesharding" : "fensme"})
 { "ok" : 1 }
 

註:一旦enable了個資料庫,mongos將會把資料庫裡的不同資料集放在不同的分區上。只有資料集也被分區,否則一個資料集的所有資料將放在一個分區上。
 
啟用資料集分區:fensme.users
 
mongos> db.runCommand({"shardcollection" : "fensme.users", "key" : {"_id" : 1,"uid":1}})
 { "collectionsharded" : "fensme.users", "ok" : 1 }
 

查看分區狀態
 
mongos> db.printShardingStatus()
 --- Sharding Status ---
 sharding version: {
 "_id" : 1,
 "version" : 3,
 "minCompatibleVersion" : 3,
 "currentVersion" : 4,
 "clusterId" : ObjectId("51a8d3287034310ad2f6a94e")
 }
 shards:
 { "_id" : "shard0000", "host" : "localhost:10001" }
 { "_id" : "shard0001", "host" : "localhost:10002" }
 { "_id" : "shard0002", "host" : "localhost:10003" }
 databases:
 { "_id" : "admin", "partitioned" : false, "primary" : "config" }
 { "_id" : "fensme", "partitioned" : true, "primary" : "shard0000" }
 fensme.users
 shard key: { "_id" : 1, "uid" : 1 }
 chunks:
 shard0000 1
 { "_id" : { "$minKey" : 1 }, "uid" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 }, "uid" : { "$maxKey" : 1 } } on : shard0000 { "t" : 1, "i" : 0 }
 
fensme資料庫是支援shard的,主shard是shard0000,對應host是localhost:10001

MongoDB 的詳細介紹:請點這裡
MongoDB 的:請點這裡

MongoDB備份與恢複

CentOS編譯安裝MongoDB

CentOS 編譯安裝 MongoDB與mongoDB的php擴充

CentOS 6 使用 yum 安裝MongoDB及伺服器端配置

Ubuntu 13.04下安裝MongoDB2.4.3

如何在MongoDB中建立新資料庫和集合

MongoDB入門必讀(概念與實戰並重)

《MongoDB 權威指南》(MongoDB: The Definitive Guide)英文文字版[PDF]

  • 1
  • 2
  • 下一頁

聯繫我們

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