Mongodb 更多操作

來源:互聯網
上載者:User

mongodb還提供了HTTP查看運行 狀態及restfull的介面
預設的訪問連接埠 是28017
rest的提供者
參考地址:http://www.mongodb.org/display/DOCS/Http+Interface
MongoDB的sharding功能
MongoDB的auto-sharding功能是指mongodb通過mongos自動建立一個水平擴充的資料 庫叢集 系統 ,將資料庫 分表格儲存體在sharding的各個節點上。
一個mongodb叢集包括一些shards(包括一些mongod進程 ),mongos路由進程,一個或多個config伺服器
Shards
每一個shard包括一個或多個服務 和儲存資料的mongod進程(mongod是 MongoDB資料的核心進程)
典型的每個shard開啟多個服務來提高服務的可用性。這些服務/mongod進程在shard中組成一個複製集
Chunks
Chunk是一個來自特殊集合中的一個資料範圍,(collection,minKey,maxKey)描敘一個chunk,它介於minKey和 maxKey範圍之間。
例如chunks 的maxsize大小是100M,如果一個檔案 達到或超過這個範圍時,會被切分到2個新的 chunks中。當一個shard的資料過量時,chunks將會被遷移到其他的shards上。同樣,chunks也可以遷移到其他的shards上
Config Server s
Config伺服器儲存著叢集的metadata資訊,包括每個伺服器,每個shard的基本資料和chunk資訊
Config伺服器主要儲存的是chunk資訊。每一個config伺服器都複製了完整的chunk資訊。
配置:(類比2個shard服務和一個config服務)
Shard1:27020
Shard2:27021
Config:27022
Mongos啟動時預設使用的27017連接埠
建立存放資料的目錄
[falcon@www.fwphp .cn  ~/mongodata]$ mkdir 27020 27021 27022
[falcon@www.fwphp.cn  ~/mongodata]$ ls
27020  27021  27022
[falcon@www.fwphp.cn  ~/mongodb/bin]$ ./mongod --dbpath /home/falcon/mongodata/27020 --port 27020 > /home/falcon/mongodata/27020.log &
[falcon@www.fwphp.cn  ~/mongodb/bin]$ ./mongod --dbpath /home/falcon/mongodata/27021 --port 27021  > /home/falcon/mongodata/27021.log &
[falcon@www.fwphp.cn  ~/mongodb/bin]$ ./mongod --dbpath /home/falcon/mongodata/27022 --port 27022  > /home/falcon/mongodata/27022.log &
啟動mongos時,預設開啟了27017連接埠
[falcon@www.fwphp.cn  ~/mongodb/bin]$ ./mongos --configdb localhost:27022 > /home/falcon/mongodata/config.log &
檢查是否啟動
[falcon@www.fwphp.cn  ~/mongodb/bin]$ ps -ef|grep mongo
falcon    2612     1  0 20:15 ?        00:00:00 ./mongod --dbpath /home/falcon/mongodata/27020 --port 27020
falcon    2619     1  0 20:15 ?        00:00:00 ./mongod --dbpath /home/falcon/mongodata/27021 --port 27021
falcon    2625     1  0 20:15 ?        00:00:00 ./mongod --dbpath /home/falcon/mongodata/27022 --port 27022
falcon    2658     1  0 20:15 ?        00:00:00 ./mongos --configdb localhost:27022
falcon    2804  2772  0 20:31 pts/0    00:00:00 bin/mongo
falcon    2847  2812  0 20:55 pts/2    00:00:00 grep mongo
[falcon@www.fwphp.cn  ~/mongodb/bin]$
[falcon@www.fwphp.cn  ~/mongodb/bin]$ netstat -an  -t
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State     
tcp        0      0 0.0.0.0:10022               0.0.0.0:*                   LISTEN     
tcp        0      0 0.0.0.0:27017               0.0.0.0:*                   LISTEN     
tcp        0      0 0.0.0.0:587                 0.0.0.0:*                   LISTEN     
tcp        0      0 0.0.0.0:27020               0.0.0.0:*                   LISTEN     
tcp        0      0 0.0.0.0:27021               0.0.0.0:*                   LISTEN     
tcp        0      0 0.0.0.0:27022               0.0.0.0:*                   LISTEN     
......  
tcp        0      0 0.0.0.0:28020               0.0.0.0:*                   LISTEN     
tcp        0      0 0.0.0.0:28021               0.0.0.0:*                   LISTEN     
tcp        0      0 0.0.0.0:28022               0.0.0.0:*                   LISTEN     
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN    
........
複製代 碼
看到以上資訊證明mongodb啟動完整,對於開啟的28020、28021、28022是對於的http介面
[falcon@www.fwphp.cn  ~/mongodb/bin]$ ./mongo   預設串連到mongos上
MongoDB shell version: 1.2.4-
url: test
connecting to: test
type "help" for help
> show dbs
admin
config
Local
加 入shard節點
> use admin
switched to db admin
> db.runCommand( { addshard : "localhost:27020", allowLocal : true } )
{"ok" : 1 , "added" : "localhost:27020"}
> db.runCommand( { addshard : "localhost:27021", allowLocal : true } )
{"ok" : 1 , "added" : "localhost:27021"}
> db.runCommand({listshards:1});   查看shard節點列表
{
        "shards" : [
                {
                        "_id" : ObjectId("4b9cd380c33000afad27718e"),
                        "host" : "localhost:27020"
                },
                {
                        "_id" : ObjectId("4b9cd381c33000afad27718f"),
                        "host" : "localhost:27021"
                }
        ],
        "ok" : 1
}
新 建自動切片的庫user001:
> config = connect("localhost:27022")
> config = config.getSisterDB("config")
> user001=db.getSisterDB("user001");
user001
> db.runCommand({enablesharding:"user001"})
{ "ok" : 1 }
> db.printShardingStatus();
--- Sharding Status ---
  sharding version: { "_id" : ObjectId("4b9cd354c33000afad27718d"), "version" : 2 }
  shards:
      { "_id" : ObjectId("4b9cd380c33000afad27718e"), "host" : "localhost:27020" }
      { "_id" : ObjectId("4b9cd381c33000afad27718f"), "host" : "localhost:27021" }
  databases:
        { "name" : "admin", "partitioned" : false, "primary" : "localhost:27022", "_id" : ObjectId("4b9cd3776693dcfa468dec13") }
        { "name" : "user001", "partitioned" : true, "primary" : "localhost:27021", "_id" : ObjectId("4b9cde866693dcfa468dec17") }
                my chunks
