您的当前位置:首页正文

MongoDB的日常维护管理

2024-10-18 来源:威能网

通过使用--eval参数直接执行ti的集合中的数

$./mongo.exe --eval "printjson(db.t1.count())"
MongoDB shell version: 2.0.2
connecting to: test
5

2.使用js文件执行文件内容

$cat t1_count.js
var count = db.t1.count();
printjson('count of t1 is: '+count);

显示为:

 $./mongo t1_count.js
MongoDB shell version: 2.0.2
connecting to: test
"count of t1 is: 5"

Tips:通过--quiet参数屏蔽部分登陆信息,使结果更清晰

$ ./mongo --quiet t1_count.js
"count of t1 is: 5"

进程管理

查看活动进程
> db.currentOp()
>db.$cmd.sys.inprog.findOne() //$cmd调用外部函数

显示如下:

> db.currentOp()
{
 "inprog" : [
 {
 "opid" : 630385, 
 "active" : true,
 "lockType" : "read",
 "waitingForLock" : false,
 "secs_running" : 0,
 "op" : "query",
 "ns" : "test",
 "query" : {
 "count" : "t1",
 "query" : {

 },
 "fields" : {

 }
 },
 "client" : "127.0.0.1:51324",
 "desc" : "conn",
 "threadId" : "0x7f066087f710",
 "connectionId" : 7,
 "numYields" : 0
 }
 ]
}
>

代码解释:

opid:操作进程号
op: 操作类型(query ,update ,etc)
ns: 命名空间(namespace),操作对象
query :显示操作的具体内容
lockType: 锁的类型,指明是写锁还是读锁
结束进程
> db.killOp(630385)
{ "info" : "attempting to kill op" }

我们查看下:

> db.currentOp()
{ "inprog" : [ ] }
>

监控系统的状态和性能

使用serverStatus命令可以获取到运行中的MongoDB服务器统计信息,下面我们来执行命令,查看MongoDB服务器的统计信息(不同平台或不同版本的键会有所不同),代码如下:

> db.runCommand({"serverStatus":1})
{
 "host" : "lindenpatservermongodb01",
 "version" : "2.0.2",
 "process" : "mongod",
 "uptime" : 6003,
 "uptimeEstimate" : 5926,
 "localTime" : ISODate("2012-04-15T11:02:21.795Z"),
 "globalLock" : {
 "totalTime" : 6002811172,
 "lockTime" : 24867,
 "ratio" : 0.000004142559092311891,
 "currentQueue" : {
 "total" : 0,
 "readers" : 0,
 "writers" : 0
 },
 "activeClients" : {
 "total" : 0,
 "readers" : 0,
 "writers" : 0
 }
 },
 "mem" : {
 "bits" : 64,
 "resident" : 52,
 "virtual" : 1175,
 "supported" : true,
 "mapped" : 160,
 "mappedWithJournal" : 320
 },
 "connections" : {
 "current" : 1,
 "available" : 818
 },
 "extra_info" : {
 "note" : "fields vary by platform",
 "heap_usage_bytes" : 341808,
 "page_faults" : 14
 },
 "indexCounters" : {
 "btree" : {
 "accesses" : 1,
 "hits" : 1,
 "misses" : 0,
 "resets" : 0,
 "missRatio" : 0
 }
 },
 "backgroundFlushing" : {
 "flushes" : 100,
 "total_ms" : 13,
 "average_ms" : 0.13,
 "last_ms" : 1,
 "last_finished" : ISODate("2012-04-15T11:02:19.010Z")
 },
 "cursors" : {
 "totalOpen" : 0,
 "clientCursors_size" : 0,
 "timedOut" : 0
 },
 "network" : {
 "bytesIn" : 1729666458,
 "bytesOut" : 1349989344,
 "numRequests" : 21093517
 },
 "opcounters" : {
 "insert" : 5,
 "query" : 8,
 "update" : 0,
 "delete" : 0,
 "getmore" : 0,
 "command" : 21093463
 },
 "asserts" : {
 "regular" : 0,
 "warning" : 0,
 "msg" : 0,
 "user" : 0,
 "rollovers" : 0
 },
 "writeBacksQueued" : false,
 "dur" : {
 "commits" : 30,
 "journaledMB" : 0,
 "writeToDataFilesMB" : 0,
 "compression" : 0,
 "commitsInWriteLock" : 0,
 "earlyCommits" : 0,
 "timeMs" : {
 "dt" : 3073,
 "prepLogBuffer" : 0,
 "writeToJournal" : 0,
 "writeToDataFiles" : 0,
 "remapPrivateView" : 0
 }
 },
 "ok" : 1
}
>
数据导出与导入 mongoexport和mongoinport

