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​​​​​​​
backup
restore
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
hostManager:
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" }] })
1

Create super user

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

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
db.auth("admin","password")
1

Delete a single user

db.system.users.remove({user:"XXXXXX"})
1

Delete all users

db.system.users.remove({})

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
1
#【第一个admin是 用户名,第二个是 密码】
show dbs #可以看到所有的数据库

Ordinary user login

./bin/mongo
1

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

db.auth("user1","root")