mongoDB之使用者及使用權限設定 轉載 還沒有仔細查看細節東西

來源:互聯網
上載者:User

標籤:

之前用MongoDB沒有設定使用者和許可權,一直都是本機應用程式串連MongoDB。在伺服器上部署後對外沒有開資料庫連接連接埠,本機應用程式串連再開放應用程式連接埠供外部存取。想想很不對勁還是設定下使用者授權吧。

我部署的環境是windows下MongoDB2.4.8版本。首先要開啟安全模式的話,在啟動MongoDB服務的時候就要加上--auth參數,命令如下:

D:\mongodb>mongod.exe --dbpath D:\mongodb\data --logpath=D:\mongodb\logs\mongodb.log --auth

這樣的話再串連MongoDB操作的時候就需要一定的許可權。

一、首先我們需要在沒有加"--auth"許可權參數啟動服務進行使用者權限管理

我們這樣啟動MongoDB服務:

D:\mongodb>mongod.exe --dbpath D:\mongodb\data --logpath=D:\mongodb\logs\mongodb.log

這時不指定使用者串連mongodb,是可以登入並操作的,我的操作如下:

D:\>mongoMongoDB shell version: 2.4.8connecting to: test> show dbsadmin   (empty)local   0.078125GB>

可以看到預設顯示兩個庫,我們需要進入admin庫進行使用權限設定(這個時候因為服務沒有添加許可權參數,所以預設是有許可權進行相關設定的)

> use admin

switched to db admin

> db.addUser(‘sa‘,‘sa‘)

{

"user" : "sa",

"readOnly" : false,

"pwd" : "75692b1d11c072c6c79332e248c4f699",

"_id" : ObjectId("53af835ada88ac42a40917a0")

}

> db.system.users.find()

{ "_id" : ObjectId("53af835ada88ac42a40917a0"), "user" : "sa", "readOnly" : false, "pwd" : "75692b1d11c072c6c79332e248c4f699" }

二、我們關掉之前開啟的服務,添加"--auth"許可權參數,重新啟動MongoDB服務

D:\>mongod --dbpath D:\mongodb\data --logpath=D:\mongodb\logs\mongodb.log --auth

我們再次串連並操作:

D:\>mongo

MongoDB shell version: 2.4.8

connecting to: test

> use admin

switched to db admin

> show collections

Sun Jun 29 11:17:27.103 error: {

"$err" : "not authorized for query on admin.system.namespaces",

"code" : 16550

} at src/mongo/shell/query.js:128

發現如果不加身份資訊預設串連的話,是沒有許可權操作的。我們驗證下之前添加的使用者,再操作試下:

> db.auth(‘sa‘,‘sa‘)1            //返回1表示驗證成功,返回0表示驗證失敗> show collectionssystem.indexessystem.users

發現驗證成功可以對admin庫進行操作了,我們再串連其他庫試試:

D:\>mongoMongoDB shell version: 2.4.8connecting to: test> show collectionsSun Jun 29 11:20:17.996 error: {  "$err" : "not authorized for query on test.system.namespaces",  "code" : 16550} at src/mongo/shell/query.js:128> db.auth(‘sa‘,‘sa‘)Error: 18 { code: 18, ok: 0.0, errmsg: "auth fails" }0

發現這裡驗證失敗了,之前設定的admin使用者驗證不管用。查了資料知道必須先從admin登入再use其他庫才能被驗證通過:

D:\>mongoMongoDB shell version: 2.4.8connecting to: test> use adminswitched to db admin> db.auth(‘sa‘,‘sa‘)1> use testswitched to db test> show collections

三、添加完頂層admin使用者,可以用admin賬戶進行其他使用者的設定

如果想讓單獨的庫有單獨的使用者名稱就得先從admin登入然後設定相應的使用者資訊,具體操作如下:

D:\>mongoMongoDB shell version: 2.4.8connecting to: test> use adminswitched to db admin> db.auth(‘sa‘,‘sa‘)1                                //先從admin登入> use testswitched to db test> show collections> db.addUser(‘test‘,‘test‘) //添加test庫的使用者{        "user" : "test",        "readOnly" : false,        "pwd" : "a6de521abefc2fed4f5876855a3484f5",        "_id" : ObjectId("53af874c5017b6747e68da2a")} 

再次單獨登入test試試:

D:\>mongoMongoDB shell version: 2.4.8connecting to: test> show collections       //未登入沒有許可權Sun Jun 29 11:27:52.899 error: {  "$err" : "not authorized for query on test.system.namespaces",  "code" : 16550} at src/mongo/shell/query.js:128> db.auth(‘test‘,‘test‘)     //之前設定的賬戶1    //登入成功> show collectionssystem.indexessystem.users

四、設定完admin賬戶後開啟帶許可權參數的MongoDB服務在可視化管理器中系統管理使用者許可權

在windows下我使用了MongoVUE可視化管理工具。建立新串連:

之前啟動不帶auth參數的服務時,不添加使用者名稱密碼串連是可以串連的,因為設定了auth參數所以這裡必須添加使用者名稱和密碼;把上面設定的admin使用者sa填入就可以串連上了。 串連上之後開啟一個庫,裡面有個Users設定

這裡可以對某一個庫進行使用者的添加刪除修改等操作,這是最高許可權的admin視圖,如果用test庫的test使用者登入的話,視圖就只能看見test庫了

以上就是windows下使用MongoDB在shell和視覺化檢視中對使用者權限設定的簡單操作方法。

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.