I’m trying to use a simple Java program to connect to a MongoDB hosted on my Mac accessible by Compass using the authentication user and password I set up.
However, in the JDBC code, I get the error 18 (authentication failure). I have no idea why. Same connection string as in Compass.
My two dependencies in pom.xml are:
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>4.6.0</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-jdbc</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
The main method is utterly simple but fails
try
{
Class.forName("com.mongodb.jdbc.MongoDriver");
Connection connection = DriverManager.getConnection(
"mongodb://corpus:passpass@localhost:27017/?authMechanism=DEFAULT&authSource=corpus_test");
.
.
.
I’ve tried many variations on the connection string. None have worked.
Here is the error I cannot get past: “Command failed with error 18 (AuthenticationFailed): ‘Authentication failed.’ on server localhost:27017. The full response is {“ok”: 0.0, “errmsg”: “Authentication failed.”, “code”: 18, “codeName”: “AuthenticationFailed”}”
Would appreciate any suggestions on how to tackle this issue.
Thanks