MongoDB Permissions Control

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

MongoDB version is 4.0.1

1. The core idea is:

Create an administrator rights user in the Admin library,

The user then assigns the Operation library data to other libraries through this administrator privilege.

2. Steps and precautions are as follows:

① Start MongoDB Server

./mongod &

Note: There's a d behind here, and it's running in & background.

② start the MongoDB client to connect

./mongo

Note: There's no d behind here.

③ Enter the admin table and create an Admin user

Note: Just after the installation of MongoDB does not take any parameters to start, there is no user and no start permission verification mechanism.

Use admin
Db.createuser (  {    "adminUser",    pwd"  adminpwd",    "useradminanydatabase " " Admin "  } ]  })

Returns the result of the operation displayed

successfully added User: {"User":"AdminUser",        "Roles" : [                {                        "role":"Useradminanydatabase",                        "DB":"Admin"                }        ]}

④ after setting up the admin user, kill the MongoDB and start the permission verification mechanism with the start parameter--auth after killing.

./mongod--auth &

Note: Here are two-instead of just one

⑤ after the login client is connected, authorize

Use Admindb.auth ("adminUser","adminpwd")

⑥ adds a user who can read and write library data operations to the test library

Use test
Db.createuser ({    "testUser",    pwd"  Testpwd",    "readWrite""test " } ] })

Returns the result of the operation displayed

successfully added User: {"User":"TestUser",        "Roles" : [                {                        "role":"ReadWrite",                        "DB":"Test"                }        ]}

Note: You can use testuser for read and write operations on the test library in the future.

⑦ Authorized Login Test Library

Use test
Db.auth ("testUser","testpwd")

3. Summary

① need to use the user in the Admin library to create data operations for other libraries

Users in the ②admin library cannot manipulate data from other libraries (only user Rights control actions)

③ must log in to the corresponding library (such as test) to manipulate the data in the library

Above.

MongoDB Permissions Control

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.