com.mongodb.MongoCommandException: Command failed with error 18

I have Java SpringBoot application connecting to a database in MongoDB enterprise 7.x version. The application runs in OpenShift container platform. Previously it used in PCF Cloud. I have observed below error occurring randomly.

When i try to test using postman, i randomly get this error. During subsequent attempt to run the same test, connection is successful.

Any idea what needs to be changed so that the application won’t get this error?

=============
Connection String:
mongodb://xxxxxxx:xxxxxxx@xxxxxxx,xxxxxxx,xxxxxxx/dbname?replicaSet=rs_replicaname&authMechanism=PLAIN&authSource=$external

======
ERROR:
Waiting for server to become available for operation with ID 1069229. Remaining time: 29999 ms. Selector: ReadPreferenceServerSelector{readPreference=primary}, topology description: {type=REPLICA_SET, servers=[{address=xxxxxxx, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=PLAIN, userName=‘xxxxxxxxxxxxx’, source=‘$external’, password=, mechanismProperties=}}, caused by {com.mongodb.MongoCommandException: Command failed with error 18 (AuthenticationFailed): ‘Authentication failed.’ on server xxxxxxx. The full response is {“ok”: 0.0, “errmsg”: “Authentication failed.”, “code”: 18, “codeName”: “AuthenticationFailed”, “$clusterTime”: {“clusterTime”: {“$timestamp”: {“t”: 1752859399, “i”: 1}}, “signature”: {“hash”: {“$binary”: {“base64”: “xxxxxxxxxxxx”, “subType”: “00”}}, “keyId”: xxxxxxxxxx}}, “operationTime”: {“$timestamp”: {“t”: 1752859399, “i”: 1}}}}}, {address=xxxxxxxxxxxxxx, type=REPLICA_SET_SECONDARY, roundTripTime=37.5 ms, state=CONNECTED}, {address=xxxxxxxxxxxx, type=REPLICA_SET_SECONDARY, roundTripTime=20.5 ms, state=CONNECTED}, {address=xxxxxxxxxxxxx, type=REPLICA_SET_SECONDARY, roundTripTime=24.1 ms, state=CONNECTED}, {address=xxxxxxxxx, type=REPLICA_SET_SECONDARY, roundTripTime=0.6 ms, state=CONNECTED}, {address=xxxxxxxxxxx, type=REPLICA_SET_SECONDARY, roundTripTime=32.0 ms, state=CONNECTED}]

Hi @Babu_Krishnamoorthy

Further information detailing the authentication error will be available in the mongod server logs.

There are a few settings that can improve LDAP/AD authentication.
Use https://support.mongodb.com and search for LDAP best practices or recommendations. Number 1 on my list would be to to enable LDAP connection pooling.

Opening a support case with MongoDB would also be a good idea.

Thank you, Chris. I am unable to find a posting in support forum on the LDAP best practices topic. I will check on the LDAP connection pooling and plan to submit a case with MongoDB.

1 Like

Support is best to guide you on this.

This is the KB I was referring to: https://support.mongodb.com/article/000020058

Thank you, Chris! I get an error to access this KB article. I will reach out to the MongoDB support to create a support case.

“To access the MongoDB Support Portal, please ensure that you are a member of a supported customer project.”