mongodb set password
Reprinted January 5, 2018 17:43:20
- Label:
- mongodb
- 188
There are some differences between mongodb password and traditional data such as mysql:
- MongoDB usernames and passwords are database-specific, not system-wide. All all database db need to set password
mongodb set the administrative user and password:
show dbs
There is no admin database in the new version of mongodb, but it does not prevent the operation of step 2.use admin
Enter the admin database- Creating
db.createUser({ user: "useradmin", pwd: "adminpassword", roles: [{ role: "userAdminAnyDatabase", db: "admin" }] })
a user in the administrator account mongodb is based on the identity role, and the role of the administrator account is userAdminAnyDatabase. 'userAdmin' stands for user admin identity and 'AnyDatabase' means can manage any database. - Verify that the user is added successfully in step 3.
db.auth("useradmin", "adminpassword")
If it returns 1, it means success.
exit
The exit system
db.auth()
method is understood as the authentication function of the user - Modify the configuration
sudo vi /etc/mongod.conf
Find#security:
the uncomment and modify it to:security:
authorization: enabled #注意缩进,缩进参照配置文件其他配置。缩进错误可能第6步重启不成功。
- restart mongodb
sudo service mongod restart
- Enter mongodb, log in with the administrator account in step 3, and use this account to create other database administrator accounts
use admin
db.auth("useradmin", "adminpassword")
-
Create a new account and password for the mongodb data you need to manage.
use yourdatabase
db.createUser({ user: "youruser", pwd: "yourpassword", roles: [{ role: "dbOwner", db: "yourdatabase" }] })
rote:dbOwner represents the database owner role and has the highest authority of the database. such as creating an index
-
Create a new database read and write account
use yourdatabase
db.createUser({ user: "youruser2", pwd: "yourpassword2", roles: [{ role: "readWrite", db: "yourdatabase" }] })
This user is used to read and write the data and only has read and write permissions.
-
The username and password for the data are now created.
You can use: to linkmongodb://youruser2:yourpassword2@localhost/yourdatabase
-
user: username, pwd: password, roles: specify user roles, you can use an empty array to set empty roles for new users; in the roles field, you can specify built-in roles and user-defined roles. The roles in role can be selected:
Built-In Roles: 1. Database user roles: read, readWrite; 2. Database management roles: dbAdmin, dbOwner, userAdmin; 3. Cluster management roles: clusterAdmin, clusterManager, clusterMonitor, hostManager; 4. Backup and recovery roles: backup, restore; 5. All database roles: readAnyDatabase, readWriteAnyDatabase, userAdminAnyDatabase, dbAdminAnyDatabase 6. Superuser role: root 7. Internal role: __system