MongoDB 數(shù)據(jù)庫命令

安裝完成后,在瀏覽器中輸入 http://localhost:27017/

It looks like you are trying to access MongoDB over HTTP on the native driver port.

# 登錄客戶端灭翔,MongoDB安裝后無需密碼也無任何用戶炮赦。
> mongo

# 查看數(shù)據(jù)庫
> show dbs;
admin  0.000GB
local  0.000GB

# 查看當前數(shù)據(jù)庫
> db

# 選擇數(shù)據(jù)庫给梅,如果數(shù)據(jù)庫不存在抖剿,將創(chuàng)建一個新的數(shù)據(jù)庫池户, 否則將返回現(xiàn)有的數(shù)據(jù)庫片吊。
> use admin;
 switched to db admin

# 刪除已經(jīng)use的數(shù)據(jù)庫
> db.dropDatabase()

# 創(chuàng)建集合佩谣,默認并不需要創(chuàng)建集合把还,當插入文檔時會自動創(chuàng)建集合。
> db.createCollection(name,options)
- name String 集合名稱
- options Document 指定有關內(nèi)存大小和索引選項(可選)
  - capped Boolean 是否啟用上限集合茸俭,上限集合是一個固定大小的集合吊履,當它大多其最大尺寸會自動覆蓋最老的條目。
  - size number 上限集合字節(jié)的最大尺寸
  - max number 指定上限集合與允許的最大文件數(shù)
  - autoIndexID Boolean 是否自動創(chuàng)建索引_id字段调鬓,默認false艇炎。
  
# 刪除集合
> db.COLLECTION_NAME.drop()

# 查看集合
> show collections;

# 集合插入文檔,若未指定_id參數(shù)腾窝,則默認文檔分配一個唯一的ObjectId缀踪。
> db.COLLECTION_NAME.insert(document)

_id 是一個12byte十六進制數(shù),12個字節(jié)的結構劃分為
_id: ObjectId(4bytes timestamp, 3bytes machine id, 2bytes process, 3bytes incrementer)

# 從集合中查詢所有條目虹脯,結果將以非結構化方式顯示驴娃。
> db.COLLECTION_NAME.find()

# 從集合中查詢所有條目,結果以結構化方式顯示循集。
> db.COLLECTION_NAME.find().pretty()


# 添加管理員
> db.createUser({user:'admin', pwd:'123456', roles:[{role:'userAdminAnyDatabase', db:'admin'}] })

# 查詢admin集合中所有記錄
> db.admin.find()

# 顯示系統(tǒng)用戶
> show users;

# 查看系統(tǒng)已存在的用戶
> db.system.users.find();

# 刪除系統(tǒng)已存在的用戶
> db.system.users.remove({user:'root'});

# 關閉數(shù)據(jù)庫唇敞,僅root角色可操作。
> db.shutdownServer();

# 登錄啟用授權認證
> mongod --auth --dbpath=/home/mongo/data --logpath=/home/log/mongo/mongodb.log

# 在admin下面添加的帳號到admin下面驗證
> use admin;

# 授權認證
> db.auth("root", "root")

權限驗證

MongoDB3.0.3加入SCRAM-SHA-1校驗方式暇榴,需第三方工具配合進行校驗厚棵。
若需關閉認證,需球蓋system.version文檔authSchema版本為3蔼紧,默認初始安裝為5.

> use admin

> db.system.version.find()
{ "_id" : "featureCompatibilityVersion", "version" : "3.4" }
{ "_id" : "authSchema", "currentVersion" : 5 }

# 方式1
> var schema = db.system.version.findOne({"_id":"authSchema"})
> schema.currentVersion = 3
> db.system.version.save(schema)

# 刪除用戶
> db.dropUser("admin")
# 修改版本
> db.system.version.update({'_id':'authSchema'}, {$set, {'currentVersion':3}})
# 重建賬戶
> db.createUser({user:'admin', pwd:'123456', roles:[]})

Authentication failed

使用root權限在指定業(yè)務庫中婆硬,使用命令行創(chuàng)建用戶,項目或第三方工具才能使用賬戶或密碼連接MongoDB指定的數(shù)據(jù)庫奸例。

