现在的位置: 首页 > 综合 > 正文

MongoDB权限管理之用户名和密码的操作

2012年01月07日 ⁄ 综合 ⁄ 共 3194字 ⁄ 字号 评论关闭

 

MongoDB权限管理之用户名和密码的操作

MongoDB默认是不需要输入用户名和密码,客户就可以登录的。但是出于安全性的考虑,我们还是要为其设置用户名和密码。本文主要介绍的是MongoDB权限管理之用户名和密码的操作,希望能对您有所帮助。

AD:


    本文我们介绍MongoDB权限管理,主要介绍的是如何设置用户名和密码。接下来我们就一一介绍。

    添加用户的时候必须满足以下两个条件:

    1.有相关权限的情况下(后面会说)。

    2.mongod没有加--auth的情况下(如果加了,你添加权限的话 会出现下面的情况)。

    1. > use admin    
    2.  
    3. switched to db admin    
    4.  
    5. > db.addUser('sa','sa')    
    6.  
    7. Fri Jul 22 14:31:13 uncaught exception: error {    
    8.  
    9. "$err" : "unauthorized db:admin lock type:-1 client:127.0.0.1",    
    10.  
    11. "code" : 10057    
    12.  
    13. }    
    14.  
    15. >    

    所以我们添加用户时必须先在没有加--auth的时候添加个super  admin。

    服务起来后,进入./mongo。

    1. [root@:/usr/local/mongodb/bin]#./mongo    
    2.  
    3. MongoDB shell version: 1.8.2    
    4.  
    5. connecting to: test    
    6.  
    7. > use admin    
    8.  
    9. switched to db admin    
    10.  
    11. > db.adduser('sa','sa')    
    12.  
    13. Fri Jul 22 14:34:24 TypeError: db.adduser is not a function (shell):1    
    14.  
    15. > db.addUser('sa','sa')    
    16.  
    17. {    
    18.  
    19. "_id" : ObjectId("4e2914a585178da4e03a16c3"),    
    20.  
    21. "user" : "sa",    
    22.  
    23. "readOnly" : false,    
    24.  
    25. "pwd" : "75692b1d11c072c6c79332e248c4f699"    
    26.  
    27. }    
    28.  
    29. >    

    这样就说明 已经成功建立了,然后我们试一下权限。

    1. > show collections    
    2.  
    3. system.indexes    
    4.  
    5. system.users   

    在没有加--auth的情况下 可以正常访问admin喜爱默认的两个表。

    1. > db.system.users.find()    
    2.  
    3. { "_id" : ObjectId("4e2914a585178da4e03a16c3"), "user" : "sa", "readOnly" : false, "pwd" : "75692b1d11c072c6c79332e248c4f699" }>    

    已经成功建立。

    下面把服务加上--auth的选项,再进入./mongo。

    1. MongoDB shell version: 1.8.2    
    2.  
    3. connecting to: test    
    4.  
    5. > use admin    
    6.  
    7. switched to db admin    
    8.  
    9. > show collections    
    10.  
    11. Fri Jul 22 14:38:49 uncaught exception: error: {    
    12.  
    13. "$err" : "unauthorized db:admin lock type:-1 client:127.0.0.1",    
    14.  
    15. "code" : 10057    
    16.  
    17. }    
    18.  
    19. >    

    可以看出已经没有访问权限了。

    我们就用自己的密钥登录下:

    1. > db.auth('sa','sa')    
    2.  
    3. 1   

    返回1说明验证成功!

    再show collections下就成功了。

    .....

    我们登录其它表试试:

    1. [root@:/usr/local/mongodb/bin]#./mongo    
    2.  
    3. MongoDB shell version: 1.8.2    
    4.  
    5. connecting to: test    
    6.  
    7. > use test    
    8.  
    9. switched to db test    
    10.  
    11. > show collections    
    12.  
    13. Fri Jul 22 14:40:47 uncaught exception: error: {    
    14.  
    15. "$err" : "unauthorized db:test lock type:-1 client:127.0.0.1",    
    16.  
    17. "code" : 10057    
    18.  
    19. }   

    也需要验证,试试super admin登录:

    1. [root@:/usr/local/mongodb/bin]#./mongo    
    2.  
    3. MongoDB shell version: 1.8.2    
    4.  
    5. connecting to: test    
    6.  
    7. > use test    
    8.  
    9. switched to db test    
    10.  
    11. > show collections    
    12.  
    13. Fri Jul 22 14:40:47 uncaught exception: error: {    
    14.  
    15. "$err" : "unauthorized db:test lock type:-1 client:127.0.0.1",    
    16.  
    17. "code" : 10057    
    18.  
    19. }    
    20.  
    21. > db.auth('sa','sa')    
    22.  
    23. 0   

    返回0验证失败。 

    好吧,不绕圈子,其实super admin必须从admin那么登录 然后 再use其它表才可以。

    1. > use admin    
    2.  
    3. > use admin  
    4.  
    5. switched to db admin    
    6.  
    7. > db.auth('sa','sa')    
    8.  
    9. 1    
    10.  
    11. > use test    
    12.  
    13. switched to db test    
    14.  
    15. > show collections    
    16.  
    17. >    

    如果想单独访问一个表,用独立的用户名,就需要在那个表里面建相应的user。

    1. [root@:/usr/local/mongodb/bin]#./mongo    
    2.  
    3. MongoDB shell version: 1.8.2    
    4.  
    5. connecting to: test    
    6.  
    7. > use admin    
    8.  
    9. switched to db admin    
    10.  
    11. > db.auth('sa','sa')    
    12.  
    13. 1    
    14.  
    15. > use test    
    16.  
    17. switched to db test    
    18.  
    19. > db.addUser('test','test')    
    20.  
    21. {    
    22.  
    23. "user" : "test",    
    24.  
    25. "readOnly" : false,    
    26.  
    27. "pwd" : "a6de521abefc2fed4f5876855a3484f5"    
    28.  
    29. }    
    30.  
    31. >    

    当然必须有相关权限才可以建立。

    再登录看看:

    1. [root@:/usr/local/mongodb/bin]#./mongo    
    2.  
    3. MongoDB shell version: 1.8.2    
    4.  
    5. connecting to: test    
    6.  
    7. > show collections    
    8.  
    9. Fri Jul 22 14:45:08 uncaught exception: error: {    
    10.  
    11. "$err" : "unauthorized db:test lock type:-1 client:127.0.0.1",    
    12.  
    13. "code" : 10057    
    14.  
    15. }    
    16.  
    17. > db.auth('test','test')    
    18.  
    19. 1    
    20.  
    21. > show collections    
    22.  
    23. system.indexes    
    24.  
    25. system.users    
    26.  
    27. >    

     

    抱歉!评论已关闭.