mongodb用户管理简单记录
生活随笔
收集整理的這篇文章主要介紹了
mongodb用户管理简单记录
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Mongodb用戶分為三種
1、全局用戶
2、數據庫對應用戶
3、只讀用戶
查看所有的數據庫
查看現有所有的有哪些用戶,要切換到admin數據庫中
>?use?admin switched?to?db?admin >?db.system.users.find() {?"_id"?:?"admin.root",?"user"?:?"root",?"db"?:?"admin",?"credentials"?:?{?"MONGODB-CR"?:?"1a0f1c3c3aa1d592f490a2addc559383"?},?"roles"?:?[?{?"role"?:?"root",?"db"?:?"admin"?}?]?} {?"_id"?:?"test.test_user",?"user"?:?"test_user",?"db"?:?"test",?"credentials"?:?{?"MONGODB-CR"?:?"6076b96fc3fe6002c810268702646eec"?},?"roles"?:?[?{?"role"?:?"dbOwner",?"db"?:?"test"?}?]?} {?"_id"?:?"test.read_only",?"user"?:?"read_only",?"db"?:?"test",?"credentials"?:?{?"MONGODB-CR"?:?"f497e180c9dc0655292fee5893c162f1"?},?"roles"?:?[?{?"role"?:?"read",?"db"?:?"test"?}?]?} >創建一個全局用戶global_user密碼為global123
創建全局用戶要切換到admin數據庫中
>?use?admin switched?to?db?admin >?db.addUser("global_user","global123") WARNING:?The?'addUser'?shell?helper?is?DEPRECATED.?Please?use?'createUser'?instead Successfully?added?user:?{?"user"?:?"global_user",?"roles"?:?[?"root"?]?}>?db.system.users.find() {?"_id"?:?"admin.root",?"user"?:?"root",?"db"?:?"admin",?"credentials"?:?{?"MONGODB-CR"?:?"1a0f1c3c3aa1d592f490a2addc559383"?},?"roles"?:?[?{?"role"?:?"root",?"db"?:?"admin"?}?]?} {?"_id"?:?"test.test_user",?"user"?:?"test_user",?"db"?:?"test",?"credentials"?:?{?"MONGODB-CR"?:?"6076b96fc3fe6002c810268702646eec"?},?"roles"?:?[?{?"role"?:?"dbOwner",?"db"?:?"test"?}?]?} {?"_id"?:?"test.read_only",?"user"?:?"read_only",?"db"?:?"test",?"credentials"?:?{?"MONGODB-CR"?:?"f497e180c9dc0655292fee5893c162f1"?},?"roles"?:?[?{?"role"?:?"read",?"db"?:?"test"?}?]?} {?"_id"?:?"admin.global_user",?"user"?:?"global_user",?"db"?:?"admin",?"credentials"?:?{?"MONGODB-CR"?:?"cad9c3ca71940e1e57c49dcca9e36f7a"?},?"roles"?:?[?{?"role"?:?"root",?"db"?:?"admin"?}?]?} >開啟驗證權限
停止mongodb
[root@server?bin]#?/usr/local/mongodb/bin/mongod?--dbpath=/data/mongodb_data/data/?--logpath=/data/mongodb_data/logs/mongodb.log?--auth?--fork重新登入
[root@server?~]#?mongodb MongoDB?shell?version:?2.6.3 connecting?to:?test >?show?dbs 2014-07-23T15:20:16.161+0800?listDatabases?failed:{ "ok"?:?0, "errmsg"?:?"not?authorized?on?admin?to?execute?command?{?listDatabases:?1.0?}", "code"?:?13 }?at?src/mongo/shell/mongo.js:47 >?use?admin switched?to?db?admin >?show?dbs 2014-07-23T15:20:41.848+0800?listDatabases?failed:{ "ok"?:?0, "errmsg"?:?"not?authorized?on?admin?to?execute?command?{?listDatabases:?1.0?}", "code"?:?13 }?at?src/mongo/shell/mongo.js:47 >?db.auth("global_user","global123") 1 >?show?dbs admin??????0.078GB book_blog??0.078GB local??????0.078GB mydb???????0.078GB newdb??????0.078GB test???????0.078GB >創建對應數據庫的用戶
從以下可以看出,在沒有授權驗證前,是無法訪問的
[root@server?~]#?mongodb MongoDB?shell?version:?2.6.3 connecting?to:?test >?show?dbs 2014-07-23T15:28:15.546+0800?listDatabases?failed:{ "ok"?:?0, "errmsg"?:?"not?authorized?on?admin?to?execute?command?{?listDatabases:?1.0?}", "code"?:?13 }?at?src/mongo/shell/mongo.js:47 >?use?admin switched?to?db?admin >?show?dbs 2014-07-23T15:28:24.734+0800?listDatabases?failed:{ "ok"?:?0, "errmsg"?:?"not?authorized?on?admin?to?execute?command?{?listDatabases:?1.0?}", "code"?:?13 }?at?src/mongo/shell/mongo.js:47 >?db.auth("new_user","new123") Error:?18?{?ok:?0.0,?errmsg:?"auth?failed",?code:?18?} 0 >?use?newdb switched?to?db?newdb >?db.auth("new_user","new123")查看現有的有哪些用戶
>?db.auth("global_user","global123") 1 >?show?users { "_id"?:?"admin.root", "user"?:?"root", "db"?:?"admin", "roles"?:?[ { "role"?:?"root", "db"?:?"admin" } ] } { "_id"?:?"admin.global_user", "user"?:?"global_user", "db"?:?"admin", "roles"?:?[ { "role"?:?"root", "db"?:?"admin" } ] } >?
轉載于:https://blog.51cto.com/alwaysyunwei/1457671
總結
以上是生活随笔為你收集整理的mongodb用户管理简单记录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: swift 函数.和匿名函数
- 下一篇: W5100使用中的常见问题