zoukankan      html  css  js  c++  java
  • 学习mongo系列(十)MongoDB 备份(mongodump)与恢复(mongorerstore) 监控(mongostat mongotop)

    一、备份

      在Mongodb中我们使用mongodump命令来备份MongoDB数据。该命令可以导出所有数据到指定目录中。

      mongodump命令可以通过参数指定导出的数据量级转存的服务器。

        

      mongodump命令脚本语法如下:

      >mongodump -h dbhost -d dbname -o dbdirectory
    • -h:

      MongDB所在服务器地址,例如:127.0.0.1,当然也可以指定端口号:127.0.0.1:27017

    • -d:

      需要备份的数据库实例,例如:test

    • -o:

      备份的数据存放位置,例如:c:datadump,当然该目录需要提前建立,在备份完成后,系统自动在dump目录下建立一个test目录,这个目录里面存放该数据库实例的备份数据。

      示例:在本地使用 27017 启动你的mongod服务。打开命令提示符窗口,进入MongoDB安装目录的bin目录输入命令mongodump:

    C:Program FilesMongoDBServer3.2in>mongodump
    2016-01-13T10:27:25.076+0800 writing admin.system.users to
    2016-01-13T10:27:25.078+0800 done dumping admin.system.users (1 document)
    2016-01-13T10:27:25.078+0800 writing admin.system.version to
    2016-01-13T10:27:25.080+0800 done dumping admin.system.version (1 document)
    2016-01-13T10:27:25.082+0800 writing admin.col to
    2016-01-13T10:27:25.084+0800 writing maxh.maxh to
    2016-01-13T10:27:25.085+0800 writing maxh.user to
    2016-01-13T10:27:25.087+0800 done dumping admin.col (2 documents)
    2016-01-13T10:27:25.088+0800 done dumping maxh.maxh (1 document)
    2016-01-13T10:27:25.090+0800 done dumping maxh.user (6 documents)

    备份指定的数据库集合。

    C:Program FilesMongoDBServer3.2in>mongodump --collection user --db maxh
    2016-01-13T10:41:55.955+0800 writing maxh.user to
    2016-01-13T10:41:55.957+0800 done dumping maxh.user (6 documents)

     

    二、恢复

      示例:在本地使用 27017 启动你的mongod服务。打开命令提示符窗口,进入MongoDB安装目录的bin目录输入命令mongorestore:

    C:Program FilesMongoDBServer3.2in>mongorestore
    2016-01-13T10:50:51.787+0800 using default 'dump' directory
    2016-01-13T10:50:51.790+0800 building a list of dbs and collections to restor
    e from dump dir
    2016-01-13T10:50:51.793+0800 don't know what to do with subdirectory "testma
    xh", skipping...
    2016-01-13T10:50:51.796+0800 reading metadata for maxh.user from dumpmaxhus
    er.metadata.json
    2016-01-13T10:50:51.799+0800 reading metadata for admin.col from dumpadminc
    ol.metadata.json
    2016-01-13T10:50:51.801+0800 restoring maxh.user from dumpmaxhuser.bson
    2016-01-13T10:50:51.803+0800 reading metadata for maxh.maxh from dumpmaxhma
    xh.metadata.json
    2016-01-13T10:50:51.805+0800 restoring admin.col from dumpadmincol.bson
    2016-01-13T10:50:51.807+0800 restoring maxh.maxh from dumpmaxhmaxh.bson
    2016-01-13T10:50:51.818+0800 error: multiple errors in bulk operation:
    - E11000 duplicate key error collection: admin.col index: _id_ dup key: { : Ob
    jectId('5694f184617e63523cfdefe1') }
    - E11000 duplicate key error collection: admin.col index: _id_ dup key: { : Ob
    jectId('5695b2574fe3fe853bfe7c3a') }

    2016-01-13T10:50:51.825+0800 error: multiple errors in bulk operation:
    - E11000 duplicate key error collection: maxh.user index: _id_ dup key: { : Ob
    jectId('56946fba3a18f4867aecbcd1') }
    - E11000 duplicate key error collection: maxh.user index: _id_ dup key: { : Ob
    jectId('5694714a3a18f4867aecbcd2') }
    - E11000 duplicate key error collection: maxh.user index: _id_ dup key: { : Ob
    jectId('5694ce6685cf2632ea951c05') }
    - E11000 duplicate key error collection: maxh.user index: _id_ dup key: { : Ob
    jectId('5694d84c85cf2632ea951c06') }
    - E11000 duplicate key error collection: maxh.user index: _id_ dup key: { : Ob
    jectId('5694dab385cf2632ea951c07') }
    - E11000 duplicate key error collection: maxh.user index: _id_ dup key: { : Ob
    jectId('5694dae885cf2632ea951c08') }

    2016-01-13T10:50:51.838+0800 restoring indexes for collection admin.col from
    metadata
    2016-01-13T10:50:51.841+0800 error: E11000 duplicate key error collection: ma
    xh.maxh index: _id_ dup key: { : ObjectId('569467e73a18f4867aecbcd0') }
    2016-01-13T10:50:51.844+0800 restoring indexes for collection maxh.user from
    metadata
    2016-01-13T10:50:51.847+0800 finished restoring admin.col (2 documents)
    2016-01-13T10:50:51.849+0800 restoring indexes for collection maxh.maxh from
    metadata
    2016-01-13T10:50:51.851+0800 finished restoring maxh.user (6 documents)
    2016-01-13T10:50:51.855+0800 finished restoring maxh.maxh (1 document)
    2016-01-13T10:50:51.857+0800 restoring users from dumpadminsystem.users.bso
    n
    2016-01-13T10:50:52.163+0800 done

    C:Program FilesMongoDBServer3.2in>

     三、监控

      MongoDB中提供了mongostat 和 mongotop 两个命令来监控MongoDB的运行情况。

    C:Program FilesMongoDBServer3.2in>mongostat
    insert query update delete getmore command % dirty % used flushes vsize res q
    r|qw ar|aw netIn netOut conn time
    *0 *0 *0 *0 0 1|0 0.0 0.0 0 141.0M 77.0M
    0|0 0|0 79b 18k 1 2016-01-13T11:14:37+08:00
    *0 *0 *0 *0 0 1|0 0.0 0.0 0 141.0M 77.0M
    0|0 0|0 79b 18k 1 2016-01-13T11:14:38+08:00
    *0 *0 *0 *0 0 1|0 0.0 0.0 0 141.0M 77.0M
    0|0 0|0 79b 18k 1 2016-01-13T11:14:39+08:00
    *0 *0 *0 *0 0 1|0 0.0 0.0 0 141.0M 77.0M
    0|0 0|0 79b 18k 1 2016-01-13T11:14:40+08:00
    *0 *0 *0 *0 0 1|0 0.0 0.0 0 141.0M 77.0M
    0|0 0|0 79b 18k 1 2016-01-13T11:14:41+08:00

      mongotop也是mongodb下的一个内置工具,mongotop提供了一个方法,用来跟踪一个MongoDB的实例,查看哪些大量的时间花费在读取和写入数据。 mongotop提供每个集合的水平的统计数据。默认情况下,mongotop返回值的每一秒。

       后面的10是<sleeptime>参数 ,可以不使用,等待的时间长度,以秒为单位,mongotop等待调用之间。通过的默认mongotop返回数据的每一秒。

    C:Program FilesMongoDBServer3.2in>mongotop 10
    2016-01-13T11:12:18.752+0800 connected to: 127.0.0.1

    ns total read write 2016-01-13T11:12:28+08:00
    admin.col 0ms 0ms 0ms
    admin.system.roles 0ms 0ms 0ms
    admin.system.users 0ms 0ms 0ms
    admin.system.version 0ms 0ms 0ms
    local.startup_log 0ms 0ms 0ms
    local.system.replset 0ms 0ms 0ms
    maxh.maxh 0ms 0ms 0ms
    maxh.user 0ms 0ms 0ms

     

     C:Program FilesMongoDBServer3.2in>mongotop --locks

    报告每个数据库的锁的使用中,使用mongotop - 锁,:

    C:Program FilesMongoDBServer3.2in>mongotop --locks
    2016-01-13T11:17:15.224+0800 Failed: server does not support reporting lock i
    nformation

    抛出一个问题--------------------------------------------------------------

  • 相关阅读:
    EF的连表查询Lambda表达式和linq语句
    C#.NET里面抽象类,接口,虚方法
    ASP.Net WebAPI的返回值
    IHttpActionResult不识别解决办法
    web api 开发之 filter
    SQL语句大全教程
    ASP.NET Web API 跨域访问(CORS)要注意的地方
    铁乐学python_shelve模块详解
    铁乐学python_day25_序列化模块
    铁乐学python_day24_面向对象进阶1_内置方法
  • 原文地址:https://www.cnblogs.com/mxh1099/p/5126528.html
Copyright © 2011-2022 走看看