To authenticate as a user, you must provide a username, password, and the authentication database associated with that user.
Importante
Using mongosh, you can:
Start mongosh with the -u <username>, -p, and the --authenticationDatabase <database> command line options:
mongosh --port 27017 --authenticationDatabase \ "admin" -u "myUserAdmin" -p
Digite sua senha quando solicitado.
Using mongosh, connect to the mongod or mongos instance:
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:
use admin db.auth("myUserAdmin", passwordPrompt()) // or cleartext password
Dica
O método passwordPrompt() solicita que você insira a senha. Você também pode especificar sua senha diretamente como uma string. Recomendamos usar o método passwordPrompt() para evitar que a senha fique visível na tela e possa vazar a senha para o histórico do shell.
Digite a senha quando solicitado.
Para obter exemplos com drivers do MongoDB, veja a documentação de driver.