mongod默認啟動不加任何參數時,是沒有身份認證的,任何人都可以登錄上進行任何操作
啟動時添加--auth可以使用身份驗證模式
使用mongod -f mongod.conf配置文件啟動時,配置文件的security.authorization為enabled,也是使用身份認證模式
同時使用配置文件若想強制不使用身份驗證則添加--noauth參數
若首次啟動即添加身份認證,因為mongodb的身份只能使用客戶端連接后
use admin
再在admin下使用
db.createUser(canshu)
創建對應用戶
所以,可能會令一個用戶也添加不了。
其實這是多慮的,mongodb添加了Localhost Exception
官方文檔:
The localhost exception allows you to enable authorization before creating the first user in the system. When active, the localhost exception allows connections from the localhost interface to create the first user on theadmin database. The exception applies only when there are no users created in the MongoDB instance.
Changed in version 3.0: The localhost exception changed so that these connections only have access to create the first user on the admin database. In previous versions, connections that gained access using the localhost exception had unrestricted access to the MongoDB instance.
If you use the localhost exception when deploying a new MongoDB system, the first user you create must be in the admin database with privileges to create other users, such as a user with the userAdmin oruserAdminAnyDatabase role. See Enable Client Access Control and Create a User Administrator for more information.
即允許使用localhost(127.0.0.1)的客戶端連接
在系統中沒有用戶時
第一次連接進來產生例外,允許創建一個用戶
(類似於mysql安裝時設置root的密碼)
創建帶權限用戶步驟:
1、use admin;
只有在admin上才能創建高權限賬戶,即用於所有數據庫管理權限的用戶。
2、db.createUser({user:"root",pwd:"root",roles:[{role:"root",db:"admin"}]})
db.createUser({user:"admin",pwd:"admin",roles:[{role:"userAdminAnyDatabase",db:"admin"},{role:"dbAdminAnyDatabase",db:"admin"},{role:"readWriteAnyDatabase",db:"admin"}]})
參數為一個js對象,參考上面。
常用的roles
root:即root權限,可以管理所有的數據庫及用戶,擁有所有角色的所有權限。只能創建在admin上
userAdminAnyDatabase:擁有所有數據庫的用戶管理權限
dbAdminAnyDatabase:擁有所有數據庫的數據庫管理權限
readWriteAnyDatabase:擁有所有數據庫的讀寫權限
這些角色都只能創建在admin數據庫上。
去掉AnyDatabase后可以創建在普通數據庫上。
一旦執行過一次createUser,Localhost Exception立即消失,只能通過驗證后才能進行創建操作
db.auth("root","root")。
3、修改一個用戶
db.updateUser("admin",{roles:[{role:"userAdminAnyDatabase",db:"admin"},{role:"dbAdminAnyDatabase",db:"admin"},{role:"readWriteAnyDatabase",db:"admin"}]})
4、刪除一個用戶
db.dropUser("admin")
同時,若想開啟http接口訪問功能,則不能使用身份驗證模式
雖然在使用驗證模式時,訪問http接口會彈出輸入用戶名和密碼的窗口(http默認支持身份認證),但是其實mongodb不支持這種身份驗證。
官方說明:
While MongoDB Enterprise does support Kerberos authentication, Kerberos is not supported in HTTP status interface in any version of MongoDB.
Changed in version 3.0.
Neither the HTTP status interface nor the REST API support the SCRAM-SHA-1 challenge-response user authentication mechanism introduced in version 3.0.
SCRAM-SHA-1 is the default mechanism for MongoDB versions beginning with the 3.0 series. However, if you are upgrading a MongoDB 2.6 instances that already have users credentials, MongoDB will continue to use MONGODB-CR for challenge-response authentication until you upgrade the authentication schema. Even when using the MONGODB-CR authentication mechanism, clients and drivers that support MongoDB 3.0 features (see Driver Compatibility Changes) will use the SCRAM communication protocol.
由此可見,不支持,只能關閉身份認證再訪問了。
http://docs.mongodb.org/manual/core/authentication/#authentication-scram-sha-1
注意:
1,mongodb是沒有默認管理員賬號,所以要先添加管理員賬號,在開啟權限認證。
2,切換到admin數據庫,添加的賬號才是管理員賬號。
3,用戶只能在用戶所在數據庫登錄,包括管理員賬號。
4,管理員可以管理所有數據庫,但是不能直接管理其他數據庫,要先在admin數據庫認證后才可以。這一點比較怪