安裝完成后,在瀏覽器中輸入 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
}