To apply for security credentials in MongoDB, you need to configure authentication and authorization mechanisms. Here's a step-by-step guide:
Enable Authentication:
--auth parameter or set security.authorization: enabled in the configuration file (mongod.conf).security:
authorization: enabled
Create User Accounts:
admin database.db.createUser() method to create users with roles.use admin
db.createUser({
user: "adminUser",
pwd: "securePassword",
roles: ["root"]
})
Assign Roles:
read, readWrite, dbAdmin) or custom roles.use myDatabase
db.createUser({
user: "appUser",
pwd: "appPassword",
roles: ["readWrite"]
})
Connect Using Credentials:
mongo shell or a driver with the username and password.mongo -u adminUser -p securePassword --authenticationDatabase admin
For managed MongoDB services like Tencent Cloud Database for MongoDB, credentials are typically auto-generated during instance creation. You can manage users and permissions via the Tencent Cloud console or API.
Example (Tencent Cloud):
Always follow the principle of least privilege when assigning roles.