日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

mongodb用户管理简单记录

發布時間:2025/3/19 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mongodb用户管理简单记录 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Mongodb用戶分為三種

1、全局用戶

2、數據庫對應用戶

3、只讀用戶


查看所有的數據庫

>?show?dbs admin??????0.078GB book_blog??0.078GB local??????0.078GB mydb???????0.078GB newdb??????0.078GB test???????0.078GB


查看現有所有的有哪些用戶,要切換到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 >

創建對應數據庫的用戶

>?use?newdb switched?to?db?newdb >?db.addUser("new_user","new123") WARNING:?The?'addUser'?shell?helper?is?DEPRECATED.?Please?use?'createUser'?instead Successfully?added?user:?{?"user"?:?"new_user",?"roles"?:?[?"dbOwner"?]?} >


從以下可以看出,在沒有授權驗證前,是無法訪問的

[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用户管理简单记录的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。