MongoDB:使用者認證

edagarli發表於2015-01-28
    MongoDB 安裝後預設不啟用認證,也就是說在本地可以通過 mongo 命令不輸入使用者名稱密碼,
直接登陸到資料庫,下面介紹下啟用 mongodb 使用者認證,詳細如下:

    啟用 mongodb 認證只需要在啟動 mongod 服務時配置 auth 引數成 'true'即可可 ,在配置
引數前先新增超級使用者。

疑問:
一。在開啟--auth之後,如果需要新增新資料庫怎麼操作?
1.先關閉原來的服務,然後使用不含--auth的方式登入。此時就可以以匿名的方式登入了!
2.登入後建立資料庫,然後給資料庫中新增使用者。

二。開啟--auth之後,怎樣登入mongo shell?

  1. mongo admin -uroot -p  

具體內容參考幫助文件:

  1. mongo -help  


一 啟用認證
--1.1 增加管理使用者

  1.  > use admin;  
  2. switched to db admin  
  3.   
  4. > db.addUser('root','123456');  
  5. {  
  6.         "user" : "root",  
  7.         "readOnly" : false,  
  8.         "pwd" : "34e5772aa66b703a319641d42a47d696",  
  9.         "_id" : ObjectId("50ad456a0b12589bdc45cf92")  
  10. }  
  11.   
  12. > db.system.users.find();  
  13. { "_id" : ObjectId("50ad6ecda579c47efacf811b"), "user" : "root", "readOnly" : false, "pwd" : "34e5772aa66b703a319641d42a47d696" }  



   備註:在 admin 庫中增加的使用者為超級使用者,許可權最大,可以訪問所有庫。


--1.2 增加普通使用者

  1.  > use skytf;  
  2. switched to db skytf  
  3.   
  4. > db.addUser('skytf','skytf');  
  5. {  
  6.         "user" : "skytf",  
  7.         "readOnly" : false,  
  8.         "pwd" : "8c438fc9e2031577cea03806db0ee137",  
  9.         "_id" : ObjectId("50ad45dd0b12589bdc45cf93")  
  10. }  
  11.   
  12. > db.system.users.find();  
  13. { "_id" : ObjectId("50ad6ef3a579c47efacf811c"), "user" : "skytf", "readOnly" : false, "pwd" : "8c438fc9e2031577cea03806db0ee137" }  

--1.3 配置 auth 引數
vim /database/mongodb/data/mongodb_27017.conf,增加 " auth = true ” 引數

 fork = true
bind_ip = 127.0.0.1
port = 27017
quiet = true
dbpath = /database/mongodb/data/
logpath = /var/applog/mongo_log/mongo.log
logappend = true
journal = true
auth = true

    備註:增加 “auth = true” 配置。


--1.4 重啟 mongodb
  1.  [mongo@redhatB data]$ ps -ef | grep mongo  
  2. root     10887 10859  0 04:47 pts/0    00:00:00 su - mongo  
  3. mongo    10889 10887  0 04:47 pts/0    00:00:00 -bash  
  4. root     10984 10964  0 04:53 pts/1    00:00:00 su - mongo  
  5. mongo    10986 10984  0 04:53 pts/1    00:00:00 -bash  
  6. mongo    12749     1  0 07:54 ?        00:00:01 mongod -f /database/mongodb/data/mongodb_27017.conf  
  7. mongo    13035 10986 13 08:21 pts/1    00:00:00 ps -ef  
  8. mongo    13036 10986  0 08:21 pts/1    00:00:00 grep mongo  
  9.   
  10. [mongo@redhatB data]$ kill 12749  
  11.   
  12. [mongo@redhatB data]$ mongod -f /database/mongodb/data/mongodb_27017.conf  
  13. forked process: 13042  
  14. all output going to: /var/applog/mongo_log/mongo.log  


   