# 查看數(shù)據(jù)庫級別命令的幫助
> db.help()
DB methods:
    db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [ just calls db.runCommand(...) ]
    db.auth(username, password)
    db.cloneDatabase(fromhost)
    db.commandHelp(name) returns the help for the command
    db.copyDatabase(fromdb, todb, fromhost)
    db.createCollection(name, { size : ..., capped : ..., max : ... } )
    db.createView(name, viewOn, [ { $operator: {...}}, ... ], { viewOptions } )
    db.createUser(userDocument)
    db.currentOp() displays currently executing operations in the db
    db.dropDatabase()
    db.eval() - deprecated
    db.fsyncLock() flush data to disk and lock server for backups
    db.fsyncUnlock() unlocks server following a db.fsyncLock()
    db.getCollection(cname) same as db['cname'] or db.cname
    db.getCollectionInfos([filter]) - returns a list that contains the names and options of the db's collections
    db.getCollectionNames()
    db.getLastError() - just returns the err msg string
    db.getLastErrorObj() - return full status object
    db.getLogComponents()
    db.getMongo() get the server connection object
    db.getMongo().setSlaveOk() allow queries on a replication slave server
    db.getPrevError()
    db.getProfilingLevel() - deprecated
    db.getProfilingStatus() - returns if profiling is on and slow threshold
    db.getReplicationInfo()
    db.getSiblingDB(name) get the db at the same server as this one
    db.getWriteConcern() - returns the write concern used for any operations on this db, inherited from server object if set
    db.hostInfo() get details about the server's host
    db.isMaster() check replica primary status
    db.killOp(opid) kills the current operation in the db
    db.listCommands() lists all the db commands
    db.loadServerScripts() loads all the scripts in db.system.js
    db.logout()
    db.printCollectionStats()
    db.printReplicationInfo()
    db.printShardingStatus()
    db.printSlaveReplicationInfo()
    db.dropUser(username)
    db.repairDatabase()
    db.resetError()
    db.runCommand(cmdObj) run a database command.  if cmdObj is a string, turns it into { cmdObj : 1 }
    db.serverStatus()
    db.setLogLevel(level,<component>)
    db.setProfilingLevel(level,<slowms>) 0=off 1=slow 2=all
    db.setWriteConcern( <write concern doc> ) - sets the write concern for writes to the db
    db.unsetWriteConcern( <write concern doc> ) - unsets the write concern for writes to the db
    db.setVerboseShell(flag) display extra information in shell output
    db.shutdownServer()
# 獲取當前數(shù)據(jù)庫名稱
> db.getName()
test

> db
test
# 獲取當前數(shù)據(jù)庫版本
> db.version()
3.4.10
# 獲取當前數(shù)據(jù)庫信息
> db.stats()
{
    "db" : "test",
    "collections" : 1,
    "views" : 0,
    "objects" : 2,
    "avgObjSize" : 57.5,
    "dataSize" : 115.0,
    "storageSize" : 32768.0,
    "numExtents" : 0,
    "indexes" : 1,
    "indexSize" : 32768.0,
    "ok" : 1.0
}
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末彬犯,一起剝皮案震驚了整個濱河市向楼,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌谐区,老刑警劉巖湖蜕,帶你破解...
    沈念sama閱讀 219,490評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異宋列,居然都是意外死亡昭抒,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評論 3 395
  • 文/潘曉璐 我一進店門炼杖,熙熙樓的掌柜王于貴愁眉苦臉地迎上來灭返,“玉大人,你說我怎么就攤上這事坤邪∥鹾” “怎么了?”我有些...
    開封第一講書人閱讀 165,830評論 0 356
  • 文/不壞的土叔 我叫張陵艇纺,是天一觀的道長怎静。 經(jīng)常有香客問我,道長黔衡,這世上最難降的妖魔是什么蚓聘? 我笑而不...
    開封第一講書人閱讀 58,957評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮员帮,結果婚禮上或粮,老公的妹妹穿的比我還像新娘。我一直安慰自己捞高,他們只是感情好,可當我...
    茶點故事閱讀 67,974評論 6 393
  • 文/花漫 我一把揭開白布渣锦。 她就那樣靜靜地躺著硝岗,像睡著了一般。 火紅的嫁衣襯著肌膚如雪袋毙。 梳的紋絲不亂的頭發(fā)上型檀,一...
    開封第一講書人閱讀 51,754評論 1 307
  • 那天,我揣著相機與錄音听盖,去河邊找鬼胀溺。 笑死,一個胖子當著我的面吹牛皆看,可吹牛的內(nèi)容都是我干的仓坞。 我是一名探鬼主播,決...
    沈念sama閱讀 40,464評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼腰吟,長吁一口氣:“原來是場噩夢啊……” “哼无埃!你這毒婦竟也來了徙瓶?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤嫉称,失蹤者是張志新(化名)和其女友劉穎侦镇,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體织阅,經(jīng)...
    沈念sama閱讀 45,847評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡壳繁,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,995評論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了荔棉。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片闹炉。...
    茶點故事閱讀 40,137評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖江耀,靈堂內(nèi)的尸體忽然破棺而出剩胁,到底是詐尸還是另有隱情,我是刑警寧澤祥国,帶...
    沈念sama閱讀 35,819評論 5 346
  • 正文 年R本政府宣布昵观,位于F島的核電站,受9級特大地震影響舌稀,放射性物質發(fā)生泄漏啊犬。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,482評論 3 331
  • 文/蒙蒙 一壁查、第九天 我趴在偏房一處隱蔽的房頂上張望觉至。 院中可真熱鬧,春花似錦睡腿、人聲如沸语御。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽应闯。三九已至,卻和暖如春挂捻,著一層夾襖步出監(jiān)牢的瞬間碉纺,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評論 1 272
  • 我被黑心中介騙來泰國打工刻撒, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留骨田,地道東北人。 一個月前我還...
    沈念sama閱讀 48,409評論 3 373
  • 正文 我出身青樓声怔,卻偏偏與公主長得像态贤,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子捧搞,可洞房花燭夜當晚...
    茶點故事閱讀 45,086評論 2 355

推薦閱讀更多精彩內(nèi)容