mongodb使用者管理

來源:互聯網
上載者:User

標籤:集合   change   cti   添加   create   參數   版本   ada   讀寫權限   

角色具體說明:
Read:允許使用者讀取指定資料庫
readWrite:允許使用者讀寫指定資料庫
dbAdmin:允許使用者在指定資料庫中執行管理函數,如索引建立、刪除,查看統計或訪問system.profile
userAdmin:允許使用者向system.users集合寫入,可以找指定資料庫裡建立、刪除和系統管理使用者
dbOwner:資料庫擁有者,包含readWrite、dbAdmin、userAdmin
clusterAdmin:只在admin資料庫中可用,賦予使用者所有分區和複製集相關函數的系統管理權限
readAnyDatabase:只在admin資料庫中可用,賦予使用者所有資料庫的讀許可權
readWriteAnyDatabase:只在admin資料庫中可用,賦予使用者所有資料庫的讀寫權限
userAdminAnyDatabase:只在admin資料庫中可用,賦予使用者所有資料庫的userAdmin許可權
dbAdminAnyDatabase:只在admin資料庫中可用,賦予使用者所有資料庫的dbAdmin許可權
root:只在admin資料庫中可用。超級帳號,超級許可權
__system: 內部角色,不建議使用



建立一個不受訪問限制的超級使用者

use admin
db.createUser(
{
   user:"root",
   pwd:"www.ttlsa.com",
   roles:["root"]
}
)




建立一個超級使用者

超級使用者的role有兩種,userAdmin和userAdminAnyDatabase
use admin
db.createUser(
{
    user: "admin",
    pwd: "www.ttlsa.com",
    roles:[
        {
             role: "userAdminAnyDatabase",
             db: "admin"
        }]
}
)


db是指定資料庫的名字,admin是管理資料庫。


建立普通使用者

use admin
db.createUser(
{
    user:"dev",
    pwd:"www.ttlsa.com",
    roles:[
        {role:"read",db:"db01"},
        {role:"read",db:"db02"},
        {role:"read",db:"db03"}
    ]
}
)

    
刪除使用者

首先進入目標庫:use test
db.dropUser("testuser")
db.system.users.remove({user:"testuser"});
db.removeUser("testuser");


查看目前使用者許可權

db.runCommand(
{
   usersInfo:"userName",
   showPrivileges:true
}
)

    


只能查看當前資料庫中的使用者,哪怕當前資料庫admin資料庫,也只能查看admin資料庫中建立的使用者。

修改使用者密碼

use admin
db.changeUserPassword("username", "xxx")
 


查看使用者資訊

db.runCommand({usersInfo:"userName"})
db.system.users.find()
 


修改使用者密碼和使用者資訊

db.runCommand(
{
    updateUser:"username",
    pwd:"xxx",
    customData:{title:"xxx"}
}
)
 

添加角色

首先進入目標庫:use test

db.grantRolesToUser(  "testuser",  [    { role: "read",db:"admin"}  ] )    



回收角色許可權

首先進入目標庫:use test

db.revokeRolesFromUser("testuser",[    { role: "read",db:"admin"}  ] )



搭建認證環境和認證登入

1、找到mongodb設定檔,設定noauth=true

重啟Mongodb後,登入admin帳號,建立一個超級許可權使用者

use admin

db.createUser({user:‘root‘,pwd:‘root‘,roles:[{ "role" : "root", "db" : "admin" }]});


2、關閉mongodb

3、啟用認證參數

要保證許可權認證生效,需要在mongodb設定檔中加入auth=true,同時取消掉noauth=true

4、啟動Mongodb

5、認證登入

> use admin
switched to db admin
> db.auth(‘root‘,‘root‘)

1
> use hong
switched to db hong
> show collections;
mycol
system.users
> db.mycol.find();
{ "_id" : ObjectId("56f255b77fa46acddeb4507b"), "name" : "mycol" }



註:
與使用者管理相關的操作基本都要在admin資料庫下運行,要先use admin;
如果在某個單一的資料庫下,那隻能對當前資料庫的許可權進行操作;
db.addUser是老版本的操作,現在版本也還能繼續使用,建立出來的user是帶有root role的超級管理員。

mongodb使用者管理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.