0
votes

I have been battling to access MongoDB collections in my openshift web application from my Java client, and failing at every turn. I can connect but cannot query the collections in any way.

Here is the current error message:

JBWEB000070: exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=127.12.158.2:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating}, caused by {com.mongodb.MongoCommandException: Command failed with error 18: 'auth fails' on server 127.12.158.2:27017. The full response is { "code" : 18, "ok" : 0.0, "errmsg" : "auth fails" }}}]

    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:965)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:844)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:847)

JBWEB000071: root cause

com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=127.12.158.2:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating}, caused by {com.mongodb.MongoCommandException: Command failed with error 18: 'auth fails' on server 127.12.158.2:27017. The full response is { "code" : 18, "ok" : 0.0, "errmsg" : "auth fails" }}}]

    com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:370)
    com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:101)
    com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:75)
    com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:71)
    com.mongodb.binding.ClusterBinding.getReadConnectionSource(ClusterBinding.java:63)
    com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:167)
    com.mongodb.operation.FindOperation.execute(FindOperation.java:394)
    com.mongodb.operation.FindOperation.execute(FindOperation.java:57)
    com.mongodb.Mongo.execute(Mongo.java:760)
    com.mongodb.Mongo$2.execute(Mongo.java:747)
    com.mongodb.OperationIterable.iterator(OperationIterable.java:47)
    com.mongodb.FindIterableImpl.iterator(FindIterableImpl.java:135)
    com.mongodb.FindIterableImpl.iterator(FindIterableImpl.java:36)
    org.jboss.tools.example.springmvc.mongodb.model.util.MongodbUtil.getCataloguesWithoutJoins(MongodbUtil.java:184)

The code I use to obtain the mongoClient that is used throughout the MondodbUtil class mentioned in the stacktrace is:

public static MongoClient getMongoClient() {
    // If mongoClient not yet set up 
    if (mongoClient == null) {
        mongoClient = new MongoClient(new MongoClientURI(
                "mongodb://"+System.getenv("OPENSHIFT_MONGODB_DB_USER")+":"+
            System.getenv("OPENSHIFT_MONGODB_DB_PASSWORD")+"@"+
            System.getenv("OPENSHIFT_MONGODB_DB_HOST")+":"+
            System.getenv("OPENSHIFT_MONGODB_DB_PORT")+"/jbosseap"));
    }

    return mongoClient;
}

I have also tried:

public static MongoClient getMongoClient() {
    // If mongoClient not yet set up 
    if (mongoClient == null) {
        List<ServerAddress> seeds = new ArrayList<ServerAddress>();
        seeds.add( new ServerAddress(System.getenv("OPENSHIFT_MONGODB_DB_HOST"), 
                    Integer.parseInt(System.getenv("OPENSHIFT_MONGODB_DB_PORT"))));
        List<MongoCredential> credentials = new ArrayList<MongoCredential>();
        credentials.add(
            MongoCredential.createMongoCRCredential(
                "OPENSHIFT_MONGODB_DB_USER",
                "jbosseap",
                "OPENSHIFT_MONGODB_DB_PASSWORD".toCharArray()
            )
        );
        mongoClient = new MongoClient( seeds, credentials );

    }
    return mongoClient;
}

And also:

public static MongoClient getMongoClient() {
    // If mongoClient not yet set up 
    if (mongoClient == null) {

            mongoClient = new MongoClient(
                new ServerAddress(System.getenv("OPENSHIFT_MONGODB_DB_HOST"), 
                Integer.parseInt(System.getenv("OPENSHIFT_MONGODB_DB_PORT"))));

    }
    return mongoClient;
}

I have run out of ideas now, so seeking your wisdom!
2

2 Answers

1
votes

Have you kept mongod process running before executing your program? Please follow the below steps before executing your program:

  1. Press Windows key and type cmd.exe
  2. Press Ctrl+Shift+Enter to run command prompt as Administrator
  3. On command prompt, type cd c:/Mongo/mongodb/bin (replace this with your actual bin path)
  4. Make sure you have your db path configured in a folder like this: c:/data/db (this path might vary according to the command prompt you use)
  5. start mongod process by running the below command on command prompt: mongod
  6. once the mongod process starts you will get the following message on command prompt:

2017-02-26T11:33:37.233+0530 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory 'E:/data/db/diagnostic.data' 2017-02-26T11:33:37.359+0530 I NETWORK [thread1] waiting for connections on port 27017

  1. Now execute your program.

I faced a similar problem and this is how it got resolved.

-1
votes

I have fixed the problem.

Using the environment variable OPENSHIFT_MONGODB_DB_USER does not work, as this is set blank. If hard-coded to admin in the code then the problem goes away.

Somewhat confusingly (to me at least) the environment variable OPENSHIFT_MONGODB_DB_PASSWORD is set.