启动后能够通过数据库的IP加port号訪问Web形式数据库。
通过使用拂去配置文件的方式启动数据库实例。在bin目录下创建并编辑mongodb.config(名字能够任意) 事例加上 dbpath =/data/db/ 启动时加上 --f 參数,而且指向配置文件就可以。
为什么我们使用Daemon方式?当我们关闭数据库服务的sessionport的时候。MongoDB的服务也随之终止,这样是十分不安全的。通过守护进程的方式,启动就可以。 加入 --fork 參数,这里必须指定存储日志的文件。即为启动 --logpath 參数。 事比例如以下
./mongod.exe --dbpath = D:\MongoDB --logpath = D:\MongoDB\log\mongodb50.log --fork注意:不能使用kill -9 PID 杀死进程。这样可能导致MongoDB数据库损坏。
默认有个admin数据库。在admin.system.users中保存的用户比其它的数据库设置的用户权限更大。
在未加入admin.system.users用户的权限的的情况下。 client无需不论什么认证就能够连接到数据库。而且能够对数据库进行不论什么操作,仅仅有在admin.system.users加入了用户。启动--auth參数才会起作用。
1.建立系统root用户
>db.addUser("root","123456") >db.auth("root","123456")2.建立仅仅读权限用户
>db.addUser("user_reader","1234567",true)加入仅仅读权限的用户仅仅需加入第三个參数,true。
MongoDB不仅能够交互,还能够运行指定的JavaScript文件,运行指定的命令片段,使用Linux Shell。
1.通过eval參数运行指定的语句 查询test库的t1集合的记录有多少:
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 } db.t1.count() 5
通过使用--eval參数直接运行ti的集合中的数
$./mongo.exe --eval "printjson(db.t1.count())" MongoDB shell version: 2.0.2 connecting to: test 52.使用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() { "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.currentOp() { "inprog" : [ ] } >使用serverStatus命令能够获取到执行中的MongoDBserver统计信息。以下我们来执行命令。查看MongoDBserver的统计信息(不同平台或不同版本号的键会有所不同)。代码例如以下:
> 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导出数据 先看数据:
> 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 指明导入的文件路径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版权声明:本文博客原创文章,博客,未经同意,不得转载。
转载于:https://www.cnblogs.com/bhlsheji/p/4622480.html
相关资源:数据结构—成绩单生成器