這裏還有另一個答案:Can't connect to MongoDB with authentication enabled。我試過,但仍然可以;弄清楚爲什麼我的配置有什麼問題。無法通過身份驗證連接到mongodb?
我使用Ubuntu 14.04,蒙戈3.4.1(最新)作爲服務安裝,安裝後
首先我運行此命令,就像它的文檔here:
mongo --port 27017
use admin
db.createUser({user: "adminUser",pwd: "abc123",roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]})
返回Successfully added user
。然後我重新配置/etc/mongod.conf
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
net:
port: 27017
bindIp: 127.0.0.1
security:
authorization: enabled
保存並重新啓動mongod的服務器:sudo service mongod restart
嘗試與連接:mongo -u "adminUser" -p "abc123" --authenticationDatabase "admin"
這是全成,那麼如果我改變到另一個數據庫命令use testDatabase
,我無法對其進行任何操作。
use testDatabase
db.createCollection("people")
結果:
{
"ok" : 0,
"errmsg" : "not authorized on testDatabase to execute command { create: \"people\" }",
"code" : 13,
"codeName" : "Unauthorized"
}
這裏登記的用戶在我的數據庫
use admin
db.system.users.find()
{ "_id" : "admin.adminUser",
"user" : "adminUser",
"db" : "admin",
"credentials" : { "SCRAM-SHA-1" : {....} },
"roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ]
}
看來,userAdminAnyDatabase
角色不再工作或者是有什麼錯我的設置?
它的工作原理,但這是否意味着我需要爲不同的數據庫創建不同的用戶? – DennyHiu
這取決於用例。你可以採取任何方式。更多在這裏。 https://docs.mongodb.com/manual/reference/built-in-roles/和https://docs.mongodb.com/manual/tutorial/manage-users-and-roles/ – Veeram