管理(mongodb权威指南第8章)

mongodb的设计理念:尽可能简化系统操作。

  • mongodb就是一个普通的命令行程序,用mongod调用
  • 提供了内置的管理借口和监控功能,易于与第三方监控宝集成
  • 支持基本的、数据库级别的用户认证、包括只读用户、以及独立的管理员权限
  • 多种方式备份mongodb,主要取决于实际的情况是那种

8.1 启动和停止

    管理严在生产环境中部署mongodb的要点

    8.1.1 从命令行启动












http://blog.mongodb.org/post/13




















D:\java\mongodb-win32-i386-2.2.3\bin>mongod --dbpath c:\data\db Wed Mar 20 20:43:19 Wed Mar 20 20:43:19 warning: 32-bit servers don't have journaling enabled by def ault. Please use --journal if you want durability. Wed Mar 20 20:43:19 Wed Mar 20 20:43:19 [initandlisten] MongoDB starting : pid=7944 port=27017 dbpat h=c:\data\db 32-bit host=king-PC Wed Mar 20 20:43:19 [initandlisten] Wed Mar 20 20:43:19 [initandlisten] ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data Wed Mar 20 20:43:19 [initandlisten] **       see 7788967/32-bit-limitations Wed Mar 20 20:43:19 [initandlisten] **       with --journal, the limit is lower Wed Mar 20 20:43:19 [initandlisten] Wed Mar 20 20:43:19 [initandlisten] db version v2.2.3, pdfile version 4.5 Wed Mar 20 20:43:19 [initandlisten] git version: f570771a5d8a3846eb7586eaffcf4c2 f4a96bf08 Wed Mar 20 20:43:19 [initandlisten] build info: windows sys.getwindowsversion(ma jor=6, minor=0, build=6002, platform=2, service_pack='Service Pack 2') BOOST_LIB _VERSION=1_49 Wed Mar 20 20:43:19 [initandlisten] options: { dbpath: "c:\data\db" } Wed Mar 20 20:43:19 [initandlisten] Unable to check for journal files due to: bo ost::filesystem::basic_directory_iterator constructor: ϵͳ�Ҳ���ָ����·����: "c:\d ata\db\journal" Wed Mar 20 20:43:19 [initandlisten] waiting for connections on port 27017 Wed Mar 20 20:43:19 [websvr] admin web console waiting for connections on port 2 8017 Wed Mar 20 20:43:38 [initandlisten] connection accepted from 127.0.0.1:50891 #1 (1 connection now open) Wed Mar 20 20:44:51 [conn1] build index test.foo { _id: 1 } Wed Mar 20 20:44:51 [conn1] build index done.  scanned 0 total records. 0.003 se cs
 D:\java\mongodb-win32-i386-2.2.3\bin>mongod --help
Wed Mar 20 20:42:43
Wed Mar 20 20:42:43 ** NOTE: when using MongoDB 32 bit, you are limited to about
 2 gigabytes of data
Wed Mar 20 20:42:43 **       see http://blog.mongodb.org/post/137788967/32-bit-l
imitations
Wed Mar 20 20:42:43 **       with --journal, the limit is lower
Wed Mar 20 20:42:43
Allowed options:

 
General options:
  -h [ --help ]               show this usage information
  --version                   show version information
  -f [ --config ] arg         configuration file specifying additional options
  -v [ --verbose ]            be more verbose (include multiple times for more
                              verbosity e.g. -vvvvv)
  --quiet                     quieter output
  --port arg                  specify port number - 27017 by default
  --bind_ip arg               comma separated list of ip addresses to listen on
                              - all local ips by default
  --maxConns arg              max number of simultaneous connections - 20000 by
                              default
  --objcheck                  inspect client data for validity on receipt
  --logpath arg               log file to send write to instead of stdout - has
                              to be a file, not directory
  --logappend                 append to logpath instead of over-writing
  --pidfilepath arg           full path to pidfile (if not set, no pidfile is
                              created)
  --keyFile arg               private key for cluster authentication
  --auth                      run with security
  --cpu                       periodically show cpu and iowait utilization
  --dbpath arg                directory for datafiles - defaults to \data\db\
  --diaglog arg               0=off 1=W 2=R 3=both 7=W+some reads
  --directoryperdb            each database will be stored in a separate
                              directory
  --ipv6                      enable IPv6 support (disabled by default)
  --journal                   enable journaling
  --journalCommitInterval arg how often to group/batch commit (ms)
  --journalOptions arg        journal diagnostic options
  --jsonp                     allow JSONP access via http (has security
                              implications)
  --noauth                    run without security
  --nohttpinterface           disable http interface
  --nojournal                 disable journaling (journaling is on by default
                              for 64 bit)
  --noprealloc                disable data file preallocation - will often hurt
                              performance
  --noscripting               disable scripting engine
  --notablescan               do not allow table scans
  --nssize arg (=16)          .ns file size (in MB) for new databases
  --profile arg               0=off 1=slow, 2=all
  --quota                     limits each database to a certain number of files           (8 default)
  --quotaFiles arg            number of files allowed per db, requires --quota
  --repair                    run repair on all dbs
  --repairpath arg            root directory for repair files - defaults to
                              dbpath
  --rest                      turn on simple rest api
  --slowms arg (=100)         value of slow for profile and console log
  --smallfiles                use a smaller default file size
  --syncdelay arg (=60)       seconds between disk syncs (0=never, but not
                              recommended)
  --sysinfo                   print some diagnostic system information
  --upgrade                   upgrade db if needed