我們來在user001中建立表,插入資料
> use user001
switched to db user001
> db.createCollection("user_001")
{ "ok" : 1 }
> show collections
system.indexes
user_001
> db.user_001.insert({uid:1,username:"Falcon.C",sex:"男",age:25});
> db.user_001.find();
{ "_id" : ObjectId("4b9ce1a6c84d7f20576c4df1"), "uid" : 1, "username" : "Falcon.C", "sex" : "男", "age" : 25 }
我們來看看user001庫被分配到了哪個shard 上
[falcon@www.fwphp.cn  ~/mongodata]$ ls -R
.:
27020  27021  27022  mongos.log
./27020:
27020.log  mongod.lock  test.0  test.1  test.ns  _tmp
./27020/_tmp:
./27021:
27021.log  mongod.lock  _tmp  user.0  user001.0  user001.1  user001.ns  user.1  user.ns
./27021/_tmp:
./27022:
27022.log  config.0  config.ns  mongod.lock  mongos.log  _tmp
./27022/_tmp:
[falcon@www.fwphp.cn  ~/mongodata]$
從以上的檔案可以看出,user001被分配到了 27021的shard上了,但是通過mongos路由,我們並感覺不到是資料存放在哪個shard的chunk上
Sharding的管理 命令
> db.$cmd.findOne({isdbgrid:1});                                                                         
{ "isdbgrid" : 1, "hostname" : "http://www.fwphp.cn/", "ok" : 1 }
> db.$cmd.findOne({ismaster:1});
{ "ismaster" : 1, "msg" : "isdbgrid", "ok" : 1 }
> printShardingStatus(db.getSisterDB("config"))
--- Sharding Status ---
  sharding version: { "_id" : ObjectId("4b9cd354c33000afad27718d"), "version" : 2 }
  shards:
      { "_id" : ObjectId("4b9cd380c33000afad27718e"), "host" : "localhost:27020" }
      { "_id" : ObjectId("4b9cd381c33000afad27718f"), "host" : "localhost:27021" }
  databases:
        { "name" : "admin", "partitioned" : false, "primary" : "localhost:27022", "_id" : ObjectId("4b9cd3776693dcfa468dec13") }
                my chunks
        { "name" : "user001", "partitioned" : true, "primary" : "localhost:27021", "_id" : ObjectId("4b9cde866693dcfa468dec17") }
                my chunks