--1.5 測試 skytf 帳號

  1.  [mongo@redhatB data]$ mongo 127.0.0.1/skytf -u skytf -p  
  2. MongoDB shell version: 2.2.1  
  3. Enter password:  
  4. connecting to: 127.0.0.1/skytf  
  5. Error: { errmsg: "auth fails", ok: 0.0 }  
  6. Thu Nov 22 08:23:11 uncaught exception: login failed  
  7. exception: login failed  
  8.   
  9. [mongo@redhatB data]$ mongo 127.0.0.1/skytf -u skytf -p  
  10. MongoDB shell version: 2.2.1  
  11. Enter password:  
  12. connecting to: 127.0.0.1/skytf  
  13.   
  14. > show collections;  
  15. system.indexes  
  16. system.users  
  17. test_1  
  18. test_2  
  19. test_3  
  20. test_4  
  21. things  
  22. things_1  
  23.   
  24. > db.test_5.find();  
  25. { "_id" : ObjectId("50ad7177d114dcf18a8bb220"), "id" : 1 }  
  26.   
  27. > show dbs;  
  28. Thu Nov 22 08:24:03 uncaught exception: listDatabases failed:{ "errmsg" : "need to login", "ok" : 0 }  
  29.   
  30. > use test;  
  31. switched to db test  
  32.   
  33. > show collections;  
  34. Thu Nov 22 09:01:32 uncaught exception: error: {  
  35.         "$err" : "unauthorized db:test ns:test.system.namespaces lock type:0 client:127.0.0.1",  
  36.         "code" : 10057  

   
    備註:從上看出, skytf 使用者的認證已生效,並且能檢視資料庫 skytf 裡的集合,但不能執行 “show dbs”
              命令;並且能連線資料庫 test ,但沒有許可權執行“show collections” 命令。

 

二 切換使用者
--2.1 在普通庫中切換成 root 使用者

  1.  > use test;  
  2. switched to db test  
  3.   
  4. > db.auth('root','123456');db.auth('root','123456');  
  5. Error: { errmsg: "auth fails", ok: 0.0 }  
  6. 0  



      備註:在普通庫中切換成超級使用者失敗,超級使用者需要在 admin 庫中切換才能生效。
   

--2.2 在 admin 庫中切換成 root 使用者

  1.  > use admin;  
  2. switched to db admin  
  3.   
  4.   
  5. > db.auth('root','123456');  
  6. 1  


      備註:在 admin 庫中切換成超級使用者成功。
  


三 新增只讀帳號
--3.1 增加只讀帳號

  1.  > db.addUser('skytf_select','skytf_select',true);  
  2. {  
  3.         "user" : "skytf_select",  
  4.         "readOnly" : true,  
  5.         "pwd" : "e344f93a69f20ca9f3dfbc40da4a3082",  
  6.         "_id" : ObjectId("50ad71c7d114dcf18a8bb221")  
  7. }  
  8.   
  9. > db.system.users.find();db.system.users.find();  
  10. { "_id" : ObjectId("50ad6ef3a579c47efacf811c"), "user" : "skytf", "readOnly" : false, "pwd" : "8c438fc9e2031577cea03806db0ee137" }  
  11. { "_id" : ObjectId("50ad71c7d114dcf18a8bb221"), "user" : "skytf_select", "readOnly" : true, "pwd" : "e344f93a69f20ca9f3dfbc40da4a3082" }  



    備註:只需在 addUser 命令中增加第三個引數,並指定為“true” ,即可建立只讀帳號。   


--3.2 測試

  1.  > db.addUser('skytf_select','skytf_select',true);  
  2. {  
  3.         "user" : "skytf_select",  
  4.         "readOnly" : true,  
  5.         "pwd" : "e344f93a69f20ca9f3dfbc40da4a3082",  
  6.         "_id" : ObjectId("50ad71c7d114dcf18a8bb221")  
  7. }  
  8.   
  9. > db.system.users.find();db.system.users.find();  
  10. { "_id" : ObjectId("50ad6ef3a579c47efacf811c"), "user" : "skytf", "readOnly" : false, "pwd" : "8c438fc9e2031577cea03806db0ee137" }  
  11. { "_id" : ObjectId("50ad71c7d114dcf18a8bb221"), "user" : "skytf_select", "readOnly" : true, "pwd" : "e344f93a69f20ca9f3dfbc40da4a3082" }  


   

    備註:以只讀帳號 skytf_select 登陸庫 skytf,有許可權執行查詢操作,沒有許可權執行插入操作;

 

四 附 命令參考
--4.1 db.addUser

Parameters:     


username (string) – Specifies a new username.
password (string) – Specifies the corresponding password.
readOnly (boolean) – Optional. Restrict a user to read-privileges only. Defaults to false.

Use this function to create new database users, by specifying a username and password as arguments
to the command. If you want to restrict the user to have only read-only privileges, supply a true third
argument; however, this defaults to false。

 

--4.2 db.auth
    Parameters:     


    username (string) – Specifies an existing username with access privileges for this database.
    password (string) – Specifies the corresponding password.

 Allows a user to authenticate to the database from within the shell. Alternatively use mongo
 --username and --password to specify authentication credentials.


五 參考
http://docs.mongodb.org/manual/tutorial/control-access-to-mongodb-with-authentication/
http://docs.mongodb.org/manual/administration/security/
http://blog.163.com/dazuiba_008/blog/static/36334981201110311534143/

相關文章