Windows Service Control Manager options:
  --install                install Windows service
  --remove                 remove Windows service
  --reinstall              reinstall Windows service (equivalent to --remove
                           followed by --install)
  --serviceName arg        Windows service name
  --serviceDisplayName arg Windows service display name
  --serviceDescription arg Windows service description
  --serviceUser arg        account for service execution
  --servicePassword arg    password used to authenticate serviceUser

Replication options:
  --oplogSize arg       size to use (in MB) for replication op log. default is
                        5% of disk space (i.e. large is good)

Master/slave options:
  --master              master mode
  --slave               slave mode
  --source arg          when slave: specify master as <server:port>
  --only arg            when slave: specify a single database to replicate
  --slavedelay arg      specify delay (in seconds) to be used when applying
                        master ops to slave
  --autoresync          automatically resync if slave data is stale

Replica set options:
  --replSet arg           arg is <setname>[/<optionalseedhostlist>]
  --replIndexPrefetch arg specify index prefetching behavior (if secondary)
                          [none|_id_only|all]

Sharding options:
  --configsvr           declare this is a config db of a cluster; default port
                        27019; default dir /data/configdb
  --shardsvr            declare this is a shard db of a cluster; default port
                        27018
  --noMoveParanoia      turn off paranoid saving of data for moveChunk.  this
                        is on by default for now, but default will switch

  •  --dbpath 指定数据目录
  • --port 指定服务器监听的端口号,默认是27017
  • --fork 一守护进程的方式运行mongodb,创建服务器进程
  • --logpath 置顶日志输出路径   写权限  --logappend
  • --config 指定配置文件,加载命令行位指定的各种选项

  8.1.2 配置文件

    mongodb支持从文件获取配置信息。

  8.1.3 停止mongodb

8.2 监控

  8.2.1 使用管理接口

     默认情况,启动mongodb是会启动一个HTTP服务器,+1000端口

    要想利用好管理接口,需要用--rest选项开启REST支持,也可以在启动mongod是使用--nohttpinterface关闭管理接口

    http://localhost:28017/

 

mongod king-PC

List all commands | Replica set status

Commands: buildInfo cursorInfo features hostInfo isMaster listDatabases replSetGetStatus serverStatus top
db version v2.2.3, pdfile version 4.5
git hash: f570771a5d8a3846eb7586eaffcf4c2f4a96bf08
sys info: windows sys.getwindowsversion(major=6, minor=0, build=6002, platform=2, service_pack='Service Pack 2') BOOST_LIB_VERSION=1_49
uptime: 3734 seconds
overview (only reported if can acquire read lock quickly)
time to get readlock: 0ms
# databases: 2
# Cursors: 0
replication: 
master: 0
slave:  0
clients 
Client OpId Locking Waiting SecsRunning Op Namespace Query client msg progress
DataFileSync 0 { waitingForLock: false } 0 :27017
initandlisten 0 { waitingForLock: false } 2004 test { options.temp: { $in: [ true, 1 ] } } 0.0.0.0:0
clientcursormon 0 { waitingForLock: false } 0 :27017
TTLMonitor 133 { waitingForLock: false } 2004 test.system.indexes { expireAfterSeconds: { $exists: true } } 0.0.0.0:0
snapshotthread 0 { waitingForLock: false } 0 :27017
websvr 0 { waitingForLock: false } 0 :27017
conn1 11 { waitingForLock: false } 2004 { replSetGetStatus: 1.0, forShell: 1.0 } 127.0.0.1:50891
dbtop (occurrences|percent of elapsed)
NS total Reads Writes Queries GetMores Inserts Updates Removes
TOTAL 0 0% 0 0% 0 0% 0 0% 0 0% 0 0% 0 0% 0 0%
write lock % time in write lock, by 4 sec periods
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
write locked now: false Log 
Wed Mar 20 20:43:19 [initandlisten] MongoDB starting : pid=7944 port=27017 dbpath=c:\data\db 32-bit host=king-PC
           20:43:19 [initandlisten] 
           20:43:19 [initandlisten] ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
           20:43:19 [initandlisten] **       see http://blog.mongodb.org/post/137788967/32-bit-limitations 20:43:19 [initandlisten] **       with --journal, the limit is lower
           20:43:19 [initandlisten] 
           20:43:19 [initandlisten] db version v2.2.3, pdfile version 4.5
           20:43:19 [initandlisten] git version: f570771a5d8a3846eb7586eaffcf4c2f4a96bf08
           20:43:19 [initandlisten] build info: windows sys.getwindowsversion(major=6, minor=0, build=6002, platform=2, service_pack='Service Pack 2') BOOST_LIB_VERSION=1_49
           20:43:19 [initandlisten] options: { dbpath: "c:\data\db" }
           20:43:19 [initandlisten] Unable to check for journal files due to: boost::filesystem::basic_directory_iterator constructor: ϵͳ�Ҳ���ָ����·����: "c:\data\db\journal"
           20:43:19 [initandlisten] waiting for connections on port 27017
           20:43:19 [websvr] admin web console waiting for connections on port 28017
           20:43:38 [initandlisten] connection accepted from 127.0.0.1:50891 #1 (1 connection now open)
           20:44:51 [conn1] build index test.foo { _id: 1 }
           20:44:51 [conn1] build index done.  scanned 0 total records. 0.003 secs

  ****不要用驱动程序连接HTTP接口,也不要用HTTP连接本机驱动端口

         驱动端口只能处理本机mongodb传输协议,不能处理HTTP请求

         不能用本机mongodb传输协议去访问管理接口的端口

        http://localhost:27017/

 
