MongoDB set the user name and password

MongoDB default username and password is not set, we need to set up their own. In simple terms a first set of user roles to manage all user admin, then add other roles to this user as needed.

 

1. Set a user to manage all user roles admin

For example, following the use of roles and  roles myUserAdminin adminthe database  to create user .

use admin
db.createUser(
  {
    user: "myUserAdmin",
    pwd: passwordPrompt(), // or cleartext password
    roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
  }
)

Note that the above code passwordPrompt ():

Starting in version 4.2 of the mongo shell, you can use the passwordPrompt() method in conjunction with various user authentication/management methods/commands to prompt for the password instead of specifying the password directly in the method/command call. However, you can still specify the password directly as you would with earlier versions of the mongo shell.

From the mongobeginning of shell version 4.2, You can use this passwordPrompt()method with a variety of user authentication / management / commands used in combination to prompt for a password, rather than directly in the method / command invokes the specified password. However, you can still use earlier versions of like mongoshell as directly specify a password.

Then require authorization (the author does not restart after performing the above operations can be achieved directly to the next step):

use admin // If you can not restart, can not enter the phrase 
db.auth ( "myUserAdmin", "abc123 ")

Return 1 for success.

2. Create a database user

use test
db.createUser(
  {
    user: "myTester",
    pwd:  passwordPrompt(),   // or cleartext password
    roles: [ { role: "readWrite", db: "test" },
             { role: "read", db: "reporting" } ]
  }
)

 

Guess you like

Origin www.cnblogs.com/w-honey/p/11402900.html