mongodb set password

mongodb set password

Reprinted January 5, 2018 17:43:20

  • 188

 

There are some differences between mongodb password and traditional data such as mysql:

  1. 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:

  1. show dbs
    There is no admin database in the new version of mongodb, but it does not prevent the operation of step 2.
  2. use admin Enter the admin database
  3. 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.
  4. Verify that the user is added successfully in step 3.
    db.auth("useradmin", "adminpassword") If it returns 1, it means success.
    exitThe exit system
    db.auth()method is understood as the authentication function of the user
  5. Modify the configuration
    sudo vi /etc/mongod.conf
    Find #security: the uncomment and modify it to:
     
    1. security:
    2. authorization: enabled #注意缩进,缩进参照配置文件其他配置。缩进错误可能第6步重启不成功。
  6. restart mongodb sudo service mongod restart
  7. Enter mongodb, log in with the administrator account in step 3, and use this account to create other database administrator accounts
     
    1. use admin
    2. db.auth("useradmin", "adminpassword")
  8. Create a new account and password for the mongodb data you need to manage.

     
    1. use yourdatabase
    2. 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

  9. Create a new database read and write account

     
    1. use yourdatabase
    2. 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.

  10. The username and password for the data are now created.
    You can use: to linkmongodb://youruser2:yourpassword2@localhost/yourdatabase

  11. 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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325063870&siteId=291194637