MongoDB 使用 shell 命令连接到身份验证数据库

MongoDB connecting to authenticationdatabase using shell command

当我使用 OS shell,

连接时
mongo --port 27017 -u "testusr" -p "testpwd"  --authenticationDatabase "testdb"

它不是切换到 testdb,而是总是让我登录到 test 数据库? 我在这里缺少什么?

db.auth("testusr","testpwd") 是否在没有任何身份验证问题的情况下工作? 如何重定向到正确的数据库?

以下适合我

mongo localhost:27017/testdb -u "user" -p "pass"  --authenticationDatabase "testdb"

你可以找到帮助(我在linux中使用)

mongo --help
usage: mongo [options] [db address] [file names (ending in .js)]
db address can be:
foo                   foo database on local machine
192.169.0.5/foo       foo database on 192.168.0.5 machine
192.169.0.5:9999/foo  foo database on 192.168.0.5 machine on port 9999