MongoDB permission management-user name and password operations

Source: Internet
Author: User

 

By default, users can log on to apsaradb for MongoDB without entering the user name and password. However, for the sake of security, we still need to set the user name and password for it. This document describes how to operate the username and password of MongoDB permission management.

AD:


    This document introducesMongoDB permission managementThis section describes how to set the user name and password. Next we will introduce them one by one.

    The following conditions must be met when adding a user:

    1. If you have the relevant permissions (I will talk about it later ).

    2. When mongod does not add -- auth (if it is added, the following situation occurs when you add permissions ).

     
     
    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. >    

    Therefore, when adding a user, we must first add a super admin when -- auth is not added.

    After the service is started, enter./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. >    

    In this case, the permission has been successfully created, and we will try the permission.

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

    Without -- auth, you can access the default two tables that admin prefers.

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

    Created successfully.

    Add the -- auth option to the service and then enter./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. >    

    You can see that you have no access permission.

    We can log on with our own key:

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

    1 indicates that the verification is successful!

    Then show collections.

    .....

    Try logging on to another table:

     
     
    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. }   

    You also need to verify. Try super Admin Logon:

     
     
    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 Verification Failed.

    Well, it's not going around. In fact, super admin must log on from Admin and then use other tables.

     
     
    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. >    

    If you want to access a table separately and use an independent user name, you need to create the corresponding user in the table.

     
     
    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. >    

    Of course, you must have the relevant permissions before you can create a project.

    Log on again and see:

     
     
    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. >    

     
    Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.