- MongoDB version v4.0.7
- System Win10
note:
To create a user for the database, you must first switch to the appropriate database:
To create a user for the database, you must first switch to the appropriate database:
To create a user for the database, you must first switch to the appropriate database:
# First switching use database name; # then create db.createUser ({})
Super Admin
Set admin (for admin password at the library)
use.admin db.createUser ({ User: ' ADMIN ' , // username pwd: ' 123456 ' , // Password Roles: [{ Role: ' the root ' , // Roles DB: ' ADMIN ' // database }] })
Setup is complete, you can enter show users
to see if the setting is successful.
Open the verification
MongoDB find installation directory, open mongodb.conf file, locate the following sentence
authu=true
Restart MongoDB
kill -9 mongo process ID
./mongod -f mongodb.conf
Close the mongod --journal --shutdown -f / usr / local / MongoDB / bin / mongodb.conf start the mongod --journal -f /usr/local/mongodb/bin/mongodb.conf
Find MongoDB service, right restarted.
At this time, we can open the powershell connect to the database:
Input mongo:
![](https://img2018.cnblogs.com/i-beta/1513642/202001/1513642-20200104192610522-911905172.png)
Show a successful connection, but when we enter other instructions are not prompted to no authority but does not perform:
Log database
// embodiment a Mongo use ADMIN db.auth ( ' ADMIN ' , ' 123456 ' ) // Second way Mongo ADMIN -u ADMIN -p 123456
This time we can properly access and manipulate the data.
Adding database users
Apart from the super administrator can set up the database, you also can set up a separate administrator for each database. Its only operating authority must separate the data.
use test // 跳转到需要添加用户的数据库
db.createUser({ user: 'mts', // 用户名 pwd: '123456', // 密码 roles:[{ role: 'readWrite', // 角色 db: 'qidatas' // 数据库名 }] })
Common Commands
show users // 查看当前库下的用户 db.dropUser('testadmin') // 删除用户 db.updateUser('admin', {pwd: '654321'}) // 修改用户密码 db.auth('admin', '654321') // 密码认证
MongoDB 数据库默认角色
数据库用户角色:read、readWrite
数据库管理角色:dbAdmin、dbOwner、userAdmin
集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager
备份恢复角色:backup、restore
所有数据库角色: readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、
dbAdminAnyDatabase
超级用户角色:root