Mongo "auth failed" Only for Remote Connections. Local Works fine

If you're using more recent versions of MongoDB (server version 4.2.6 / shell version v3.6.9 in my case) you don't have to force them to match like in @Alexandre's example. For instance, if you're getting this error:

[thread1] Error: Authentication failed. :
DB.prototype._authOrThrow@src/mongo/shell/db.js:1608:20

You can connect with this syntax:

mongo --host mongodb://username:password@IP:PORT/ --authenticationDatabase admin

I had previously be installing MongoDB version 3.2.12 and was able to connect to a remote instance using:

mongo -u ‘<USERNAME>’ -p ‘<PASSWORD>’ --host <REPLICA_SET>/<HOST>:<PORT> admin

I am creating a new cluster with version 3.4.2 and was not able to connect with the same command. After trying many different options I was finally able to figure out that I needed to add --authenticationDatabase before the admin database.

mongo -u ‘<USERNAME>’ -p ‘<PASSWORD>’ --host <REPLICA_SET>/<HOST>:<PORT> --authenticationDatabase admin

I was facing the same issue.

The problem for me:

My local mongo shell was v2.6.10. It uses an authentication method called MONGODB-CR that has been deprecated.

My server version is v3.0.4. It uses an authentication method called SCRAM-SHA-1.

Try to check your local shell and remote server versions with:

mongo --version
mongod --version

If they are different, upgrade your local shell to v3. (I had to uninstall and install it again.)