MongoDB Permission Validation

Source: Internet
Author: User
Tags auth mongodb client mongodb server

In MongoDB, the service start by default is not permission authentication, in terms of security, this is certainly not possible, so need to add permission validation.

Since it is necessary to do permission verification, it must have a user, so the first step in authorization authentication is to add users to the MongoDB library.

Start the MongoDB server, and then start the MongoDB client, using commands from the client to add users to the MongoDB library, see the code:

[SQL]View Plaincopy
    1. > Db.adduser ("sa","123");
    2. {
    3. "user": "sa",
    4. "ReadOnly": false,
    5. "pwd": "F7559CFE0BC0A305F82C0D83664538FD"
    6. }

tip: Adding users to the admin library is global, which means adding users to the admin, which is also applicable in other libraries.

To add permission validation to the server side of MongoDB, you need to add the--auth parameter when starting MongoDB service, the code is as follows:

[SQL]View Plaincopy
  1. D:\mongodb\bin>mongod --dbpath d:\mongodb\data--logpath d:\mongodb\log\mongodb.log--logappend--servicename MongoDB--auth--install
  2. All output going to:d:\mongodb\log\mongodb.log
  3. Creating service MongoDB.
  4. Service creation successful.
  5. Service can is started from the command line via ' net start ' MongoDB '.
  6. D:\mongodb\bin>net start "MongoDB"
  7. Mongo DB Service is starting.
  8. The Mongo DB service has started successfully.
Here I start the MongoDB service as a service, the first command is to install the service, and the second one is to start the service.

Service has been started, then left open the client, this is simple, look at the code:

[SQL]View Plaincopy
  1. D:\mongodb\bin>mongo
  2. MongoDB Shell version:1.8.2
  3. Connecting to:test
  4. > Show DBS;
  5. Mon Oct 21:54:13 uncaught exception:listdatabases failed:{
  6. " assertion": "unauthorized db:admin lock Type:-1 client:127.0.0.1",
  7. "Assertioncode": 10057,
  8. " errmsg": "DB assertion Failure",
  9. "OK": 0
  10. }
See, Show DBS Command reported abnormal, and then look at the specific information of the exception, unauthorized db:admin lock, permission verification does not pass AH!

Well, here is the authorization authentication, here nagging, in the authentication of permissions, the first exit the client, or another client, the code is as follows:

[SQL]View Plaincopy
    1. D:\mongodb\bin>mongo admin-u sa-p 123
    2. MongoDB Shell version:1.8.2
    3. Connecting to:admin
    4. > Show DBS;
    5. Admin 0.078125GB
    6. Local (empty)
    7. Test (empty)
Command format: MONGO [database name]-u [user name]-p [Password]

The show DBS above can execute and see the results.

In this way, the process of permission validation is complete.

MongoDB Permission Validation

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.