主頁 > 知識庫 > mongodb基礎(chǔ)之用戶權(quán)限管理實例教程

mongodb基礎(chǔ)之用戶權(quán)限管理實例教程

熱門標(biāo)簽:蘋果汽車租賃店地圖標(biāo)注 老虎洗衣店地圖標(biāo)注 杭州人工電銷機器人價格 濟南電銷機器人加盟公司 怎么投訴地圖標(biāo)注 電銷機器人是什么軟件 云南外呼系統(tǒng) 呼和浩特電銷外呼系統(tǒng)加盟 廣州長安公司怎樣申請400電話

前言

本文主要介紹了mongodb用戶權(quán)限管理的相關(guān)內(nèi)容,關(guān)于接著上次實踐的部分,下面話不多說了,來一起看看詳細的介紹吧

啟動mongodb并連接

./bin/mongod -f conf/mongod.conf
./bin/mongo 127.0.0.1:12345

查看默認的數(shù)據(jù)庫情況

> show dbs
admin 0.000GB
local 0.000GB

> use admin
switched to db admin
> show tables
system.version

可以看到,目前數(shù)據(jù)庫里除了一些基本信息,什么都沒有

在創(chuàng)建設(shè)置用戶權(quán)限之前,先了解一下文檔知識

創(chuàng)建用戶

# demo
db.createUser(
 {
 user: "reportsUser",
 pwd: "12345678",
 roles: [
  { role: "read", db: "reporting" },
  { role: "read", db: "products" },
  { role: "read", db: "sales" },
  { role: "readWrite", db: "accounts" }
 ]
 }
)

數(shù)據(jù)庫內(nèi)建角色

數(shù)據(jù)庫用戶角色

  • read (讀取指定數(shù)據(jù)庫)
  • readWrite (讀寫指定數(shù)據(jù)庫)

數(shù)據(jù)庫管理角色

  • dbAdmin (數(shù)據(jù)庫管理員)
  • dbOwner (數(shù)據(jù)庫所有者,合并了 readWrite, dbAdmin and userAdmin roles.)
  • userAdmin (用戶管理員,可以找指定數(shù)據(jù)庫里創(chuàng)建、刪除和管理用戶)

集群管理角色

  • clusterAdmin (集群管理員)
  • clusterManager (集群管理者)
  • clusterMonitor (集合監(jiān)視者)
  • hostManager (主機管理者)

備份恢復(fù)角色

  • backup (備份)
  • restore (還原)

所有數(shù)據(jù)庫角色

  • readAnyDatabase (讀任何數(shù)據(jù)庫)
  • readWriteAnyDatabase (讀寫任何數(shù)據(jù)庫)
  • userAdminAnyDatabase (用戶管理任何數(shù)據(jù)庫)
  • dbAdminAnyDatabase (任意數(shù)據(jù)庫管理員)

超級用戶角色

  • root

內(nèi)部角色

  • __system

有了創(chuàng)建語法,和參數(shù)說明,接下來開始實踐.

注意,還有一點,賬號是跟著數(shù)據(jù)庫綁定的,在那個庫里授權(quán),就在那個庫里驗證(auth)
否則會失敗

創(chuàng)建 賬號管理授權(quán)權(quán)限 的賬號

> db.createUser(
... {
... user: 'admin',
... pwd: '123456',
... roles: [{role: 'userAdminAnyDatabase', db: 'admin'}]
... }
... )
Successfully added user: {
  "user" : "admin",
  "roles" : [
    {
      "role" : "userAdminAnyDatabase",
      "db" : "admin"
    }
  ]
}

然后退出數(shù)據(jù)庫

> use admin
switched to db admin
> db.shutdownServer()

重新啟動mongodb,記得在配置文件mongod.conf里加上 auth = true

./bin/mongod -f conf/mongod.conf
./bin/mongo 127.0.0.1:12345
> show dbs # 沒有驗證,沒有權(quán)限,會出錯
"errmsg" : "not authorized on admin to execute command
> use admin
> db.auth('admin', '123456')
1
# 返回 1 表示授權(quán)成功,0表示失敗
> show dbs #已經(jīng)授權(quán),可以查看了

創(chuàng)建 讀、讀寫權(quán)限的賬戶

