mongoDB-3.x啓用認證html
官方文檔:mongodb
https://www.mongodb.org/downloads#productionshell
https://docs.mongodb.org/manual/reference/method/js-user-management/數據庫
https://docs.mongodb.org/manual/reference/configuration-options/工具
https://docs.mongodb.org/manual/core/authentication/ui
環境:url
CentOS6.5 x64.net
mongoDB-3.2.0命令行
一.建立管理員用戶htm
在啓用認證前要先建立制受權用戶
use admin
db.createUser(
{
user: "myUserAdmin",
pwd: "abc123",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
二.啓用認證
命令行啓動時加上 --auth
或
配置文件
security:
authorization: enabled
三.認證用戶登陸
mongo --host 192.168.192.10 --port 27017 -u "myUserAdmin" -p "abc123" --authenticationDatabase "admin"
或
mongo shell交互式登陸
注意: 用戶是建在mongo內嵌的admin庫中,驗證時也需指定認證庫
說明: 經過mongo shell命令行認證和登陸都沒問題,但經過第三方gui工具(如robomongo,目前還沒適配3.x)可能會報認證失敗的錯,具體解決方法請參看mongoDB跨平臺圖形管理工具之robomongo
2015-12-30T18:11:20.019+0800 I ACCESS [conn7] Failed to authenticate myUserAdmin@admin with mechanism MONGODB-CR: AuthenticationFailed UserNotFound Could not find user myUserAdmin@admin
我的使用來看,mongoboost仍是挺不錯的
======================
(3)使用用剛纔的超級賬號登陸數據庫(admin)mongo localhost:27017admin -u admin -p admin
如今,咱們就能夠爲其餘數據庫添加用戶了:
好比filedb庫
use filedb
db.addUser("xzsp"," xzsp")
授予這個用戶的權限:(必需要,不然沒法進行讀寫操做)
db.auth("xzsp "," xzsp")
(4)如今能夠用新用戶登陸而且操做filedb數據庫了
3.關閉本地例外登陸方式
一旦擁有了超級管理員,就能夠考慮關閉本地例外方式登陸了
方法以下:
重啓數據庫,啓動時候加上--setParameter enableLocalhostAuthBypass=0便可,這樣登陸的話就必需要用帳戶認證了
4.刪除用戶
刪除用戶要針對某個數據庫進行刪除
> use filedb
switched to db test
> db.removeUser("xzsp")
5.修改用戶密碼
普通用戶只能修改本身的密碼,userAdmin角色賬號能夠修改其餘用戶密碼
例如:
mongo 192.168.69.54:40000/admin -u admin -p admin
use filedb
db.changeUserPassword("xzsp","xzsp@123")
(3)用管理員帳戶登陸,創建新帳戶,讓他能夠讀寫數據庫test
[root@54 ~]# mongo localhost:30000/admin -u superman -p superman
mongos> use test
switched to db test
mongos> db.addUser("test","test")
{
"user" : "test",
"readOnly" : false,
"pwd" : "a6de521abefc2fed4f5876855a3484f5",
"_id" : ObjectId("51fb5d4ecaa5917203f37f63")
}
mongos> db.auth("test","test")
1
(4)用新賬號test登陸,操做數據庫test
[root@54 ~]# mongo localhost:30000/test -u test -p test
MongoDB shell version: 2.4.4
connecting to: localhost:30000/test
> for( var i = 1; i < 100000; i++ ) db.test.insert( { x:i, C_ID:i } );
說明:爲分片集羣啓用認證後,本地例外方式登陸因爲只具有admin數據庫讀寫權限,沒法進行分片操做。對本例來說,添加分片,查看分片狀態等操做都須要用superman賬號登陸才行。執行數據庫test操做用test賬號,這個賬號就是提供給客戶端的賬號。