MongoDB創建用戶

創建超級管理員
創建admin超級管理員用戶
指定用戶的角色和數據庫:
(注意此時添加的用戶都只用於admin數據庫,而非你存儲業務數據的數據庫)
(在cmd中敲多行代碼時,直接敲回車換行,最後以分號首尾)

use admin
switched to db admin
db.createUser(
... {
... user:"admin",
... pwd:"pwd",
... roles:[{role:"userAdminAnyDatabase",db:"admin"}]
... })
Successfully added user: {
"user" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}

user字段,爲新用戶的名字;
pwd字段,用戶的密碼;
cusomData字段,爲任意內容,例如可以爲用戶全名介紹;
roles字段,指定用戶的角色,可以用一個空數組給新用戶設定空角色。在roles字段,可以指定內置角色和用戶定義的角色。
超級用戶的role有兩種,userAdmin或者userAdminAnyDatabase(比前一種多加了對所有數據庫的訪問,僅僅是訪問而已)。
db是指定數據庫的名字,admin是管理數據庫。
不能用admin數據庫中的用戶登錄其他數據庫。注:只能查看當前數據庫中的用戶,哪怕當前數據庫admin數據庫,也只能查看admin數據庫中創建的用戶。

退出mongo shell

exit
bye

刪除某個數據庫

show dbs
admin 0.078GB
local 0.078GB
test 0.078GB
use test
switched to db test
db.dropDatabase()
{ "dropped" : "test", "ok" : 1 }
show dbs
admin 0.078GB
local 0.078GB

顯示集合

show collections
count
mood
test
system.indexes

查詢collection中數據

db.test.find()
{ "_id" : ObjectId("5d3faf819c5201d5d90c60b2"), "name" : "test" }

移除collection中某個數據

db.test.remove({})
WriteResult({ "nRemoved" : 1 })
db.test.find()

刪除某個collection

db.test.drop()
true
show collections
count
mood
system.indexes

連接數據庫
[test@user bin]$ ./mongo localhost:47017
MongoDB shell version: 3.0.6
connecting to: localhost:47017/test
Server has startup warnings:
2019-07-30T10:02:36.997+0800 I CONTOL [initandlisten] WARNING: You are running this process as the root user, which is not recommended.
2019-07-30T10:02:36.997+0800 I CONTOL [initandlisten]
2019-07-30T10:02:36.998+0800 I CONTOL [initandlisten]
2019-07-30T10:02:36.998+0800 I CONTOL [initandlisten]
WARNING: You are running on a NUMA machine.
2019-07-30T10:02:36.998+0800 I CONTOL [initandlisten] We suggest launching mongod like this to avoid performance problems:
2019-07-30T10:02:36.998+0800 I CONTOL [initandlisten]
numactl --interleave=all mongod [other options]
2019-07-30T10:02:36.998+0800 I CONTOL [initandlisten]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章