mongoDB-3.x启用认证
2019獨角獸企業重金招聘Python工程師標準>>>
mongoDB-3.x啟用認證
官方文檔:
https://www.mongodb.org/downloads#production
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/
?
環境:
CentOS6.5 x64
mongoDB-3.2.0
?
一.創建管理員用戶
在啟用認證前要先創建制授權用戶
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帳號,這個帳號就是提供給客戶端的帳號。
?
轉載于:https://my.oschina.net/u/1038053/blog/788344
總結
以上是生活随笔為你收集整理的mongoDB-3.x启用认证的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux下使用taskset设置进程c
- 下一篇: Atitit 图像扫描器---基于扫描线