您的位置:首页 > 数据库 > Mongodb

学习mongo系列(十)MongoDB 备份(mongodump)与恢复(mongorerstore) 监控(mongostat mongotop)

2016-01-13 10:48 706 查看
一、备份

  在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:\data\dump,当然该目录需要提前建立,在备份完成后,系统自动在dump目录下建立一个test目录,这个目录里面存放该数据库实例的备份数据。

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

C:\Program Files\MongoDB\Server\3.2\bin>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 Files\MongoDB\Server\3.2\bin>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 Files\MongoDB\Server\3.2\bin>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 "test\ma
xh", skipping...
2016-01-13T10:50:51.796+0800 reading metadata for maxh.user from dump\maxh\us
er.metadata.json
2016-01-13T10:50:51.799+0800 reading metadata for admin.col from dump\admin\c
ol.metadata.json
2016-01-13T10:50:51.801+0800 restoring maxh.user from dump\maxh\user.bson
2016-01-13T10:50:51.803+0800 reading metadata for maxh.maxh from dump\maxh\ma
xh.metadata.json
2016-01-13T10:50:51.805+0800 restoring admin.col from dump\admin\col.bson
2016-01-13T10:50:51.807+0800 restoring maxh.maxh from dump\maxh\maxh.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 dump\admin\system.users.bso
n
2016-01-13T10:50:52.163+0800 done

C:\Program Files\MongoDB\Server\3.2\bin>

三、监控

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

C:\Program Files\MongoDB\Server\3.2\bin>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 Files\MongoDB\Server\3.2\bin>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 Files\MongoDB\Server\3.2\bin>mongotop --locks

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

C:\Program Files\MongoDB\Server\3.2\bin>mongotop --locks
2016-01-13T11:17:15.224+0800 Failed: server does not support reporting lock i
nformation

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



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: