Mongodb error: authentication failed [How to Solve]

Error Messages:

Database connect error:

com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName=‘root’, source=‘gateway’, password=, mechanismProperties=} Command failed with error 18 (AuthenticationFailed): ‘Authentication failed.’ on server 192.168.81.13:27017. The full response is {“ok”: 0.0, “errmsg”: “Authentication failed.”, “code”: 18, “codeName”: “AuthenticationFailed”}.

 

Solution:

Connect URL

mongodb://user:password@host:port/dbname

Add parameter authsource

For example:

mongodb://user:password@host:port/dbname?authSource=admin

In general, authsource is admin and can be replaced as needed

Read More: