mongodb adds username and password

Article Directory

The detailed explanation of roles is as follows:

Database User Roles

read: Authorize the user to read-only data, allow the user to read the specified database
readWrite Authorize the user to read/write the data, allow the user to read/write the specified database
Database Admininstration Roles (Database Admininstration Roles)
dbAdmin: perform management operations in the current database, such as index creation, deletion, statistics, viewing, etc.
dbOwner: perform any operation in the current database, add, delete, modify, check, etc.
userAdmin: manage User in the current database To create, delete and manage users.
​​​​​​​Backup and Restoration Roles​​​​​​​
cross-database roles (All-Database Roles)
readAnyDatabase: authorized to read data on all databases, only available in admin>
readWriteAnyDatabase: authorized to read and write data on all databases, only available in admin>
userAdminAnyDatabase: authorized to manage on all databases User permissions, only available in >admin
dbAdminAnyDatabase: Authorized to manage all databases, only available in admin
Cluster Administration Roles (Cluster Administration Roles)
clusterAdmin: the highest authority authorized to manage the cluster, only available in admin.
clusterManager: authority to manage and monitor the cluster
clusterMonoitor: authority to monitor the cluster, with readonly authority to the monitoring tool
super master role of the management server (super master Roles)
root: Super account and permissions, only available in admin

Set user

1. Add users

show dbs
use admin
Create an administrator
db.createUser({ user: "admin", pwd: "password", roles: [{ role: "userAdminAnyDatabase", db: "admin" }] })

Create super user

db.createUser({user: "root",pwd: "password", roles: [ { role: "root", db: "admin" } ]})

Create a normal user

use yourdatabase
db.createUser({user: "user",pwd: "password",roles: [ { role: "dbOwner", db: "yourdatabase" } ]})

2. View users
show users

3. Delete user
First enter admin

use admin

Delete a single user


Delete all users


Modify the configuration file you created

Modify mongo.conf

Join security:
authorization: enabled

Insert picture description here

log in

admin login

./bin/mongo -u admin -p admin
#【第一个admin是 用户名,第二个是 密码】
show dbs #可以看到所有的数据库

Ordinary user login


After logging in, you cannot see the database, you must go to the specified database, enter the corresponding user name and password, and you can see the data inside

use yourdatabase