Mongodb common authentication method

  • 2020-06-19 11:57:49
  • OfStack

1. Introduction

No matter how secure or local the database is, it is necessary to have a secure environment for the database.

Mongodb provides a series of security features, and here is a common method of authentication.

2. Enable validation

By default, as long as the --auth option is not added when starting the database, there is no authentication and all clients can perform all permissions.

If added, we can connect to the database with secure authentication. If you want to authenticate in the database, you can pass db.auth (username, password), return 1 if the authentication is successful, and vice versa.

3. Build users

db. createUser() method can be used to create users, such as the following:


db.createUser({user: 'username', pwd: 'password', roles: [
  {role: 'read', db: 'test'}
]});

db createUser method to accept an object, the inside of the user on behalf of the user name, pwd on behalf of the password, and roles is an array can accept multiple objects, each object can be corresponding to database, the role field on behalf of the role of database permissions, official 1 some built-in role, can query through the document.

4. Delete users

Deleting the user requires the user with permission, via the db.dropUser () method, to accept 1 string, which is the user name:

db.dropUser('user1');

5. Get users

The user information can be obtained through the db.getUser () method, which also accepts a string of 1 for the user name:

db.getUser('user1');


Related articles: