mongoDB 安全許可權存取控制

來源:互聯網
上載者:User

標籤:help   存取控制   問控制   error   gem   alt   show user   安全   find   

MongoDB3.0許可權46564141

轉自:http://ibruce.info/2015/03/03/mongodb3-auth/?utm_source=tuicool

MongoDB3.0許可權,啥都不說了,Google百度出來的全是錯的。先安裝好盲溝,簡單的沒法說。

首先,不使用 —auth 參數,啟動 mongoDB:

mongodb-linux-i686-3.0.0/bin/mongod -f mongodb-linux-i686-3.0.0/mongodb.conf
此時你 show dbs 會看到只有一個local資料庫,那個所謂的admin是不存在的。

mongoDB 沒有炒雞無敵使用者root,只有能系統管理使用者的使用者 userAdminAnyDatabase。

開啟 mongo shell:

mongodb-linux-i686-3.0.0/bin/mongo
添加系統管理使用者:

use admin
db.createUser(
{
user: "buru",
pwd: "12345678",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
roles 中的 db 參數是必須的,不然會報錯:Error: couldn’t add user: Missing expected field “db”。另外,有很多文章記錄的是使用 db.addUser(…) 方法,這個方法是舊版的,3.0中已經不存在,詳見:http://docs.mongodb.org/manual/reference/method/js-user-management。

切換到admin下,查看剛才建立的使用者:

show users

db.system.users.find()
{ "_id" : "admin.buru", "user" : "buru", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "gwVwuA/dXvxgSHavEnlyvA==", "storedKey" : "l2QEVTEujpkCuqDEKqfIWbSv4ms=", "serverKey" : "M1ofNKXg2sNCsFrBJbX4pXbSgvg=" } }, "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }
怎麼關閉 mongoDB?千萬不要 kill -9 pid,可以 kill -2 pid 或 db.shutdownServer()

下面使用 —auth 參 數,重新啟動 mongoDB:

mongodb-linux-i686-3.0.0/bin/mongod --auth -f mongodb-linux-i686-3.0.0/mongodb.conf
再次開啟 mongo shell:

mongodb-linux-i686-3.0.0/bin/mongo
use admin
db.auth("buru","12345678") #認證,返回1表示成功

mongodb-linux-i686-3.0.0/bin/mongo -u buru -p 12345678 --authenticationDatabase admin
此時

show collections
報錯

2015-03-17T10:15:56.011+0800 E QUERY Error: listCollections failed: {
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { listCollections: 1.0 }",
"code" : 13
}
at Error ()
at DB._getCollectionInfosCommand (src/mongo/shell/db.js:643:15)
at DB.getCollectionInfos (src/mongo/shell/db.js:655:20)
at DB.getCollectionNames (src/mongo/shell/db.js:666:17)
at shellHelper.show (src/mongo/shell/utils.js:625:12)
at shellHelper (src/mongo/shell/utils.js:524:36)
at (shellhelp2):1:1 at src/mongo/shell/db.js:643
因為,使用者buru只有使用者管理的許可權。

下面建立使用者,使用者都跟著庫走,建立的使用者都是

use tianhe
db.createUser(
{
user: "bao",
pwd: "12345678",
roles: [
{ role: "readWrite", db: "tianhe" },
{ role: "read", db: "tianhe2" }
]
}
)
查看剛剛建立的使用者。

show users

{
"_id" : "tianhe.bao",
"user" : "bao",
"db" : "tianhe",
"roles" : [
{
"role" : "readWrite",
"db" : "tianhe"
},
{
"role" : "read",
"db" : "tianhe2"
}
]
}
查看整個mongoDB全部的使用者:

use admin
db.system.users.find()

{ "_id" : "admin.buru", "user" : "buru", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "gwVwuA/dXvxgSHavEnlyvA==", "storedKey" : "l2QEVTEujpkCuqDEKqfIWbSv4ms=", "serverKey" : "M1ofNKXg2sNCsFrBJbX4pXbSgvg=" } }, "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }
{ "_id" : "tianhe.bao", "user" : "bao", "db" : "tianhe", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "//xy1V1fbqEHC1gzQqZHGQ==", "storedKey" : "ZS/o54zzl/FdcXLQJ98KdAVTfF0=", "serverKey" : "iIpNYz2Gk8KhyK3zgz6muBt0PI4=" } }, "roles" : [ { "role" : "readWrite", "db" : "tianhe" }, { "role" : "read", "db" : "tianhe2" } ] }
建立完畢,驗證一下:

use buru
show collections

2015-03-17T10:30:06.461+0800 E QUERY Error: listCollections failed: {
"ok" : 0,
"errmsg" : "not authorized on buru to execute command { listCollections: 1.0 }",
"code" : 13
}
at Error ()
at DB._getCollectionInfosCommand (src/mongo/shell/db.js:643:15)
at DB.getCollectionInfos (src/mongo/shell/db.js:655:20)
at DB.getCollectionNames (src/mongo/shell/db.js:666:17)
at shellHelper.show (src/mongo/shell/utils.js:625:12)
at shellHelper (src/mongo/shell/utils.js:524:36)
at (shellhelp2):1:1 at src/mongo/shell/db.js:643
`
顯然沒許可權,先auth:

db.auth("bao","12345678")
1
show collections
news
system.indexes
wahaha
完畢!

參考:
Mongo Shell:http://docs.mongodb.org/v2.2/tutorial/getting-started-with-the-mongo-shell
Enable Access Control:http://docs.mongodb.org/manual/tutorial/enable-authentication
Add a User to a Database:http://docs.mongodb.org/manual/tutorial/add-user-to-database
User Methods:http://docs.mongodb.org/manual/reference/method/js-user-management
Role Methods:http://docs.mongodb.org/manual/reference/method/js-role-management

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.