On this page本页内容
db.
auth
()¶Allows a user to authenticate to the database from within the shell.允许用户从shell中对数据库进行身份验证。
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. mongo
shell的4.2版开始,您可以将passwordPrompt()
方法与各种用户身份验证/管理方法/命令结合使用来提示输入密码,而不是直接在方法/命令调用中指定密码。However, you can still specify the password directly as you would with earlier versions of the 但是,您仍然可以像使用早期版本的mongo
shell.mongo
shell一样直接指定密码。
Starting in MongoDB 4.4, if you use the 从MongoDB 4.4开始,如果使用db.auth(<username>, <password>)
syntax and omit the password, the user is prompted to enter a password.db.auth(<username>, <password>)
语法并省略密码,则会提示用户输入密码。
The db.auth()
has the following syntax forms:db.auth()
具有以下语法形式:
db.auth(<username>, <password>)
¶Starting in MongoDB 4.4, you can either:从MongoDB 4.4开始,您可以:
passwordPrompt()
to prompt the user to enter a password:passwordPrompt()
提示用户输入密码:
Starting in MongoDB 4.2, you can either:
passwordPrompt()
to prompt the user to enter a password:passwordPrompt()
提示用户输入密码:
In MongoDB 4.0 and earlier, you must specify a cleartext password:在MongoDB 4.0及更早版本中,必须指定明文密码:
db.auth(<user document>)
¶user |
string | |
pwd |
string |
Tip
|
mechanism |
string |
|
digestPassword |
boolean |
|
db.auth()
0
when authentication is not successful, and 1
when the operation is successful.0
,操作成功时返回1
。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. mongo
shell的4.2版开始,您可以将passwordPrompt()
方法与各种用户身份验证/管理方法/命令结合使用来提示输入密码,而不是直接在方法/命令调用中指定密码。However, you can still specify the password directly as you would with earlier versions of the 但是,您仍然可以像使用早期版本的mongo
shell.mongo
shell一样直接指定密码。
Starting in MongoDB 4.4, if you use the 从MongoDB 4.4开始,如果使用db.auth(<username>, <password>)
syntax and omit the password, the user is prompted to enter a password.db.auth(<username>, <password>)
语法并省略密码,则会提示用户输入密码。
To authenticate after connecting the 要在连接mongo
shell, issue db.auth()
in the user’s authentication database:mongo
shell后进行身份验证,请在用户的身份验证数据库中发出db.auth()
:
Starting in MongoDB 4.4, you can omit the 从MongoDB 4.4开始,您可以完全省略password
value entirely to prompt the user to enter their password:password
以提示用户输入密码:
Alternatively, you can use the 或者,您可以使用mongo shell的命令行选项mongo
shell’s command-line options --username
, --password
, --authenticationDatabase
, and --authenticationMechanism
to specify authentication credentials when connecting the mongo
shell:--username
、--password
、--authenticationDatabase
和--authenticationMechanism
来指定连接mongo
shell时的身份验证凭据: