MongoDB 3.0 Security Permission access control

Source: Internet
Author: User
MongoDB security access control, domestic publishing books, online find Bowen are all using db.adduser (Username,password) to increase users. The execution of the secondary method in MongoDB 3.0 will cause an error.

elevation information indicates that the system could not find the method. The reason is that this method has been abandoned in 3.0. When using Mogodb, your show DBS will see that there is only one local database, the so-called Super admin admin is nonexistent and there is a Super Admin user useradminanydatabase. Now we're going to add a super admin to our Mogodb:

View MongoDB Official documents: Add users are using Db.createuser ()
The original text of the official document is as follows:

Db.createuser (user, Writeconcern)

  creates a new user for the database where the method runs. Db.createuser () returns a Duplicate user error if the user already exists on the database.

As seen above CreateUser requires two parameters: User and Writeconcern.

Example of User:

{User: "<name>",
  pwd: "<cleartext password>",
  customdata: {<any information>},
  roles: [
    {role: "<role>", DB: "<database>"} | "<role>",
    ...
  ]
}

User only needs four fields: Username, password, user description, role, etc. Where the customdata is optional. And as for roles can write an empty is an array.

For example:

Db.createuser (
{  User: "Root",
   pwd: "Toor",
   roles:[]}
)

We are adding a super admin:

Use admin
db.createuser
  {
    User: ' Root ',
    pwd: ' Toor ',
    roles: [{role: ' Useradminanydatabase ", DB:" Admin "}]
  }
)
As with previous versions, the user remains under the System.users collection.

View User:
Show Users
Or
Db.system.users.find () uses –auth to start MongoDB security authentication, and then we pass Db.auth in the shell client ("root", "Toor")

Executing a query at this time will cause an error
Error:error: {"$err": "Not authorized to query on Helloworld.persons", "Code": 13}
This is because we give the root user the useradminanydatabase role. This role does not have read and write permissions, only user-managed permissions.
to create a role that can read and write to a database:
Use HelloWorld
db.createuser (
 {
   User: "Wang",
   pwd: "123456",
   roles: [
      {role: "ReadWrite", db : "HelloWorld"},
      {role: "read", DB: "Test"}
   ]
 }
)

The user created in this way is a read-write role in the HelloWorld, and is readonly in test. Now let's see what's roles in MongoDB 3.0:

Refer to the translated version of this MongoDB official document:
Http://www.cnblogs.com/SamOk/p/5162767.html

Reference:

https://docs.mongodb.org

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.