To authenticate as a user, you must provide a username, password, and the authentication database associated with that user.要作为用户进行身份验证,必须提供用户名、密码和与该用户关联的身份验证数据库。
It is not possible to switch between users in the same 不可能在同一个mongosh session. mongosh会话中的用户之间切换。Authenticating as a different user means the session has the privileges of both authenticated users. 以不同用户身份进行身份验证意味着会话具有两个已验证用户的权限。To switch between users exit and relaunch 在用户退出和重新启动mongosh.mongosh之间切换。
Using 使用mongosh, you can:mongosh,您可以:
Start 使用mongosh with the -u <username>, -p, and the --authenticationDatabase <database> command line options:-u <username>、-p和--authenticationDatabase <database>命令行选项启动mongosh:
mongosh --port 27017  --authenticationDatabase \
    "admin" -u "myUserAdmin" -pEnter your password when prompted.提示时输入密码。
Using 使用mongosh, connect to the mongod or mongos instance:mongosh连接到mongod或mongos实例:
mongosh --port 27017
In 在mongosh, switch to the authentication database (in this case, admin), and use the db.auth(<username>, <pwd>) method or the authenticate command to authenticate against the authentication database:mongosh中,切换到身份验证数据库(在本例中为admin),并使用db.auth(<username>, <pwd>)方法或authenticate命令对身份验证数据库进行身份验证:
use admin db.auth("myUserAdmin", passwordPrompt()) // or cleartext password
The passwordPrompt() method prompts you to enter the password. passwordPrompt()方法会提示您输入密码。You can also specify your password directly as a string. We recommend to use the 您也可以直接将密码指定为字符串。我们建议使用passwordPrompt() method to avoid the password being visible on your screen and potentially leaking the password to your shell history.passwordPrompt()方法,以避免密码在屏幕上可见,并可能将密码泄露到shell历史记录中。
Enter the password when prompted.提示时输入密码。
For examples using a MongoDB driver, see the driver documentation.有关使用MongoDB驱动程序的示例,请参阅驱动程序文档。