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