db.auth()
On this page本页内容
Definition定义
db.auth()-
Allows a user to authenticate to the database from within the shell.允许用户从shell中对数据库进行身份验证。TipStarting in version 4.2 of the从mongoshell的4.2版本开始,您可以将mongoshell, 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.passwordPrompt()方法与各种用户身份验证/管理方法/命令结合使用来提示输入密码,而不是直接在方法/命令调用中指定密码。However, you can still specify the password directly as you would with earlier versions of the但是,您仍然可以像使用早期版本的mongoshell一样直接指定密码。mongoshell.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>)语法并省略密码,则会提示用户输入密码。
Syntax语法
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> )
db.auth(<user document>)
db.auth( {
user: <username>,
pwd: passwordPrompt(), // Or "<cleartext password>"
mechanism: <authentication mechanism>,
digestPassword: <boolean>
} )
user | string | |
pwd | string |
Tip 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.passwordPrompt()方法与各种用户身份验证/管理方法/命令结合使用来提示输入密码,而不是直接在方法/命令调用中指定密码。但是,您仍然可以像使用早期版本的mongoshell一样直接指定密码。pwd. pwd。 |
mechanism | string | authentication mechanism to use.authentication mechanisms.hello command to determine the SASL mechanism or mechanisms for the specified user. See saslSupportedMechs. hello命令来确定指定用户的SASL机制。请参阅saslSupportedMechs。 |
digestPassword | boolean |
false. false。 |
Behavior行为
Client Disconnection客户端断开连接
Starting in MongoDB 4.2, if the client that issued 从MongoDB 4.2开始,如果在操作完成之前发出db.auth() disconnects before the operation completes, MongoDB marks db.auth() for termination using killOp.db.auth()的客户端断开连接,MongoDB会使用killOp标记db.auth()终止。
Example实例
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 但是,您仍然可以像使用早期版本的mongo shell.mongoshell一样直接指定密码。
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>)语法并省略密码,则会提示用户输入密码。
Authenticate after Connecting to the Shell连接到外壳后进行身份验证
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()方法:
Authenticate again as the same user on the same database.以同一数据库上的同一用户身份再次进行身份验证。Authenticate as a different user when previously authenticated on the same database.以前在同一数据库上进行身份验证时,以不同用户身份进行身份验证。Authenticate with a new database when previously authenticated on a different database.以前在其他数据库上进行身份验证时,使用新数据库进行身份验证。
Authenticate when Connecting to the Shell连接到外壳时进行身份验证
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