On this page本页内容
db.auth()
Allows a user to authenticate to the database from within the shell.允许用户从shell中对数据库进行身份验证。
Starting in version 4.2 of the 从mongoshell的4.2版开始,您可以将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. passwordPrompt()
方法与各种用户身份验证/管理方法/命令结合使用,提示输入密码,而不是直接在方法/命令调用中指定密码。However, you can still specify the password directly as you would with earlier versions of the 但是,您仍然可以像使用早期版本的mongoshell那样直接指定密码。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开始,您可以:
Omit the password to prompt the user to enter a password:省略密码以提示用户输入密码:
db.auth( <username> )
Use 使用passwordPrompt()
to prompt the user to enter a password:passwordPrompt()
提示用户输入密码:
db.auth( <username>, passwordPrompt() )
Specify a cleartext password.指定明文密码。
db.auth( <username>, <password> )
Starting in MongoDB 4.2, you can either:从MongoDB 4.2开始,您可以:
Use 使用passwordPrompt()
to prompt the user to enter a password:passwordPrompt()
提示用户输入密码:
db.auth( <username>, passwordPrompt() )
Specify a cleartext password:指定明文密码:
db.auth( <username>, <password> )
In MongoDB 4.0 and earlier, you must specify a cleartext password:在MongoDB 4.0及更早版本中,必须指定明文密码:
db.auth( <username>, <password> )
db.auth(<user document>)
db.auth( { user: <username>, pwd: passwordPrompt(), // Or "<cleartext password>" mechanism: <authentication mechanism>, digestPassword: <boolean> } )
user | string | |
pwd | string |
|
mechanism | string |
|
digestPassword | boolean |
|
db.auth()
returns 0
when authentication is not successful, and 1
when the operation is successful.db.auth()
在身份验证不成功时返回0
,在操作成功时返回1
。Starting in MongoDB 4.2, if the client that issued the 从MongoDB 4.2开始,如果发出db.auth()
disconnects before the operation completes, MongoDB marks the db.auth()
for termination (i.e. killOp
on the operation).db.auth()
的客户端在操作完成之前断开连接,MongoDB将db.auth()
标记为终止(即操作上的killOp
)。
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 要在连接mongosh
, issue db.auth()
in the user's authentication database:mongosh
后进行身份验证,请在用户的身份验证数据库中发出db.auth()
:
use test
db.auth( "myTestDBUser", passwordPrompt() )
Starting in MongoDB 4.4, you can omit the 从MongoDB 4.4开始,您可以完全省略password
value entirely to prompt the user to enter their password:password
值以提示用户输入密码:
use test
db.auth( "myTestDBUser" )
Starting in MongoDB 5.0, if your connection specifies the 从MongoDB 5.0开始,如果连接指定了--apiStrict
option, you may not use the db.auth()
method to:--apiStrict
选项,则不能使用db.auth()
方法:
Alternatively, you can use 或者,您可以使用mongosh
's command-line options --username
, --password
, --authenticationDatabase
, and --authenticationMechanism
to specify authentication credentials when connecting mongosh
:mongosh
的命令行选项--username
、--password
、--authenticationDatabase
和--authenticationMechanism
在连接mongosh
时指定身份验证凭据:
mongosh --username "myTestDBUser" --password --authenticationDatabase test --authenticationMechanism SCRAM-SHA-256