モンゴDBの紹介と関連操作(6)

31421 ワード

モンスターDB管理ツール
http://www.mongodb.org/display/DOCS/Admin+UIS
 
MongoDBのsharrding機能において、Shard Serverは実際のデータブロックを記憶するために使用され、実際の生産環境の中の一つのshard server役は何台かのマシングループで一つのrelica setを負担して、本体のシングルポイントの故障を防止します。
Replica Setsはn個のMongodノードを使用して、オートフォールト転移、自動回復(aut-recovery)を備えた高い利用可能なスキームを構築する。通常は3つのmongodのインスタンス、または2 mongod+1アービターのプログラムを使用します。 
 
(1)まず必要なMongodノードを起動します。注意replSetパラメータを使ってSets Nameを指定します。
 
sudo./mongod--fork--logpath/dev/null--dbpath/var/mongodb/0--port 27017--replSet mmset forked process:1166 all outpputgoing to:/dev/null$sudo./mogod--fork--logpath/detpath/dettttttggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg// null$sudo./mongod--fork--logpath/dev/null--dbpath/var/mongodb/2--port 27019--replSet myst forked process:1180 all output going to:/dev/null
   
 
dbpath 数据文件路径
 port 端口
logpath log路径,与 Logging 有关的参数除了 --logpath,还有 --logappend 和 --verbose。
   默认情况下,Logging 是覆盖模式(overwrite),通过 --logappend 可以添加模式记录日志。
    参数 --verbose 设置记录等级,相当于 -v,更多的级别包括 -vv 直到 -vvvvv。与之相对的是 --    quiet,生成最少的日志信息。
replSet 后面是replSet的名字
rest 参数打开 RESTful 操作
以 Daemon 方式运行,需要同时使用 --fork、--logpath 参数。
 
--directoryperdb 参数让系统为每个 DB 创建一个独立子目录。
--config test.conf 如果嫌命令行参数太长,可以考虑使用配置文件。
(2) 使用 mongo 配置 Replica Sets。

$ ./mongo > cfg = { _id: "myset", members: [ ... { _id:0, host:"localhost:27017" }, ... { _id:1, host:"localhost:27018" }, ... { _id:2, host:"localhost:27019" } ... ]} > rs.initiate(cfg) > rs.conf()

 
 
  相关配置数据保存在 local 数据库中oplog.rs记录,oplog.rs 是一个固定长度的 capped collection,用于记录 Replica Sets 操作日志。

> show dbs > use local > show collections oplog.rs slaves system.indexes system.replset > db.system.replset.find()

 
  (3) 可以用 isMaster 和 status 命令查看 Replica Sets 状态。

> rs.isMaster() { "ismaster" : true, "secondary" : false, "hosts" : [ "localhost:27017", "localhost:27019", "localhost:27018" ], "ok" : 1 } > rs.status() { "set" : "myset", "date" : "Sat Aug 21 2010 15:21:13 GMT+0800 (CST)", "myState" : 1, "members" : [ { "_id" : 0, "name" : "localhost:27017", "health" : 1, "state" : 1, "self" : true }, { "_id" : 1, "name" : "localhost:27018", "health" : 1, "state" : 2, "uptime" : 280, "lastHeartbeat" : "Sat Aug 21 2010 15:21:11 GMT+0800 (CST)" }, { "_id" : 2, "name" : "localhost:27019", "health" : 1, "state" : 2, "uptime" : 284, "lastHeartbeat" : "Sat Aug 21 2010 15:21:11 GMT+0800 (CST)" } ], "ok" : 1 }

 

 在同一时刻,每组 Replica Sets 只有一个 Primary,用于接受写操作。而后会异步复制到其他成员数据库中。一旦 primary 死掉,会自动投票选出接任的 primary 来,原服务器恢复后成为普通成员。如果数据尚未从先前的 primary 复制到成员服务器,有可能会丢失数据。

 

(5) 我们还可以在运行时添加成员。必须连接到 primary 才能添加成员

$ ./mongo localhost:27017 > rs.add("localhost:27020") > rs.conf() > rs.status() --查看状态

 

 

 

sharding version: { "_id" : 1, "version" : 3 }
  shards:
      { "_id" : "shard0000", "host" : "192.168.37.4:27020" }
      { "_id" : "shard0001", "host" : "192.168.37.4:27021" }
  databases:
        { "_id" : "admin", "partitioned" : false, "primary" : "config" }
        { "_id" : "test_db1", "partitioned" : true, "primary" : "shard0000" }
                test_db1.shardcollect chunks:
                                shard0001       8
                                shard0000       6
                        { "id" : { $minKey : 1 } } -->> { "id" : 0 } on : shard0001 { "t" : 6000, "i" : 1 }
                        { "id" : 0 } -->> { "id" : 70081 } on : shard0000 { "t": 5000, "i" : 1 }
                        { "id" : 70081 } -->> { "id" : 130186 } on : shard0001 { "t" : 3000, "i" : 2 }
                        { "id" : 130186 } -->> { "id" : 190374 } on : shard0001{ "t" : 3000, "i" : 4 }
                        { "id" : 190374 } -->> { "id" : 250531 } on : shard0000{ "t" : 4000, "i" : 2 }
                        { "id" : 250531 } -->> { "id" : 310680 } on : shard0000{ "t" : 4000, "i" : 4 }
                        { "id" : 310680 } -->> { "id" : 370846 } on : shard0000{ "t" : 4000, "i" : 6 }
                        { "id" : 370846 } -->> { "id" : 431019 } on : shard0000{ "t" : 4000, "i" : 8 }
                        { "id" : 431019 } -->> { "id" : 491216 } on : shard0001{ "t" : 5000, "i" : 2 }
                        { "id" : 491216 } -->> { "id" : 611561 } on : shard0001{ "t" : 5000, "i" : 4 }
                        { "id" : 611561 } -->> { "id" : 731941 } on : shard0001{ "t" : 5000, "i" : 6 }
                        { "id" : 731941 } -->> { "id" : 852261 } on : shard0001{ "t" : 5000, "i" : 8 }
                        { "id" : 852261 } -->> { "id" : 972562 } on : shard0001{ "t" : 5000, "i" : 10 }
                        { "id" : 972562 } -->> { "id" : { $maxKey : 1 } } on : shard0000 { "t" : 6000, "i" : 0 }
 
shard0001       8 trunkshard0000       6 trunk
  id  shardtrunk   sharding id   
 
 
     
> use admin;
> db.runCommand("shutdown"); // shut down the database
  
> db._adminCommand("shutdown");
> db.shutdownServer()
 
     
    mongod     --journal  
 
Replica sets
    2 shardService   arbiter   3 shardService
     mongosmongos        shardService
        Masteroplog   
・         一つのマシンを再起動するには5分かかります。
・         再起動すると、mongodはoplogとmasterのoplogの違いを比較します。
・         mongod 判断あたご5分
・         mongod masterのoplogsを順次処理します。masterと統一するまで。
oplog    capped collection,       ,         
  oplog        ,     ,             master oplog,        ,   stale   ,                  (1.8.0+oplog
db.printReplicationInfo()      primary stale  oploglogs      ,        ,            ,     。
> db.printReplicationInfo()
configured oplog size:   47.6837158203125MB
log length start to end: 132secs (0.04hrs)
oplog first event time:  Wed Apr 13 2011 02:58:08 GMT-0400
oplog last event time:   Wed Apr 13 2011 03:00:20 GMT-0400
now:                     Wed Apr 13 2011 14:09:08 GMT-0400
 
     —oplogSize      
             oplogSize
> use local
> db.oplog.rs.stats()
 
1.     データを削除します。dbpathとサブディレクトリが含まれています。再起動したら自動的に同期します。データベースがよければ、これは遅くなります。
2.     他のマシンからデータファイルをコピーして、一番簡単なのはstop他のマシンです。ファイルをコピーして、再起動します。EBSまたはSANを使っている場合は、fsync and lockが使用できます。 
 
      Set nodeノード
    
$ mongod --replSet foo
      
rs.add("broadway:27017");
—fastsync  
$ mongod --replSet foo –fastsync
 
      Set arbiterノード
rs.addArb("broadway:27017");
 
      
master
    
・         他のノードはマスターを見ても到達できません。
・         このノードはアービターではありません。
・         このノードの優先度がグループ内の他のすべての資格のあるノードより高いか等しいです。
 
            
・         > cfg=rs.co nf()
・         > cfg.members[0].votes=2
・         > rs.reconfig(cfg)
 
            
・         Shutdownオンラインのmongod
・         バックアップ、削除 local.* のデータファイル
・         replSetの再登録、新しいノードの追加など、対応する操作を行う。
 
 
 
 
 
モンスタースター
   mongostat –help              
Fields
   inserts      - # of inserts per second
   query        - # of queries per second
   update       - # of updates per second
   delete       - # of deletes per second
   getmore      - # of get mores (cursor batch) per second
   command      - # of commands per second
   flushes      - # of fsync flushes per second
   mapped       - amount of data mmaped (total data size) megabytes
   visze        - virtual size of process in megabytes
   res          - resident size of process in megabytes
   faults       - # of pages faults per sec (linux only)
   locked       - percent of time in global write lock
   idx miss     - percent of btree page misses (sampled)
   qr|qw        - queue lengths for clients waiting (read|write)
   ar|aw        - active clients (read|write)
   netIn        - network traffic in - bits
   netOut       - network traffic out - bits
   conn         - number of open connections