I create an account on a MongoDB instance, and the account name and password are both “test”. The system returns the error “Authentication failed” when I run
mongo —host $myhost —port $myport -u test -p test.
The MongoDB account is associated with the database. The database to which the account belongs must be specified during authentication. Assume that the “test” account belongs to the admin database. You can run the following command to log on to the database:
mongo —host $myhost —port $myport -u test -p test —authenticationDatabase admin
mongo —host $myhost —port $myport
mongo> use admin
mongo> db.auth(“test”, “test”)