使用mongoexport导出数据

先看数据:

> db.t1.find()
{ "_id" : ObjectId("4f8ac746b2764d3c3e2cafbb"), "num" : 1 }
{ "_id" : ObjectId("4f8ac74cb2764d3c3e2cafbc"), "num" : 2 }
{ "_id" : ObjectId("4f8ac74eb2764d3c3e2cafbd"), "num" : 3 }
{ "_id" : ObjectId("4f8ac751b2764d3c3e2cafbe"), "num" : 4 }
{ "_id" : ObjectId("4f8ac755b2764d3c3e2cafbf"), "num" : 5 }
>

使用代码:
./mongoexport.exe -d test -c t1 -o t1.dat
connected to: 127.0.0.1
exported 5 records
[root@mongodb01 /home/mongo/mongodb-2.0.2/bin]$ ls
bsondump mongodump mongoimport mongosniff t1_count.js
mongo mongoexport mongorestore mongostat t1.dat
mongod mongofiles mongos mongotop testfiles.txt
[root@mongodb01 /home/mongo/mongodb-2.0.2/bin]$ cat t1.dat
{ "_id" : ObjectId("4f8ac746b2764d3c3e2cafbb"), "num" : 1 }
{ "_id" : ObjectId("4f8ac74cb2764d3c3e2cafbc"), "num" : 2 }
{ "_id" : ObjectId("4f8ac74eb2764d3c3e2cafbd"), "num" : 3 }
{ "_id" : ObjectId("4f8ac751b2764d3c3e2cafbe"), "num" : 4 }
{ "_id" : ObjectId("4f8ac755b2764d3c3e2cafbf"), "num" : 5 }

./mongoexport.exe -d test -c t1 -o t1.dat 使用参数说明

-d: 指明使用的数据库
-c: 指明导出的集合,这里是t1
-o: 导出的数据名,这里的数据名默认使用相对路径,也可以使用绝对路径。

导出的数据格式的是JSON方式,也可导出csv格式;

导出为CSV格式代码文件如下:

./mongoexport -d test -c t1 -csv -f num -o t1.dat
-csv:指明了要导出的是CSV格式
-f: 指明需要导出的是哪些例子

数据导入 mongoimport

现将ti删除:

> db.t1.drop()
true
> show collections
system.indexes
system.users
>

再导入数据,这里导入的是csv数据:

./mongoimport -d test -c t1 --type csv --headerline -file /home/t1.dat
connected to: 127.0.0.1

看看导入的数据是不是一样:

> db.t1.find()
{ "_id" : ObjectId("4f8ac746b2764d3c3e2cafbb"), "num" : 1 }
{ "_id" : ObjectId("4f8ac74cb2764d3c3e2cafbc"), "num" : 2 }
{ "_id" : ObjectId("4f8ac74eb2764d3c3e2cafbd"), "num" : 3 }
{ "_id" : ObjectId("4f8ac751b2764d3c3e2cafbe"), "num" : 4 }
{ "_id" : ObjectId("4f8ac755b2764d3c3e2cafbf"), "num" : 5 }
>
--type csv 导入的数据格式为CSV,为什么导入CSV格式:CSV对各大主流的数据库支持更良好,而JSON作为轻量级的数据格式,还有些弊端。
--file 指明导入的文件路径

数据备份和恢复

使用 数据备份 mongodump
./mongodump -d test -o /home/dump
-o:表示输出的备份路径,如果没有使用这个选项的话,MongoDB会自动创建dump文件夹并将备份文件放于其内。
使用数据恢复 mongorestore

mongorestore获取mongodump的输出结果,并将备份的数据插入到运行的MongoDB中。

./mongorestore -d test dump/*
connected to: 127.0.0.1
Thu Apr 19 18:16:12 dump/test/system.users.bson
Thu Apr 19 18:16:12 going into namespace [test.system.users]
2 objects found
Thu Apr 19 18:16:12 dump/test/t1.bson
Thu Apr 19 18:16:12 going into namespace [test.t1]
5 objects found
Thu Apr 19 18:16:12 dump/test/system.indexes.bson
Thu Apr 19 18:16:12 going into namespace [test.system.indexes]
Thu Apr 19 18:16:12 { key: { _id: 1 }, ns: "test.system.users", name: "_id_" }
Thu Apr 19 18:16:13 { key: { _id: 1 }, ns: "test.t1", name: "_id_" }
2 objects found
显示全文