MongoDB experiment - MongoDB configures user access control

MongoDB configures user access control

1. Experimental principles

Understanding the admin database: When installing MongoDB, the admin database will be automatically created. This is a special database that provides functions that ordinary databases do not have. For example, some account roles give users permission to operate multiple databases, and these roles can only be used in the admin database. Created in . When checking credentials, MongoDB will check the user account in the specified database and the admin database.

Create user account

An important part of database administration is creating user accounts that can manage users and databases, as well as read and write to the database. To add a user, use methods in the MongoDB shell createUser()。createUser()that take a document object as a parameter, allowing you to specify a username, role, and password. The fields that can be specified in the document object are listed below.

image-20221105210631583

MongoDB provides a variety of roles that can be assigned to user accounts. These roles allow you to grant complex permissions and restrictions to user accounts. Listed below are some common roles that can be assigned to users.

image-20221105210728236

image-20221105210738186

Tip: Roles readAnyDatabase、readWriteAnyDatabase、dbAdminAnyDatabase和userAdminAnyDatabasecan only be assigned to user accounts in the admin database because they specify permissions on all databases

2. Experimental steps

  1. Start the MongoDB database
  2. Create a super account now

image-20221105213339063

The creation is successful, where user is the user name, pwd is the password, and roles are the roles of the specified user. You can use an empty array to set empty roles for the new user; in the roles field, you can specify built-in roles and user-defined roles. The roles in role can be selected.

  1. Next we close the MongoDB data service and verify the root account

image-20221106141438256

  1. Before logging in, authentication should be enabled, open the mongodb.config file, and remove the # in front of "#auth =true".

image-20221106142036592

  1. Start the mongod service

image-20221106143539804

  1. Next we use the root super user and specify the admin library to log in.

image-20221106144031053

  1. Query the database currently in use and query all database names

image-20221106144122153

  1. Under the test database, create read-only users and read-write users

image-20221106144534417

image-20221106145151661

  1. View all users under the current library

image-20221106145234991

2 accounts were created above, now verify their permissions

  1. Enter the exit command to exit the current user and enter the read-only user 'zhangyur'.

image-20221106145454863

  1. Insert data into the collection mycollection

image-20221106145716079

The insertion failed because we only gave it read-only permissions when we created the user, so data could not be inserted. We switched to the 'zhangyu' user with read-write permissions and inserted data again.

image-20221106145911579

  1. Create a cross-database user, switch to the admin database, log in as the root user, and create a user for the test library under the admin library.

image-20221106150511386

  1. Query all users

image-20221106150554348

  1. You can see that the account kuaku of the 'test' library exists under the admin library. Switch to the test library to verify the kuaku user.

image-20221106150639967

Authentication failed! We then switch to the 'admin' library to verify the kuaku user.

image-20221106150719465

Certification successful! The results show that users created under admin cannot be directly authenticated in other libraries, but can only be authenticated under the user's creation library. The database account follows the database, and is authenticated wherever it is created.

  1. So many users have been created, let’s query all users

image-20221106151059851

image-20221106151113290

image-20221106151128368

image-20221106151138092

  1. Delete 'kuaku' user

image-20221106151223806

At this point, the experiment is over!

Guess you like

Origin blog.csdn.net/weixin_57367513/article/details/132582377