> use admin
switched to db admin
> db.runCommand({netstat:1})
{ "configserver" : "localhost:27022", "isdbgrid" : 1, "ok" : 1 }
>
參 考資訊:http://www.mongodb.org/display/DOCS/Sharding
MongoDB資料庫的MapReduce簡單操作
MongoDB也簡單的實現了MapReduce的功能來提供分布式的資料 查詢服務 ,MapReduce的分布是功能主要用在 Shard上
db.runCommand(
{ mapreduce : <collection>,
   map : <mapfunction>,
   reduce : <reducefunction>
   [, query : <query filter object>]
   [, sort : <sort the query.  useful for optimization>]
   [, limit : <number of objects to return from collection>]
   [, out : <output-collection name>]
   [, keeptemp: <true|false>]
   [, finalize : <finalizefunction>]
   [, scope : <object where fields go into javascript global scope >]
   [, verbose : true]
}
);
下 面是對MapReduce的簡單測試
此例子來源於:http://www.mongodb.org/display/DOCS/MapReduce
> db.things.insert({_id:1,tags:['dog','cat']});                                                                                   
> db.things.insert({_id:2,tags:['cat']});     
> db.things.insert({_id:3,tags:['mouse','cat','dog']});
> db.things.insert({_id:4,tags:[]});                 
> m = function(){
... this.tags.forEach(
...   function(z){     
...             emit(z,{count:1});
...   }
...  );
};
function () {
    this.tags.forEach(function (z) {emit(z, {count:1});});
}
> r=function(key,values){
... var total = 0;
... for(var i=0;i<values.length;i++)
... total += values[i].count;
... return {count:total};
... };
function (key, values) {
    var total = 0;
    for (var i = 0; i < values.length; i++) {
        total += values[i].count;
    }
    return {count:total};
}
> res=db.things.mapReduce(m,r);
{
        "result" : "tmp.mr.mapreduce_1268577545_1",
        "timeMillis" : 25,
        "counts" : {
                "input" : 4,
                "emit" : 6,
                "output" : 3
        },
        "ok" : 1,
        "ok" : 1,
}
> res
{
        "result" : "tmp.mr.mapreduce_1268577545_1",
        "timeMillis" : 25,
        "counts" : {
                "input" : 4,
                "emit" : 6,
                "output" : 3
        },
        "ok" : 1,
        "ok" : 1,
}
> db[res.result].find()
{ "_id" : "cat", "value" : { "count" : 3 } }
{ "_id" : "dog", "value" : { "count" : 2 } }
{ "_id" : "mouse", "value" : { "count" : 1 } }
> db[res.result].drop()
true
> db[res.result].find()
>
以 下有幾個MapReduce的參考例子:
http://www.mongodb.org/display/DOCS/MapReduce
http://github.com/mongodb/mongo/ ... sts/mr_bigobject.js
http://github.com/mongodb/mongo/blob/master/jstests/mr5.js
http://github.com/mongodb/mongo/blob/master/jstests/mr4.js
http://github.com/mongodb/mongo/blob/master/jstests/mr3.js
http://github.com/mongodb/mongo/blob/master/jstests/mr2.js
http://github.com/mongodb/mongo/blob/master/jstests/mr1.js

相關文章

聯繫我們

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