You are trying to access MongoDB on the native driver port. For http diagnostic access, add 1000 to the port number

     8.2.2 serverStatus

        要获取运行中的mongodb服务器的统计信息,最基本的工具是serverStatus命令,(不同平台不同版本可能不同)

       

 > db.runCommand({"serverStatus":1})
{
        "host" : "king-PC",
        "version" : "2.2.3",
        "process" : "mongod",
        "pid" : 7944,
        "uptime" : 4421,
        "uptimeMillis" : NumberLong(4421591),
        "uptimeEstimate" : 3097,
        "localTime" : ISODate("2013-03-20T13:57:00.869Z"),
        "locks" : {
                "." : {
                        "timeLockedMicros" : {
                                "R" : NumberLong(20),
                                "W" : NumberLong(66111)
                        },
                        "timeAcquiringMicros" : {
                                "R" : NumberLong(10),
                                "W" : NumberLong(16)
                        }
                },
                "admin" : {
                        "timeLockedMicros" : {

                        },
                        "timeAcquiringMicros" : {

                        }
                },
                "local" : {
                        "timeLockedMicros" : {
                                "r" : NumberLong(14218),
                                "w" : NumberLong(0)
                        },
                        "timeAcquiringMicros" : {
                                "r" : NumberLong(730),
                                "w" : NumberLong(0)
                        }
                },
                "test" : {
                        "timeLockedMicros" : {
                                "r" : NumberLong(50688),
                                "w" : NumberLong(48442)
                        },
                        "timeAcquiringMicros" : {
                                "r" : NumberLong(347),
                                "w" : NumberLong(8)
                        }
                }
        },
        "globalLock" : {
                "totalTime" : NumberLong("4421591000"),
                "lockTime" : NumberLong(66111),
                "currentQueue" : {
                        "total" : 0,
                        "readers" : 0,
                        "writers" : 0
                },
                "activeClients" : {
                        "total" : 0,
                        "readers" : 0,
                        "writers" : 0
                }
        },
        "mem" : {
                "bits" : 32,
                "resident" : 19,
                "virtual" : 147,
                "supported" : true,
                "mapped" : 64
        },
        "connections" : {
                "current" : 1,
                "available" : 19999
        },
        "extra_info" : {
                "note" : "fields vary by platform",
                "page_faults" : 17865,
                "usagePageFileMB" : 33,
                "totalPageFileMB" : 4094,
                "availPageFileMB" : 1402,
                "ramMB" : 2047
        },
        "indexCounters" : {
                "note" : "not supported on this platform"
        },
        "backgroundFlushing" : {
                "flushes" : 73,
                "total_ms" : 4461,
                "average_ms" : 61.10958904109589,
                "last_ms" : 3,
                "last_finished" : ISODate("2013-03-20T13:56:19.906Z")
        },
        "cursors" : {
                "totalOpen" : 0,
                "clientCursors_size" : 0,
                "timedOut" : 0
        },
        "network" : {
                "bytesIn" : 707,
                "bytesOut" : 823,
                "numRequests" : 9
        },
        "opcounters" : {
                "insert" : 1,
                "query" : 147,
                "update" : 0,
                "delete" : 0,
                "getmore" : 0,
                "command" : 9
        },
        "asserts" : {
                "regular" : 0,
                "warning" : 0,
                "msg" : 0,
                "user" : 0,
                "rollovers" : 0
        },
        "writeBacksQueued" : false,
        "recordStats" : {
                "accessesNotInMemory" : 4,
                "pageFaultExceptionsThrown" : 0,
                "local" : {
                        "accessesNotInMemory" : 0,
                        "pageFaultExceptionsThrown" : 0
                },
                "test" : {
                        "accessesNotInMemory" : 4,
                        "pageFaultExceptionsThrown" : 0
                }
        },
        "ok" : 1
}

     原始的统计信息同样可以有HTTP接口一json的形式得到,还有其他一些有用命令的输出

   http://localhost:28017/_status

 
{ "serverStatus" : { "host" : "king-PC", "version" : "2.2.3", "process" : "mongod", "pid" : 7944, "uptime" : 4586, "uptimeMillis" : 4586204, "uptimeEstimate" : 3261, "localTime" : { "$date" : 1363787985482 }, "locks" : { "." : { "timeLockedMicros" : { "R" : 43, "W" : 66111 }, "timeAcquiringMicros" : { "R" : 20, "W" : 16 } }, "admin" : { "timeLockedMicros" : {}, "timeAcquiringMicros" : {} }, "local" : { "timeLockedMicros" : { "r" : 14749, "w" : 0 }, "timeAcquiringMicros" : { "r" : 760, "w" : 0 } }, "test" : { "timeLockedMicros" : { "r" : 50981, "w" : 48442 }, "timeAcquiringMicros" : { "r" : 362, "w" : 8 } } }, "globalLock" : { "totalTime" : 4586204000, "lockTime" : 66111, "currentQueue" : { "total" : 0, "readers" : 0, "writers" : 0 }, "activeClients" : { "total" : 0, "readers" : 0, "writers" : 0 } }, "mem" : { "bits" : 32, "resident" : 18, "virtual" : 147, "supported" : true, "mapped" : 64 }, "connections" : { "current" : 1, "available" : 19999 }, "extra_info" : { "note" : "fields vary by platform", "page_faults" : 17872, "usagePageFileMB" : 33, "totalPageFileMB" : 4094, "availPageFileMB" : 1400, "ramMB" : 2047 }, "indexCounters" : { "note" : "not supported on this platform" }, "backgroundFlushing" : { "flushes" : 76, "total_ms" : 4744, "average_ms" : 62.42105263157895, "last_ms" : 108, "last_finished" : { "$date" : 1363787960012 } }, "cursors" : { "totalOpen" : 0, "clientCursors_size" : 0, "timedOut" : 0 }, "network" : { "bytesIn" : 860, "bytesOut" : 2610, "numRequests" : 11 }, "opcounters" : { "insert" : 1, "query" : 153, "update" : 0, "delete" : 0, "getmore" : 0, "command" : 10 }, "asserts" : { "regular" : 0, "warning" : 0, "msg" : 0, "user" : 0, "rollovers" : 0 }, "writeBacksQueued" : false, "recordStats" : { "accessesNotInMemory" : 4, "pageFaultExceptionsThrown" : 0, "local" : { "accessesNotInMemory" : 0, "pageFaultExceptionsThrown" : 0 }, "test" : { "accessesNotInMemory" : 4, "pageFaultExceptionsThrown" : 0 } } }, "buildinfo" : { "version" : "2.2.3", "gitVersion" : "f570771a5d8a3846eb7586eaffcf4c2f4a96bf08", "sysInfo" : "windows sys.getwindowsversion(major=6, minor=0, build=6002, platform=2, service_pack='Service Pack 2') BOOST_LIB_VERSION=1_49", "versionArray" : [ 2, 2, 3, 0 ], "bits" : 32, "debug" : false, "maxBsonObjectSize" : 16777216 } }

    关于serverStatus的一些参数说明:

 

 

 

  8.2.3 mongostat

     mongodb提供了mongostatus,可以动态便捷的查看serverStatus信息

  8.2.4 第三方插件

8.3 安全和认证

   mongodb支持对单个连接的认证,即便这个认证的权限模式很简陋。

   8.3.1 认证的基础知识

8.4 备份和修复

   8.4.1 数据文件备份

         这个方式实际上就是在数据库关闭时备份数据库目录的文件。

   8.4.2 mongodump和mongorestore

        mongodump提供运行时备份的解决方案,使用普通的查询机制,对运行的mongodb做查询,然后将所有查询到的文档写入磁盘。

       mongorestore恢复

   8.4.3 fsync和锁

   8.4.4 从属备份

   

 

  

 

 

 

 

 

 

    

      

你可能感兴趣的:(管理(mongodb权威指南第8章))