- Reference >
mongo
Shell Methods >- User Management Methods >
- db.auth()
db.auth()¶
On this page
Definition¶
-
db.
auth
()¶ Allows a user to authenticate to the database from within the shell.
Tip
Starting in version 4.2 of the
mongo
shell, you can use thepasswordPrompt()
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 themongo
shell.The
db.auth()
method can accept either:the username and password.
a user document that contains the username and password, and optionally, the authentication mechanism and a digest password flag.
Parameter Type Description username
string The name of the user with access privileges for this database. password
string The user’s password. The value can be either:
- the user’s password in cleartext string, or
passwordPrompt()
to prompt for the user’s password.
Tip
Starting in version 4.2 of the
mongo
shell, you can use thepasswordPrompt()
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 themongo
shell.mechanism
string Optional. The authentication mechanism to use.
For available mechanisms, see authentication mechanisms.
If unspecified, uses the
isMaster
to determine the SASL mechanism or mechanisms for the specified user. SeesaslSupportedMechs
.digestPassword
boolean Optional. Determines whether or not the supplied password should be pre-hashed before being used with the specified authentication mechanism.
- For SCRAM-SHA-1, although you may
specify
true
, setting this value totrue
does not improve security and may interfere with credentials using other mechanisms. - For all other methods, this value must be set to
false
(default value). Any other value will result in authentication failure since those methods do not understand MongoDB pre-hashing.
The default value is
false
.Returns: db.auth()
returns0
when authentication is not successful, and1
when the operation is successful.
Behavior¶
Example¶
Tip
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.
To authenticate after connecting the mongo
shell, issue
db.auth()
in the user’s authentication database:
Alternatively, you can use the mongo
shell’s
command-line options --username
,
--password
,
--authenticationDatabase
,
and --authenticationMechanism
to specify authentication credentials when
connecting the mongo
shell: