【正文】
實(shí)際很可能數(shù)據(jù)庫數(shù)據(jù)損壞或數(shù)據(jù)狀態(tài)不一致,導(dǎo)致無法正常啟動(dòng)MongoDB服務(wù)器,根據(jù)啟動(dòng)信息可以看到需要進(jìn)行修復(fù)?;蛘?,可以直接kill掉mongod進(jìn)程即可?;蛘撸部梢孕薷?etc/,設(shè)置auth=true,重啟mongod進(jìn)程。認(rèn)證成功顯示結(jié)果:[plain] view plaincopy{ user : admin, readOnly : false, pwd : 995d2143e0bf79cba24b58b3e41852cd } 如果是認(rèn)證用戶,執(zhí)行某些命令,可以看到正確執(zhí)行結(jié)果,如下所示:[plain] view plaincopy() { _id : ObjectId(4ef940a13c1fc4613425acc8), user : admin, readOnly : false, pwd : 995d2143e0bf79cba24b58b3e41852cd } 否則,認(rèn)證失敗,則執(zhí)行相關(guān)命令會(huì)提示錯(cuò)誤:[plain] view plaincopy() error: { $err : unauthorized db:admin lock type:1 client:, code : 10057 } 為數(shù)據(jù)庫寫數(shù)據(jù)(同步到磁盤)加鎖({fsync:1,lock:1})說明:該操作已經(jīng)對數(shù)據(jù)庫上鎖,不允許執(zhí)行寫數(shù)據(jù)操作,一般在執(zhí)行數(shù)據(jù)庫備份時(shí)有用。(五)數(shù)據(jù)備份、恢復(fù)與遷移管理備份全部數(shù)據(jù)庫mkdir testbakcd testbakmongodump說明:默認(rèn)備份目錄及數(shù)據(jù)文件格式為./dump/[databasename]/[collectionname].bson備份指定數(shù)據(jù)庫mongodump d pagedb說明:備份數(shù)據(jù)庫pagedb中的數(shù)據(jù)?;謴?fù)全部數(shù)據(jù)庫cd testbakmongorestore drop說明:將備份的所有數(shù)據(jù)庫恢復(fù)到數(shù)據(jù)庫,drop指定恢復(fù)數(shù)據(jù)之前刪除原來數(shù)據(jù)庫數(shù)據(jù),否則會(huì)造成回復(fù)后的數(shù)據(jù)中數(shù)據(jù)重復(fù)。恢復(fù)某個(gè)數(shù)據(jù)庫的某個(gè)集合的數(shù)據(jù)cd testbakmongorestore d pagedb c page drop說明:將備份的pagedb的的page集合的數(shù)據(jù)恢復(fù)到數(shù)據(jù)庫。需要注意的是,使用headerline選項(xiàng)時(shí),只支持csv和tsv文件。 if not specified stdin is used drop drop collection first headerline CSV,TSV only use first line as headers upsert insert or update objects that already exist upsertFields arg maseparated fields for the query part of the upsert. You should make sure this is indexed stopOnError stop importing at first error rather than continuing jsonArray load a json array, not one item per line. Currently limited to 4MB. 從向MongoDB導(dǎo)出數(shù)據(jù)mongoexport d pagedb c page q {} f _id,title,url,spiderName,pubDate csv 說明:,其中各選項(xiàng)含義:f 指定cvs列名為_id,title,url,spiderName,pubDateq 指定查詢條件其他各個(gè)選項(xiàng)的使用,可以查看幫助:[plain] view plaincopymongoexport help options: help produce help message v [ verbose ] be more verbose (include multiple times for more verbosity . vvvvv) h [ host ] arg mongo host to connect to ( set name/s1,s2 for sets) port arg server port. Can also use host hostname:port ipv6 enable IPv6 support (disabled by default) u [ username ] arg username p [ password ] arg password dbpath arg directly access mongod database files in the given path, instead of connecting to a mongod server needs to lock the data directory, so cannot be used if a mongod is currently accessing the same path directoryperdb if dbpath specified, each db is in a separate directory d [ db ] arg database to use c [ collection ] arg collection to use (some mands) f [ fields ] arg ma separated list of field names . f name,age fieldFile arg file with fields names 1 per line q [ query ] arg query filter, as a JSON string csv export to csv instead of json o [ out ] arg output file。{spiderName: mafengwoSpider}39。t connect to mongod on startup no 39。 arg expected quiet be less chatty port arg port to connect to host arg server to connect to eval arg evaluate javascript u [ username ] arg username for authentication p [ password ] arg password for authentication h [ help ] show this usage information version show version information verbose increase verbosity ipv6 enable IPv6 support (disabled by default) 基于MongoDB支持的javascript實(shí)現(xiàn)遠(yuǎn)程連接當(dāng)你已經(jīng)連接到一個(gè)遠(yuǎn)程的MongoDB數(shù)據(jù)庫服務(wù)器(例如,),現(xiàn)在想要在這個(gè)會(huì)話中連接另一個(gè)遠(yuǎn)程的數(shù)據(jù)庫服務(wù)器(),可以執(zhí)行如下命令:[plain] view plaincopy var x = new Mongo(39。) var ydb = (39。)。如果啟用了安全認(rèn)證模式,可以在獲取數(shù)據(jù)庫連接實(shí)例時(shí),指定認(rèn)證賬號(hào),例如:[plain] view plaincopy var x = new Mongo(39。) var ydb = (39。, 39。,