> use book
switched to db book
> db.createUser(
... {
... user: 'zhangsan',
... pwd: 'zhangsan',
... roles: [{role: 'read', db: 'book'}]
... }
... )
Successfully added user: {
  "user" : "zhangsan",
  "roles" : [
    {
      "role" : "read",
      "db" : "book"
    }
  ]
}
> db.createUser(
... {
... user: 'lisi',
... pwd: 'lisi',
... roles: [{role: 'readWrite', db: 'book'}]
... }
... )
Successfully added user: {
  "user" : "lisi",
  "roles" : [
    {
      "role" : "readWrite",
      "db" : "book"
    }
  ]
}
> show users
{
  "_id" : "book.lisi",
  "user" : "lisi",
  "db" : "book",
  "roles" : [
    {
      "role" : "readWrite",
      "db" : "book"
    }
  ]
}
{
  "_id" : "book.zhangsan",
  "user" : "zhangsan",
  "db" : "book",
  "roles" : [
    {
      "role" : "read",
      "db" : "book"
    }
  ]
}

然后驗證用戶權(quán)限是否正確

> db.book.insert({book: '小人書'}) # 沒驗證,會出錯
WriteResult({
  "writeError" : {
    "code" : 13,
    "errmsg" : "not authorized on book to execute command { insert: \"book\", docum
ents: [ { _id: ObjectId('5959b56edcc047dfe5c9b336'), book: \"小人書\" } ], ordered: true }"
  }
})
> db.auth('lisi', 'lisi')
1
> db.book.insert({book: '小人書'})
WriteResult({ "nInserted" : 1 })
> db.auth('zhangsan', 'zhangsan') # 用戶切到 zhangsan
1
> db.book.find() # 可以查看
{ "_id" : ObjectId("5959b59fdcc047dfe5c9b337"), "book" : "小人書" }
> db.book.insert({book: '擇天記'}) # 沒有write權(quán)限,會失敗
WriteResult({
  "writeError" : {
    "code" : 13,
    "errmsg" : "not authorized on book to execute command { insert: \"book\", docum
ents: [ { _id: ObjectId('5959b650dcc047dfe5c9b338'), book: \"擇天記\" } ], ordered: true }"
  }
})

創(chuàng)建 root 超級權(quán)限賬號

這個超級權(quán)限包括 授權(quán) 和 操控數(shù)據(jù)庫集合數(shù)據(jù),比較簡單,只需要把role設(shè)置成 root

> use admin
switched to db admin
> db.auth('admin', '123456')
1
> db.createUser(
... {
... user: 'dongsheng',
... pwd: '123456',
... roles: [{role: 'root', db: 'admin'}]
... }
... )
Successfully added user: {
  "user" : "dongsheng",
  "roles" : [
    {
      "role" : "root",
      "db" : "admin"
    }
  ]
}
> db.auth('dongsheng', '123456')
1
> use book
switched to db book
> db.book.insert({book: '笑傲江湖'})
WriteResult({ "nInserted" : 1 })
> db.book.find()
{ "_id" : ObjectId("5959b59fdcc047dfe5c9b337"), "book" : "小人書" }
{ "_id" : ObjectId("5959b7abdcc047dfe5c9b339"), "book" : "笑傲江湖" }

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

您可能感興趣的文章:
  • MongoDB 簡單入門教程(安裝、基本概念、創(chuàng)建用戶)
  • MongoDB數(shù)據(jù)庫用戶角色和權(quán)限管理詳解
  • MongoDB 用戶管理
  • MongoDB在系統(tǒng)數(shù)據(jù)庫local中無法創(chuàng)建用戶的解決辦法
  • Mac下安裝配置mongodb并創(chuàng)建用戶的方法
  • Mongodb 3.2.9開啟用戶權(quán)限認證問題的步驟詳解
  • MongoDB快速入門筆記(七)MongoDB的用戶管理操作
  • MongoDB系列教程(四):設(shè)置用戶訪問權(quán)限
  • Windows下MongoDB配置用戶權(quán)限實例
  • mongodb 添加用戶及權(quán)限設(shè)置詳解
  • MongoDB為用戶設(shè)置訪問權(quán)限
  • MongoDB 用戶相關(guān)操作

標(biāo)簽:自貢 興安盟 泰安 無錫 雞西 廈門 遼陽 玉林

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《mongodb基礎(chǔ)之用戶權(quán)限管理實例教程》,本文關(guān)鍵詞  mongodb,基礎(chǔ),之,用戶,權(quán)限,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《mongodb基礎(chǔ)之用戶權(quán)限管理實例教程》相關(guān)的同類信息!
  • 本頁收集關(guān)于mongodb基礎(chǔ)之用戶權(quán)限管理實例教程的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章